rename user verifier
This commit is contained in:
+3
-3
@@ -7,7 +7,7 @@ import ru.shadowsparky.backend.data.StringFetcher
|
|||||||
import ru.shadowsparky.backend.data.StringResource
|
import ru.shadowsparky.backend.data.StringResource
|
||||||
import ru.shadowsparky.backend.data.generateRefreshToken
|
import ru.shadowsparky.backend.data.generateRefreshToken
|
||||||
import ru.shadowsparky.backend.data.toTokenHash
|
import ru.shadowsparky.backend.data.toTokenHash
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.domain.DispatcherProvider
|
import ru.shadowsparky.domain.DispatcherProvider
|
||||||
import ru.shadowsparky.gigawear.backend.AppDatabase
|
import ru.shadowsparky.gigawear.backend.AppDatabase
|
||||||
import ru.shadowsparky.gigawear.common.AuthRepository
|
import ru.shadowsparky.gigawear.common.AuthRepository
|
||||||
@@ -23,7 +23,7 @@ import kotlin.time.toJavaInstant
|
|||||||
class BackendAuthRepositoryImpl(
|
class BackendAuthRepositoryImpl(
|
||||||
appDatabase: AppDatabase,
|
appDatabase: AppDatabase,
|
||||||
private val stringFetcher: StringFetcher,
|
private val stringFetcher: StringFetcher,
|
||||||
private val loginVerifier: LoginVerifier,
|
private val userVerifier: UserVerifier,
|
||||||
private val jwtPreparer: JwtPreparer,
|
private val jwtPreparer: JwtPreparer,
|
||||||
private val dispatcherProvider: DispatcherProvider
|
private val dispatcherProvider: DispatcherProvider
|
||||||
) : AuthRepository {
|
) : AuthRepository {
|
||||||
@@ -34,7 +34,7 @@ class BackendAuthRepositoryImpl(
|
|||||||
val userCode = request.code
|
val userCode = request.code
|
||||||
val user = usersQueries.selectByCode(userCode).executeAsOneOrNull()
|
val user = usersQueries.selectByCode(userCode).executeAsOneOrNull()
|
||||||
?: throw BadRequestException(stringFetcher.get(StringResource.UNKNOWN_USER))
|
?: throw BadRequestException(stringFetcher.get(StringResource.UNKNOWN_USER))
|
||||||
loginVerifier.verify(user.user_id)
|
userVerifier.verify(user.user_id)
|
||||||
create(user.user_id)
|
create(user.user_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -4,17 +4,17 @@ import kotlinx.coroutines.withContext
|
|||||||
import org.koin.core.annotation.Factory
|
import org.koin.core.annotation.Factory
|
||||||
import ru.shadowsparky.backend.data.StringFetcher
|
import ru.shadowsparky.backend.data.StringFetcher
|
||||||
import ru.shadowsparky.backend.data.StringResource
|
import ru.shadowsparky.backend.data.StringResource
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.domain.DispatcherProvider
|
import ru.shadowsparky.domain.DispatcherProvider
|
||||||
import ru.shadowsparky.gigawear.backend.AppDatabase
|
import ru.shadowsparky.gigawear.backend.AppDatabase
|
||||||
import ru.shadowsparky.http.domain.BadRequestException
|
import ru.shadowsparky.http.domain.BadRequestException
|
||||||
|
|
||||||
@Factory
|
@Factory
|
||||||
class LoginVerifierImpl(
|
class UserVerifierImpl(
|
||||||
private val appDatabase: AppDatabase,
|
private val appDatabase: AppDatabase,
|
||||||
private val dispatcherProvider: DispatcherProvider,
|
private val dispatcherProvider: DispatcherProvider,
|
||||||
private val stringFetcher: StringFetcher
|
private val stringFetcher: StringFetcher
|
||||||
) : LoginVerifier {
|
) : UserVerifier {
|
||||||
|
|
||||||
override suspend fun verify(userId: Long) {
|
override suspend fun verify(userId: Long) {
|
||||||
withContext(dispatcherProvider.io) {
|
withContext(dispatcherProvider.io) {
|
||||||
+5
-5
@@ -7,7 +7,7 @@ import ru.shadowsparky.backend.data.JwtPreparer
|
|||||||
import ru.shadowsparky.backend.data.StringFetcher
|
import ru.shadowsparky.backend.data.StringFetcher
|
||||||
import ru.shadowsparky.backend.data.StringResource
|
import ru.shadowsparky.backend.data.StringResource
|
||||||
import ru.shadowsparky.backend.data.toTokenHash
|
import ru.shadowsparky.backend.data.toTokenHash
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.domain.Log
|
import ru.shadowsparky.domain.Log
|
||||||
import ru.shadowsparky.http.domain.BadRequestException
|
import ru.shadowsparky.http.domain.BadRequestException
|
||||||
import ru.shadowsparky.http.domain.TokenInfo
|
import ru.shadowsparky.http.domain.TokenInfo
|
||||||
@@ -26,7 +26,7 @@ class AuthTokenRepositoryFactory(
|
|||||||
private val db: AppDatabase,
|
private val db: AppDatabase,
|
||||||
private val dispatcherProvider: DispatcherProvider,
|
private val dispatcherProvider: DispatcherProvider,
|
||||||
private val jwtPreparer: JwtPreparer,
|
private val jwtPreparer: JwtPreparer,
|
||||||
private val loginVerifier: LoginVerifier,
|
private val userVerifier: UserVerifier,
|
||||||
private val log: Log,
|
private val log: Log,
|
||||||
private val stringFetcher: StringFetcher,
|
private val stringFetcher: StringFetcher,
|
||||||
private val envFetcher: EnvFetcher,
|
private val envFetcher: EnvFetcher,
|
||||||
@@ -38,7 +38,7 @@ class AuthTokenRepositoryFactory(
|
|||||||
db,
|
db,
|
||||||
dispatcherProvider,
|
dispatcherProvider,
|
||||||
jwtPreparer,
|
jwtPreparer,
|
||||||
loginVerifier,
|
userVerifier,
|
||||||
userId,
|
userId,
|
||||||
log,
|
log,
|
||||||
stringFetcher,
|
stringFetcher,
|
||||||
@@ -52,7 +52,7 @@ class BackendAuthTokenRepository(
|
|||||||
private val db: AppDatabase,
|
private val db: AppDatabase,
|
||||||
private val dispatcherProvider: DispatcherProvider,
|
private val dispatcherProvider: DispatcherProvider,
|
||||||
private val jwtPreparer: JwtPreparer,
|
private val jwtPreparer: JwtPreparer,
|
||||||
private val loginVerifier: LoginVerifier,
|
private val userVerifier: UserVerifier,
|
||||||
private val userId: Long,
|
private val userId: Long,
|
||||||
private val log: Log,
|
private val log: Log,
|
||||||
private val stringFetcher: StringFetcher,
|
private val stringFetcher: StringFetcher,
|
||||||
@@ -132,7 +132,7 @@ class BackendAuthTokenRepository(
|
|||||||
private suspend fun create(login: String): TokenInfo = withContext(dispatcherProvider.io) {
|
private suspend fun create(login: String): TokenInfo = withContext(dispatcherProvider.io) {
|
||||||
val userInfo = userInfoCache.query(login)
|
val userInfo = userInfoCache.query(login)
|
||||||
?: throw BadRequestException(stringFetcher.get(StringResource.UNKNOWN_USER))
|
?: throw BadRequestException(stringFetcher.get(StringResource.UNKNOWN_USER))
|
||||||
loginVerifier.verify(userInfo.userId)
|
userVerifier.verify(userInfo.userId)
|
||||||
val rsp = jwtPreparer.prepare(userInfo.userId)
|
val rsp = jwtPreparer.prepare(userInfo.userId)
|
||||||
val refresh = ByteArray(32)
|
val refresh = ByteArray(32)
|
||||||
random.nextBytes(refresh)
|
random.nextBytes(refresh)
|
||||||
|
|||||||
+3
-3
@@ -3,15 +3,15 @@ package ru.shadowsparky.vbox.backend.data.auth
|
|||||||
import org.koin.core.annotation.Factory
|
import org.koin.core.annotation.Factory
|
||||||
import ru.shadowsparky.backend.data.StringFetcher
|
import ru.shadowsparky.backend.data.StringFetcher
|
||||||
import ru.shadowsparky.backend.data.StringResource
|
import ru.shadowsparky.backend.data.StringResource
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.backend.domain.VerifyTokenException
|
import ru.shadowsparky.backend.domain.VerifyTokenException
|
||||||
import ru.shadowsparky.vbox.shared.domain.FLAG_USER_BLOCKED
|
import ru.shadowsparky.vbox.shared.domain.FLAG_USER_BLOCKED
|
||||||
|
|
||||||
@Factory
|
@Factory
|
||||||
class BackendLoginVerifier(
|
class BackendUserVerifier(
|
||||||
private val stringFetcher: StringFetcher,
|
private val stringFetcher: StringFetcher,
|
||||||
private val userInfoCache: UserInfoCache
|
private val userInfoCache: UserInfoCache
|
||||||
) : LoginVerifier {
|
) : UserVerifier {
|
||||||
override suspend fun verify(userId: Long) {
|
override suspend fun verify(userId: Long) {
|
||||||
val user = userInfoCache.query(userId)
|
val user = userInfoCache.query(userId)
|
||||||
?: throw VerifyTokenException(stringFetcher.get(StringResource.UNKNOWN_USER))
|
?: throw VerifyTokenException(stringFetcher.get(StringResource.UNKNOWN_USER))
|
||||||
@@ -5,7 +5,7 @@ import org.koin.core.annotation.Single
|
|||||||
import ru.shadowsparky.backend.data.TokenVerifier
|
import ru.shadowsparky.backend.data.TokenVerifier
|
||||||
import ru.shadowsparky.backend.data.event.SessionRegistry
|
import ru.shadowsparky.backend.data.event.SessionRegistry
|
||||||
import ru.shadowsparky.backend.domain.JwtInfo
|
import ru.shadowsparky.backend.domain.JwtInfo
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.backend.domain.RemoteEventHandler
|
import ru.shadowsparky.backend.domain.RemoteEventHandler
|
||||||
import ru.shadowsparky.chat.backend.domain.ProcessUserMessageUseCase
|
import ru.shadowsparky.chat.backend.domain.ProcessUserMessageUseCase
|
||||||
import ru.shadowsparky.updater.backend.data.BackendUpdateFetcherFactory
|
import ru.shadowsparky.updater.backend.data.BackendUpdateFetcherFactory
|
||||||
@@ -41,7 +41,7 @@ class WebSocketEntryPoint(
|
|||||||
|
|
||||||
@Single
|
@Single
|
||||||
class AuthEntryPoint(
|
class AuthEntryPoint(
|
||||||
val loginVerifier: LoginVerifier,
|
val userVerifier: UserVerifier,
|
||||||
val authTokenRepositoryFactory: AuthTokenRepositoryFactory,
|
val authTokenRepositoryFactory: AuthTokenRepositoryFactory,
|
||||||
val jwtInfo: JwtInfo,
|
val jwtInfo: JwtInfo,
|
||||||
val tokenVerifier: TokenVerifier
|
val tokenVerifier: TokenVerifier
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import ru.shadowsparky.backend.presentation.obtainUserId
|
|||||||
import ru.shadowsparky.vbox.backend.di.AuthEntryPoint
|
import ru.shadowsparky.vbox.backend.di.AuthEntryPoint
|
||||||
|
|
||||||
fun Application.configureJwt(authEntryPoint: AuthEntryPoint) = with(authEntryPoint) {
|
fun Application.configureJwt(authEntryPoint: AuthEntryPoint) = with(authEntryPoint) {
|
||||||
configureJwt(jwtInfo, tokenVerifier, loginVerifier)
|
configureJwt(jwtInfo, tokenVerifier, userVerifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ApplicationCall.obtainUserId(): Long {
|
fun ApplicationCall.obtainUserId(): Long {
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import com.auth0.jwt.algorithms.Algorithm
|
|||||||
import com.auth0.jwt.interfaces.DecodedJWT
|
import com.auth0.jwt.interfaces.DecodedJWT
|
||||||
import org.koin.core.annotation.Factory
|
import org.koin.core.annotation.Factory
|
||||||
import ru.shadowsparky.backend.domain.JwtInfo
|
import ru.shadowsparky.backend.domain.JwtInfo
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.backend.domain.USER_ID_ARG
|
import ru.shadowsparky.backend.domain.USER_ID_ARG
|
||||||
|
|
||||||
@Factory
|
@Factory
|
||||||
class TokenVerifier(
|
class TokenVerifier(
|
||||||
private val jwtInfo: JwtInfo,
|
private val jwtInfo: JwtInfo,
|
||||||
private val loginVerifier: LoginVerifier
|
private val userVerifier: UserVerifier
|
||||||
) {
|
) {
|
||||||
|
|
||||||
val verifier: JWTVerifier = JWT.require(Algorithm.HMAC512(jwtInfo.secret))
|
val verifier: JWTVerifier = JWT.require(Algorithm.HMAC512(jwtInfo.secret))
|
||||||
@@ -22,7 +22,7 @@ class TokenVerifier(
|
|||||||
|
|
||||||
suspend fun verify(token: String): DecodedJWT {
|
suspend fun verify(token: String): DecodedJWT {
|
||||||
val jwt = verifier.verify(token)
|
val jwt = verifier.verify(token)
|
||||||
loginVerifier.verify(jwt.getClaim(USER_ID_ARG).asLong())
|
userVerifier.verify(jwt.getClaim(USER_ID_ARG).asLong())
|
||||||
return jwt
|
return jwt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
package ru.shadowsparky.backend.domain
|
package ru.shadowsparky.backend.domain
|
||||||
|
|
||||||
interface LoginVerifier {
|
interface UserVerifier {
|
||||||
suspend fun verify(userId: Long)
|
suspend fun verify(userId: Long)
|
||||||
}
|
}
|
||||||
|
|
||||||
+3
-3
@@ -16,7 +16,7 @@ import ru.shadowsparky.backend.data.TokenVerifier
|
|||||||
import ru.shadowsparky.backend.domain.ExceptionInfo
|
import ru.shadowsparky.backend.domain.ExceptionInfo
|
||||||
import ru.shadowsparky.backend.domain.INVALID_TOKEN_MSG
|
import ru.shadowsparky.backend.domain.INVALID_TOKEN_MSG
|
||||||
import ru.shadowsparky.backend.domain.JwtInfo
|
import ru.shadowsparky.backend.domain.JwtInfo
|
||||||
import ru.shadowsparky.backend.domain.LoginVerifier
|
import ru.shadowsparky.backend.domain.UserVerifier
|
||||||
import ru.shadowsparky.backend.domain.USER_ID_ARG
|
import ru.shadowsparky.backend.domain.USER_ID_ARG
|
||||||
import ru.shadowsparky.backend.domain.VerifyTokenException
|
import ru.shadowsparky.backend.domain.VerifyTokenException
|
||||||
import ru.shadowsparky.http.domain.HttpException
|
import ru.shadowsparky.http.domain.HttpException
|
||||||
@@ -36,7 +36,7 @@ fun ApplicationCall.obtainUserId(): Long {
|
|||||||
fun Application.configureJwt(
|
fun Application.configureJwt(
|
||||||
jwtInfo: JwtInfo,
|
jwtInfo: JwtInfo,
|
||||||
tokenVerifier: TokenVerifier,
|
tokenVerifier: TokenVerifier,
|
||||||
loginVerifier: LoginVerifier
|
userVerifier: UserVerifier
|
||||||
) {
|
) {
|
||||||
install(Authentication) {
|
install(Authentication) {
|
||||||
jwt(AUTH_JWT_NAME) {
|
jwt(AUTH_JWT_NAME) {
|
||||||
@@ -46,7 +46,7 @@ fun Application.configureJwt(
|
|||||||
val userId = credential.payload.getClaim(USER_ID_ARG).asLong()
|
val userId = credential.payload.getClaim(USER_ID_ARG).asLong()
|
||||||
try {
|
try {
|
||||||
if (userId != null) {
|
if (userId != null) {
|
||||||
loginVerifier.verify(userId)
|
userVerifier.verify(userId)
|
||||||
if (credential.payload.expiresAt == null) {
|
if (credential.payload.expiresAt == null) {
|
||||||
throw VerifyTokenException("Static tokens not supported!")
|
throw VerifyTokenException("Static tokens not supported!")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user