Skip to content

Commit

Permalink
Add authorization roles (#61)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Harsh3305 and dependabot[bot] authored Jul 24, 2023
1 parent 8075b55 commit 1f7fbf8
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Auth> {
fun getAuthAccount(jwt: String): Mono<AppWriteAuth> {
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
)
}

}
}
Original file line number Diff line number Diff line change
@@ -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<AuthWithUserType, String> {
fun existsByUserId(userId: String): Mono<Boolean>
fun findByUserId(userId: String): Mono<AuthWithUserType>
}
51 changes: 44 additions & 7 deletions src/main/kotlin/com/hrv/mart/backendauth/service/AuthService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
Expand All @@ -22,9 +25,13 @@ class AuthService (
jwt: String,
userType: UserType,
response: ServerHttpResponse
): Mono<Auth> {
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
Expand All @@ -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
)
)
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,28 +20,44 @@ 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
fun `should return login successful message if jwt is valid`(): Unit = runBlocking {
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<SenderResult<Void>>())
.`when`(mockKafkaRepository)
.createUser(auth.toUser())
Expand All @@ -61,7 +79,7 @@ class AuthControllerTest {


doReturn(Mono.error<AppwriteException>(AppwriteException("JWT is invalid")))
.`when`(mockAuthRepository)
.`when`(mockAppWriteAuthRepository)
.getAuthAccount(jwt)

StepVerifier
Expand Down

0 comments on commit 1f7fbf8

Please sign in to comment.