Compare commits

...

No commits in common. "4cdb074ff555cbaad6430c80f23df5dd8cbbf197" and "c1c05276a2e9becb1cac51a1b21a42e731c36a19" have entirely different histories.

3 changed files with 4 additions and 10 deletions

View File

@ -5,14 +5,11 @@ import be.simplenotes.UserId
import be.simplenotes.public_.tables.Users.USERS
import be.simplenotes.security.PasswordHash
import com.github.benmanes.caffeine.cache.Caffeine
import de.mkammerer.argon2.Argon2Factory
import org.jooq.DSLContext
import org.jooq.Record
import org.jooq.exception.DataAccessException
class UserRepository(private val db: DSLContext, private val passwordHash: PasswordHash) {
private val argon2 = Argon2Factory.create()
private val cache = Caffeine.newBuilder()
.maximumSize(10)
.build<UserId, User>()
@ -31,7 +28,7 @@ class UserRepository(private val db: DSLContext, private val passwordHash: Passw
fun find(username: String, password: String): User? {
val user = db.fetchOne(USERS, USERS.USERNAME.eq(username))?.map(::userMapper) ?: return null
return if (argon2.verify(user.password, password.encodeToByteArray()))
return if (passwordHash.verify(user.password, password))
user
else null
}

View File

@ -4,11 +4,11 @@ import de.mkammerer.argon2.Argon2Factory
interface PasswordHash {
fun hash(password: String): String
fun verify(password: String, hash: String): Boolean
fun verify(hash: String, password: String): Boolean
}
class Argon2PasswordHash : PasswordHash {
private val argon2 = Argon2Factory.create()
override fun hash(password: String): String = argon2.hash(10, 65536 / 2, 1, password.encodeToByteArray())
override fun verify(password: String, hash: String) = argon2.verify(password, password.encodeToByteArray())
override fun verify(hash: String, password: String) = argon2.verify(hash, password.encodeToByteArray())
}

View File

@ -14,14 +14,11 @@ class SimpleJwt(secret: String, validity: Long, timeUnit: TimeUnit) {
private val idClaim = "id"
private val verifier: JWTVerifier = JWT.require(algorithm).build()
fun sign(id: UserId): String {
return JWT.create()
fun sign(id: UserId): String = JWT.create()
.withClaim(idClaim, id.value)
.withExpiresAt(getExpiration())
.sign(algorithm)
}
fun extract(token: String): UserId? = try {
val decodedJWT = verifier.verify(token)