feat: refresh token when page is reloaded
This commit is contained in:
@@ -59,7 +59,7 @@ fun Payload.mid() = getClaim("id").asInt()
|
||||
|
||||
|
||||
object Security {
|
||||
fun DEFAULT_EXPIRY() = Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 14);
|
||||
fun DEFAULT_EXPIRY() = Date(System.currentTimeMillis() + 1000L * 60 * 60 * 24 * 40);
|
||||
|
||||
suspend fun authenticateUser(application: Application, username: String, password: String): Ministrant? {
|
||||
println("Username $username password $password")
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.walamana.views
|
||||
import at.favre.lib.crypto.bcrypt.BCrypt
|
||||
import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
import de.walamana.models.Ministrant
|
||||
import de.walamana.models.MinistrantenDao
|
||||
import de.walamana.plugins.Security
|
||||
import de.walamana.plugins.getJWTEnvironment
|
||||
@@ -98,6 +99,28 @@ fun Route.configureAuthenticationRoutes() {
|
||||
|
||||
call.respond(hashMapOf("password" to newPassword))
|
||||
}
|
||||
post("refresh") {
|
||||
val principal = call.principal<JWTPrincipal>()!!
|
||||
val jwtEnv = application.getJWTEnvironment()
|
||||
val username = principal.payload.username()
|
||||
|
||||
val ministrant = if (username == "admin") {
|
||||
val allMinis = MinistrantenDao.allMinistranten().map { it.username }
|
||||
Ministrant(0, "admin", "", "admin", "admin", null, allMinis)
|
||||
} else {
|
||||
MinistrantenDao.getMinistrant(username) ?: return@post
|
||||
}
|
||||
|
||||
val newToken = Security.createToken(jwtEnv, ministrant)
|
||||
val expiry = Security.DEFAULT_EXPIRY().toGMTString()
|
||||
|
||||
call.response.header(
|
||||
"Set-Cookie",
|
||||
"token=$newToken; HttpOnly; Expires=$expiry"
|
||||
)
|
||||
|
||||
call.respond(AuthenticationResult(true, newToken, ministrant.privileges))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user