diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7791323..7d271b1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,7 +15,7 @@ env: KAFKA_URL: localhost:9092 APPWRITE_ENDPOINT: http://localhost/v1 APPWRITE_PROJECT_ID: PROJECT_ID - APPWRITE_APIKEY: LONG_API_KEY + APPWRITE_AUTH_API_KEY: LONG_API_KEY jobs: build: diff --git a/.github/workflows/docker-images.yml b/.github/workflows/docker-images.yml index da87022..06706b6 100644 --- a/.github/workflows/docker-images.yml +++ b/.github/workflows/docker-images.yml @@ -20,7 +20,7 @@ env: KAFKA_URL: ${{secrets.KAFKA_URL}} APPWRITE_ENDPOINT: ${{secrets.APPWRITE_ENDPOINT}} APPWRITE_PROJECT_ID: ${{secrets.APPWRITE_PROJECT_ID}} - APPWRITE_APIKEY: ${{secrets.APPWRITE_APIKEY}} + APPWRITE_AUTH_API_KEY: ${{secrets.APPWRITE_APIKEY}} jobs: push_to_registries: diff --git a/Dockerfile b/Dockerfile index c8d0c8c..3ccc624 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ ARG MONGODB_URI=mongodb://localhost:27017 ARG KAFKA_URL=localhost:9092 ARG APPWRITE_ENDPOINT=http://localhost/v1 ARG APPWRITE_PROJECT_ID=PROJECT_ID -ARG APWRITE_APIKEY=LONG_API_KEY +ARG APPWRITE_AUTH_API_KEY=LONG_API_KEY ENV MONGODB_URI=$MONGODB_URI ENV APPLICATION_PORT 8082 ENV KAFKA_URL=$KAFKA_URL diff --git a/build.gradle.kts b/build.gradle.kts index 7e9f719..ed31753 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,7 +39,7 @@ dependencies { // User Model implementation("com.hrv.mart:user-library:0.0.3") // Auth Library - implementation("com.hrv.mart:auth-library:0.0.2") + implementation("com.hrv.mart:auth-library:0.0.3") // Kafka implementation("org.springframework.kafka:spring-kafka") testImplementation("org.springframework.kafka:spring-kafka-test") diff --git a/src/main/kotlin/com/hrv/mart/backendauth/repository/AuthRepository.kt b/src/main/kotlin/com/hrv/mart/backendauth/repository/AppWriteAuthRepository.kt similarity index 74% rename from src/main/kotlin/com/hrv/mart/backendauth/repository/AuthRepository.kt rename to src/main/kotlin/com/hrv/mart/backendauth/repository/AppWriteAuthRepository.kt index 5504c0f..d4e69c0 100644 --- a/src/main/kotlin/com/hrv/mart/backendauth/repository/AuthRepository.kt +++ b/src/main/kotlin/com/hrv/mart/backendauth/repository/AppWriteAuthRepository.kt @@ -1,6 +1,6 @@ package com.hrv.mart.backendauth.repository -import com.hrv.mart.authlibrary.model.Auth +import com.hrv.mart.authlibrary.model.AppWriteAuth import io.appwrite.Client import io.appwrite.services.Account import kotlinx.coroutines.runBlocking @@ -10,25 +10,28 @@ import reactor.core.publisher.Mono import reactor.kotlin.core.publisher.toMono @Repository -class AuthRepository ( +class AppWriteAuthRepository ( @Autowired private val client: Client ) { - fun getAuthAccount(jwt: String): Mono { + fun getAuthAccount(jwt: String): Mono { client.setJWT(jwt) val account = Account(client) - return runBlocking{account.get()} + return runBlocking{ + account.get() + } .toMono() .map {details -> - Auth( + + AppWriteAuth( name = details.name, email = details.email, + userId = details.id, emailVerification = details.emailVerification, createdAt = details.createdAt, updatedAt = details.updatedAt ) } - } } diff --git a/src/main/kotlin/com/hrv/mart/backendauth/repository/AuthWithUserTypeRepository.kt b/src/main/kotlin/com/hrv/mart/backendauth/repository/AuthWithUserTypeRepository.kt new file mode 100644 index 0000000..a43741e --- /dev/null +++ b/src/main/kotlin/com/hrv/mart/backendauth/repository/AuthWithUserTypeRepository.kt @@ -0,0 +1,12 @@ +package com.hrv.mart.backendauth.repository + +import com.hrv.mart.authlibrary.model.AuthWithUserType +import org.springframework.data.mongodb.repository.ReactiveMongoRepository +import org.springframework.stereotype.Repository +import reactor.core.publisher.Mono + +@Repository +interface AuthWithUserTypeRepository : ReactiveMongoRepository { + fun existsByUserId(userId: String): Mono + fun findByUserId(userId: String): Mono +} \ No newline at end of file diff --git a/src/main/kotlin/com/hrv/mart/backendauth/service/AuthService.kt b/src/main/kotlin/com/hrv/mart/backendauth/service/AuthService.kt index b3a817c..ec8c8f8 100644 --- a/src/main/kotlin/com/hrv/mart/backendauth/service/AuthService.kt +++ b/src/main/kotlin/com/hrv/mart/backendauth/service/AuthService.kt @@ -1,8 +1,9 @@ package com.hrv.mart.backendauth.service -import com.hrv.mart.authlibrary.model.Auth +import com.hrv.mart.authlibrary.model.AuthWithUserType import com.hrv.mart.authlibrary.model.UserType -import com.hrv.mart.backendauth.repository.AuthRepository +import com.hrv.mart.backendauth.repository.AppWriteAuthRepository +import com.hrv.mart.backendauth.repository.AuthWithUserTypeRepository import com.hrv.mart.backendauth.repository.KafkaRepository import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus @@ -13,7 +14,9 @@ import reactor.core.publisher.Mono @Service class AuthService ( @Autowired - private val authRepository: AuthRepository, + private val appWriteAuthRepository: AppWriteAuthRepository, + @Autowired + private val authWithUserTypeRepository: AuthWithUserTypeRepository, @Autowired private val kafkaRepository: KafkaRepository ) @@ -22,9 +25,13 @@ class AuthService ( jwt: String, userType: UserType, response: ServerHttpResponse - ): Mono { - userType.name - return authRepository.getAuthAccount(jwt) + ) = + appWriteAuthRepository + .getAuthAccount(jwt) + .flatMap { + insertUserType(it.userId, userType) + .then(Mono.just(it)) + } .flatMap {auth -> response.statusCode = HttpStatus.OK kafkaRepository @@ -35,5 +42,35 @@ class AuthService ( response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR Mono.empty() } - } + private fun insertUserType(userId: String, userType: UserType) = + authWithUserTypeRepository + .existsByUserId(userId) + .flatMap { + if (it) { + if (userType == UserType.ADMIN) { + authWithUserTypeRepository + .findByUserId(userId) + .flatMap { authWithUserType -> + if (authWithUserType.userType == UserType.ADMIN) { + Mono.empty() + } + else { + Mono.error(Throwable("User do not have required access")) + } + } + } + else { + Mono.empty() + } + } + else { + authWithUserTypeRepository + .insert( + AuthWithUserType( + userId = userId, + userType = UserType.USER + ) + ) + } + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 135cf72..c091657 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,4 +6,4 @@ spring.kafka.producer.value-serializer= org.springframework.kafka.support.serial spring.kafka.consumer.group-id=user hrv.mart.appwrite.endPoint=${APPWRITE_ENDPOINT} hrv.mart.appwrite.projectId=${APPWRITE_PROJECT_ID} -hrv.mart.appwrite.apikey=${APPWRITE_APIKEY} +hrv.mart.appwrite.apikey=${APPWRITE_AUTH_API_KEY} diff --git a/src/test/kotlin/com/hrv/mart/backendauth/controller/AuthControllerTest.kt b/src/test/kotlin/com/hrv/mart/backendauth/controller/AuthControllerTest.kt index 9722803..bee36a2 100644 --- a/src/test/kotlin/com/hrv/mart/backendauth/controller/AuthControllerTest.kt +++ b/src/test/kotlin/com/hrv/mart/backendauth/controller/AuthControllerTest.kt @@ -1,9 +1,11 @@ package com.hrv.mart.backendauth.controller -import com.hrv.mart.authlibrary.model.Auth +import com.hrv.mart.authlibrary.model.AppWriteAuth import com.hrv.mart.authlibrary.model.AuthRequest +import com.hrv.mart.authlibrary.model.AuthWithUserType import com.hrv.mart.authlibrary.model.UserType -import com.hrv.mart.backendauth.repository.AuthRepository +import com.hrv.mart.backendauth.repository.AppWriteAuthRepository +import com.hrv.mart.backendauth.repository.AuthWithUserTypeRepository import com.hrv.mart.backendauth.repository.KafkaRepository import com.hrv.mart.backendauth.service.AuthService import io.appwrite.exceptions.AppwriteException @@ -18,11 +20,16 @@ import reactor.test.StepVerifier import java.util.* class AuthControllerTest { - private val mockAuthRepository = mock(AuthRepository::class.java) + private val mockAppWriteAuthRepository = mock(AppWriteAuthRepository::class.java) private val mockKafkaRepository = mock(KafkaRepository::class.java) + private val mockAuthWithUserTypeRepository = mock(AuthWithUserTypeRepository::class.java) private val response = mock(ServerHttpResponse::class.java) - private val authService = AuthService(mockAuthRepository, mockKafkaRepository) + private val authService = AuthService( + mockAppWriteAuthRepository, + mockAuthWithUserTypeRepository, + mockKafkaRepository + ) private val authController = AuthController(authService) @Test @@ -30,16 +37,27 @@ class AuthControllerTest { val jwt = "A_VALID_JWT" val userType = UserType.USER - val auth = Auth( + val auth = AppWriteAuth( + userId = UUID.randomUUID().toString(), email = "test@test.com", emailVerification = true, createdAt = Date().toString(), updatedAt = Date().toString(), name = "Test User" ) + val authWithUserType = AuthWithUserType( + userId = auth.userId, + userType = userType + ) doReturn(Mono.just(auth)) - .`when`(mockAuthRepository) + .`when`(mockAppWriteAuthRepository) .getAuthAccount(jwt) + doReturn(Mono.just(authWithUserType)) + .`when`(mockAuthWithUserTypeRepository) + .findByUserId(auth.userId) + doReturn(Mono.just(true)) + .`when`(mockAuthWithUserTypeRepository) + .existsByUserId(auth.userId) doReturn(Mono.empty>()) .`when`(mockKafkaRepository) .createUser(auth.toUser()) @@ -61,7 +79,7 @@ class AuthControllerTest { doReturn(Mono.error(AppwriteException("JWT is invalid"))) - .`when`(mockAuthRepository) + .`when`(mockAppWriteAuthRepository) .getAuthAccount(jwt) StepVerifier