refresh token
This commit is contained in:
@@ -13,6 +13,7 @@ import com.restapi.controllers.Entities
|
||||
import com.restapi.domain.DataNotFoundException
|
||||
import com.restapi.domain.Session
|
||||
import com.restapi.domain.Session.currentTenant
|
||||
import com.restapi.domain.Session.currentToken
|
||||
import com.restapi.domain.Session.currentUser
|
||||
import com.restapi.domain.Session.objectMapper
|
||||
import com.restapi.domain.Session.redis
|
||||
@@ -93,7 +94,7 @@ fun main(args: Array<String>) {
|
||||
val iamClient = it.queryParam("client") ?: appConfig.iamClient()
|
||||
|
||||
val ep = getAuthEndpoint().tokenEndpoint
|
||||
val client = HttpClient.newHttpClient()
|
||||
val httpClient = HttpClient.newHttpClient()
|
||||
val req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(ep))
|
||||
.POST(
|
||||
@@ -110,7 +111,7 @@ fun main(args: Array<String>) {
|
||||
)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.build()
|
||||
val message = client.send(req, BodyHandlers.ofString()).body()
|
||||
val message = httpClient.send(req, BodyHandlers.ofString()).body()
|
||||
val atResponse = objectMapper.readValue<AuthTokenResponse>(message)
|
||||
val parsed = validateAuthToken(atResponse.accessToken)
|
||||
|
||||
@@ -125,6 +126,71 @@ fun main(args: Array<String>) {
|
||||
Session.jwk()
|
||||
)
|
||||
}
|
||||
|
||||
post("/refresh") { ctx ->
|
||||
//refresh authToken
|
||||
val authToken = ctx.header("Authorization")
|
||||
?.replace("Bearer ", "")
|
||||
?.replace("Bearer: ", "")
|
||||
?.trim() ?: throw UnauthorizedResponse()
|
||||
|
||||
val authUser = validateAuthToken(authToken, skipValidate = true)
|
||||
val client = ctx.queryParam("client") ?: throw BadRequestResponse("client not sent")
|
||||
val redirectUri = ctx.queryParam("redirectUri") ?: throw BadRequestResponse("redirectUri not sent")
|
||||
|
||||
val key = "AUTH_TOKEN_${authUser.userName}"
|
||||
val found = redis.llen(key)
|
||||
val foundOldAt = (0..found)
|
||||
.mapNotNull { redis.lindex(key, it) }
|
||||
.map { objectMapper.readValue<AuthTokenResponse>(it) }
|
||||
.firstOrNull { it.accessToken == authToken } ?: throw BadRequestResponse("authToken not found in cache")
|
||||
|
||||
val expiresAt = foundOldAt.createdAt.plusSeconds(foundOldAt.expiresIn + 0L)
|
||||
val rtExpiresAt = foundOldAt.createdAt.plusSeconds(foundOldAt.refreshExpiresIn + 0L)
|
||||
|
||||
val now = LocalDateTime.now()
|
||||
|
||||
//we can refresh if at is expired, but we still have time for refresh
|
||||
if (expiresAt.isBefore(now) && now.isBefore(rtExpiresAt)) {
|
||||
logger.warn("We can refresh the token for ${authUser.userName}, expires = $expiresAt, refresh Till = $rtExpiresAt")
|
||||
val ep = getAuthEndpoint().tokenEndpoint
|
||||
val httpClient = HttpClient.newHttpClient()
|
||||
val req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(ep))
|
||||
.POST(
|
||||
BodyPublishers.ofString(
|
||||
getFormDataAsString(
|
||||
mapOf(
|
||||
"refresh_token" to foundOldAt.refreshToken,
|
||||
"redirect_uri" to redirectUri,
|
||||
"client_id" to client,
|
||||
"grant_type" to "refresh_token",
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.build()
|
||||
val message = httpClient.send(req, BodyHandlers.ofString()).body()
|
||||
val atResponse = objectMapper.readValue<AuthTokenResponse>(message)
|
||||
val parsed = validateAuthToken(atResponse.accessToken)
|
||||
|
||||
//keep track of this
|
||||
redis.rpush("AUTH_TOKEN_${parsed.userName}", message)
|
||||
|
||||
ctx.json(atResponse)
|
||||
} else {
|
||||
//at is still valid
|
||||
if (expiresAt.isAfter(now)) {
|
||||
logger.warn("Still valid, the token for ${authUser.userName}")
|
||||
ctx.json(foundOldAt)
|
||||
} else {
|
||||
//we have exceeded the refresh time, so we shall ask the user to login again
|
||||
logger.warn("We can't refresh the token for ${authUser.userName}, as refresh-time is expired")
|
||||
throw UnauthorizedResponse()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
before("/api/*") { ctx ->
|
||||
@@ -166,7 +232,9 @@ fun main(args: Array<String>) {
|
||||
path("/api") {
|
||||
post("/audit/{action}") {
|
||||
logger.warn("User ${currentUser()} of tenant ${currentTenant()} has performed ${it.pathParam("action")} @ ${LocalDateTime.now()}")
|
||||
it.json(mapOf("status" to true))
|
||||
}
|
||||
|
||||
post("/script/database/{name}", Entities::executeStoredProcedure, Roles(adminRole, Role.DbOps))
|
||||
post("/script/{file}/{name}", Entities::executeScript, Roles(adminRole, Role.DbOps))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user