Separate views into a maven module
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
package be.simplenotes.domain.security
|
||||
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException
|
||||
|
||||
data class JwtPayload(val userId: Int, val username: String) {
|
||||
constructor(user: PersistedUser) : this(user.id, user.username)
|
||||
}
|
||||
|
||||
class JwtPayloadExtractor(private val jwt: SimpleJwt) {
|
||||
operator fun invoke(token: String): JwtPayload? = try {
|
||||
operator fun invoke(token: String): LoggedInUser? = try {
|
||||
val decodedJWT = jwt.verifier.verify(token)
|
||||
val id = decodedJWT.getClaim(userIdField).asInt() ?: null
|
||||
val username = decodedJWT.getClaim(usernameField).asString() ?: null
|
||||
id?.let { username?.let { JwtPayload(id, username) } }
|
||||
id?.let { username?.let { LoggedInUser(id, username) } }
|
||||
} catch (e: JWTVerificationException) {
|
||||
null
|
||||
} catch (e: IllegalArgumentException) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package be.simplenotes.domain.security
|
||||
|
||||
import be.simplenotes.config.JwtConfig
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.JWTVerifier
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
@@ -15,9 +16,9 @@ class SimpleJwt(jwtConfig: JwtConfig) {
|
||||
private val algorithm = Algorithm.HMAC256(jwtConfig.secret)
|
||||
|
||||
val verifier: JWTVerifier = JWT.require(algorithm).build()
|
||||
fun sign(jwtPayload: JwtPayload): String = JWT.create()
|
||||
.withClaim(userIdField, jwtPayload.userId)
|
||||
.withClaim(usernameField, jwtPayload.username)
|
||||
fun sign(loggedInUser: LoggedInUser): String = JWT.create()
|
||||
.withClaim(userIdField, loggedInUser.userId)
|
||||
.withClaim(usernameField, loggedInUser.username)
|
||||
.withExpiresAt(getExpiration())
|
||||
.sign(algorithm)
|
||||
|
||||
|
||||
+2
-2
@@ -4,11 +4,11 @@ import arrow.core.Either
|
||||
import arrow.core.extensions.fx
|
||||
import arrow.core.filterOrElse
|
||||
import arrow.core.rightIfNotNull
|
||||
import be.simplenotes.domain.security.JwtPayload
|
||||
import be.simplenotes.domain.security.PasswordHash
|
||||
import be.simplenotes.domain.security.SimpleJwt
|
||||
import be.simplenotes.domain.validation.UserValidations
|
||||
import be.simplenotes.persistance.repositories.UserRepository
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
|
||||
internal class LoginUseCaseImpl(
|
||||
private val userRepository: UserRepository,
|
||||
@@ -20,6 +20,6 @@ internal class LoginUseCaseImpl(
|
||||
!userRepository.find(user.username)
|
||||
.rightIfNotNull { Unregistered }
|
||||
.filterOrElse({ passwordHash.verify(form.password!!, it.password) }, { WrongPassword })
|
||||
.map { jwt.sign(JwtPayload(it)) }
|
||||
.map { jwt.sign(LoggedInUser(it)) }
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -2,6 +2,7 @@ package be.simplenotes.domain.security
|
||||
|
||||
import be.simplenotes.domain.usecases.users.login.Token
|
||||
import be.simplenotes.config.JwtConfig
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
import com.natpryce.hamkrest.absent
|
||||
@@ -13,7 +14,7 @@ import org.junit.jupiter.params.provider.MethodSource
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.stream.Stream
|
||||
|
||||
internal class JwtPayloadExtractorTest {
|
||||
internal class LoggedInUserExtractorTest {
|
||||
private val jwtConfig = JwtConfig("a secret", 1, TimeUnit.HOURS)
|
||||
private val simpleJwt = SimpleJwt(jwtConfig)
|
||||
private val jwtPayloadExtractor = JwtPayloadExtractor(simpleJwt)
|
||||
@@ -45,6 +46,6 @@ internal class JwtPayloadExtractorTest {
|
||||
@Test
|
||||
fun `parse valid token`() {
|
||||
val token = createToken(username = "someone", id = 1)
|
||||
assertThat(jwtPayloadExtractor(token), equalTo(JwtPayload(1, "someone")))
|
||||
assertThat(jwtPayloadExtractor(token), equalTo(LoggedInUser(1, "someone")))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user