Skip to content

Commit 1f7fbf8

Browse files
Add authorization roles (#61)
* Update auth library version * Add API Key for appwrite * Add API Key for appwrite in dockerfile * Pass API-Key of Appwrite as application property * Change repository * Rename Authrepository * Add authwithusertype repository * Bump com.hrv.mart:auth-library from 0.0.2 to 0.0.3 (#62) Bumps com.hrv.mart:auth-library from 0.0.2 to 0.0.3. --- updated-dependencies: - dependency-name: com.hrv.mart:auth-library dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Harsh Verma <55652117+Harsh3305@users.noreply.github.com> * Changes done related to upgrade of auth-library * Remove unused function * Add exist by and remove userBy * Update auth service. Add authWithUserType repostory in service * Remove unused imports * Minor changes --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 8075b55 commit 1f7fbf8

File tree

9 files changed

+95
-25
lines changed

9 files changed

+95
-25
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ env:
1515
KAFKA_URL: localhost:9092
1616
APPWRITE_ENDPOINT: http://localhost/v1
1717
APPWRITE_PROJECT_ID: PROJECT_ID
18-
APPWRITE_APIKEY: LONG_API_KEY
18+
APPWRITE_AUTH_API_KEY: LONG_API_KEY
1919

2020
jobs:
2121
build:

.github/workflows/docker-images.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ env:
2020
KAFKA_URL: ${{secrets.KAFKA_URL}}
2121
APPWRITE_ENDPOINT: ${{secrets.APPWRITE_ENDPOINT}}
2222
APPWRITE_PROJECT_ID: ${{secrets.APPWRITE_PROJECT_ID}}
23-
APPWRITE_APIKEY: ${{secrets.APPWRITE_APIKEY}}
23+
APPWRITE_AUTH_API_KEY: ${{secrets.APPWRITE_APIKEY}}
2424

2525
jobs:
2626
push_to_registries:

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG MONGODB_URI=mongodb://localhost:27017
44
ARG KAFKA_URL=localhost:9092
55
ARG APPWRITE_ENDPOINT=http://localhost/v1
66
ARG APPWRITE_PROJECT_ID=PROJECT_ID
7-
ARG APWRITE_APIKEY=LONG_API_KEY
7+
ARG APPWRITE_AUTH_API_KEY=LONG_API_KEY
88
ENV MONGODB_URI=$MONGODB_URI
99
ENV APPLICATION_PORT 8082
1010
ENV KAFKA_URL=$KAFKA_URL

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies {
3939
// User Model
4040
implementation("com.hrv.mart:user-library:0.0.3")
4141
// Auth Library
42-
implementation("com.hrv.mart:auth-library:0.0.2")
42+
implementation("com.hrv.mart:auth-library:0.0.3")
4343
// Kafka
4444
implementation("org.springframework.kafka:spring-kafka")
4545
testImplementation("org.springframework.kafka:spring-kafka-test")
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.hrv.mart.backendauth.repository
22

3-
import com.hrv.mart.authlibrary.model.Auth
3+
import com.hrv.mart.authlibrary.model.AppWriteAuth
44
import io.appwrite.Client
55
import io.appwrite.services.Account
66
import kotlinx.coroutines.runBlocking
@@ -10,25 +10,28 @@ import reactor.core.publisher.Mono
1010
import reactor.kotlin.core.publisher.toMono
1111

1212
@Repository
13-
class AuthRepository (
13+
class AppWriteAuthRepository (
1414
@Autowired
1515
private val client: Client
1616
)
1717
{
18-
fun getAuthAccount(jwt: String): Mono<Auth> {
18+
fun getAuthAccount(jwt: String): Mono<AppWriteAuth> {
1919
client.setJWT(jwt)
2020
val account = Account(client)
21-
return runBlocking{account.get()}
21+
return runBlocking{
22+
account.get()
23+
}
2224
.toMono()
2325
.map {details ->
24-
Auth(
26+
27+
AppWriteAuth(
2528
name = details.name,
2629
email = details.email,
30+
userId = details.id,
2731
emailVerification = details.emailVerification,
2832
createdAt = details.createdAt,
2933
updatedAt = details.updatedAt
3034
)
3135
}
32-
3336
}
3437
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hrv.mart.backendauth.repository
2+
3+
import com.hrv.mart.authlibrary.model.AuthWithUserType
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
5+
import org.springframework.stereotype.Repository
6+
import reactor.core.publisher.Mono
7+
8+
@Repository
9+
interface AuthWithUserTypeRepository : ReactiveMongoRepository<AuthWithUserType, String> {
10+
fun existsByUserId(userId: String): Mono<Boolean>
11+
fun findByUserId(userId: String): Mono<AuthWithUserType>
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.hrv.mart.backendauth.service
22

3-
import com.hrv.mart.authlibrary.model.Auth
3+
import com.hrv.mart.authlibrary.model.AuthWithUserType
44
import com.hrv.mart.authlibrary.model.UserType
5-
import com.hrv.mart.backendauth.repository.AuthRepository
5+
import com.hrv.mart.backendauth.repository.AppWriteAuthRepository
6+
import com.hrv.mart.backendauth.repository.AuthWithUserTypeRepository
67
import com.hrv.mart.backendauth.repository.KafkaRepository
78
import org.springframework.beans.factory.annotation.Autowired
89
import org.springframework.http.HttpStatus
@@ -13,7 +14,9 @@ import reactor.core.publisher.Mono
1314
@Service
1415
class AuthService (
1516
@Autowired
16-
private val authRepository: AuthRepository,
17+
private val appWriteAuthRepository: AppWriteAuthRepository,
18+
@Autowired
19+
private val authWithUserTypeRepository: AuthWithUserTypeRepository,
1720
@Autowired
1821
private val kafkaRepository: KafkaRepository
1922
)
@@ -22,9 +25,13 @@ class AuthService (
2225
jwt: String,
2326
userType: UserType,
2427
response: ServerHttpResponse
25-
): Mono<Auth> {
26-
userType.name
27-
return authRepository.getAuthAccount(jwt)
28+
) =
29+
appWriteAuthRepository
30+
.getAuthAccount(jwt)
31+
.flatMap {
32+
insertUserType(it.userId, userType)
33+
.then(Mono.just(it))
34+
}
2835
.flatMap {auth ->
2936
response.statusCode = HttpStatus.OK
3037
kafkaRepository
@@ -35,5 +42,35 @@ class AuthService (
3542
response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR
3643
Mono.empty()
3744
}
38-
}
45+
private fun insertUserType(userId: String, userType: UserType) =
46+
authWithUserTypeRepository
47+
.existsByUserId(userId)
48+
.flatMap {
49+
if (it) {
50+
if (userType == UserType.ADMIN) {
51+
authWithUserTypeRepository
52+
.findByUserId(userId)
53+
.flatMap { authWithUserType ->
54+
if (authWithUserType.userType == UserType.ADMIN) {
55+
Mono.empty()
56+
}
57+
else {
58+
Mono.error(Throwable("User do not have required access"))
59+
}
60+
}
61+
}
62+
else {
63+
Mono.empty()
64+
}
65+
}
66+
else {
67+
authWithUserTypeRepository
68+
.insert(
69+
AuthWithUserType(
70+
userId = userId,
71+
userType = UserType.USER
72+
)
73+
)
74+
}
75+
}
3976
}

src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ spring.kafka.producer.value-serializer= org.springframework.kafka.support.serial
66
spring.kafka.consumer.group-id=user
77
hrv.mart.appwrite.endPoint=${APPWRITE_ENDPOINT}
88
hrv.mart.appwrite.projectId=${APPWRITE_PROJECT_ID}
9-
hrv.mart.appwrite.apikey=${APPWRITE_APIKEY}
9+
hrv.mart.appwrite.apikey=${APPWRITE_AUTH_API_KEY}

src/test/kotlin/com/hrv/mart/backendauth/controller/AuthControllerTest.kt

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.hrv.mart.backendauth.controller
22

3-
import com.hrv.mart.authlibrary.model.Auth
3+
import com.hrv.mart.authlibrary.model.AppWriteAuth
44
import com.hrv.mart.authlibrary.model.AuthRequest
5+
import com.hrv.mart.authlibrary.model.AuthWithUserType
56
import com.hrv.mart.authlibrary.model.UserType
6-
import com.hrv.mart.backendauth.repository.AuthRepository
7+
import com.hrv.mart.backendauth.repository.AppWriteAuthRepository
8+
import com.hrv.mart.backendauth.repository.AuthWithUserTypeRepository
79
import com.hrv.mart.backendauth.repository.KafkaRepository
810
import com.hrv.mart.backendauth.service.AuthService
911
import io.appwrite.exceptions.AppwriteException
@@ -18,28 +20,44 @@ import reactor.test.StepVerifier
1820
import java.util.*
1921

2022
class AuthControllerTest {
21-
private val mockAuthRepository = mock(AuthRepository::class.java)
23+
private val mockAppWriteAuthRepository = mock(AppWriteAuthRepository::class.java)
2224
private val mockKafkaRepository = mock(KafkaRepository::class.java)
25+
private val mockAuthWithUserTypeRepository = mock(AuthWithUserTypeRepository::class.java)
2326
private val response = mock(ServerHttpResponse::class.java)
2427

25-
private val authService = AuthService(mockAuthRepository, mockKafkaRepository)
28+
private val authService = AuthService(
29+
mockAppWriteAuthRepository,
30+
mockAuthWithUserTypeRepository,
31+
mockKafkaRepository
32+
)
2633
private val authController = AuthController(authService)
2734

2835
@Test
2936
fun `should return login successful message if jwt is valid`(): Unit = runBlocking {
3037
val jwt = "A_VALID_JWT"
3138
val userType = UserType.USER
3239

33-
val auth = Auth(
40+
val auth = AppWriteAuth(
41+
userId = UUID.randomUUID().toString(),
3442
email = "test@test.com",
3543
emailVerification = true,
3644
createdAt = Date().toString(),
3745
updatedAt = Date().toString(),
3846
name = "Test User"
3947
)
48+
val authWithUserType = AuthWithUserType(
49+
userId = auth.userId,
50+
userType = userType
51+
)
4052
doReturn(Mono.just(auth))
41-
.`when`(mockAuthRepository)
53+
.`when`(mockAppWriteAuthRepository)
4254
.getAuthAccount(jwt)
55+
doReturn(Mono.just(authWithUserType))
56+
.`when`(mockAuthWithUserTypeRepository)
57+
.findByUserId(auth.userId)
58+
doReturn(Mono.just(true))
59+
.`when`(mockAuthWithUserTypeRepository)
60+
.existsByUserId(auth.userId)
4361
doReturn(Mono.empty<SenderResult<Void>>())
4462
.`when`(mockKafkaRepository)
4563
.createUser(auth.toUser())
@@ -61,7 +79,7 @@ class AuthControllerTest {
6179

6280

6381
doReturn(Mono.error<AppwriteException>(AppwriteException("JWT is invalid")))
64-
.`when`(mockAuthRepository)
82+
.`when`(mockAppWriteAuthRepository)
6583
.getAuthAccount(jwt)
6684

6785
StepVerifier

0 commit comments

Comments
 (0)