From 18afa76d5632e49a26b65bffe1a2eca6dd82238d Mon Sep 17 00:00:00 2001 From: "gowthaman.b" Date: Wed, 8 May 2024 14:20:49 +0530 Subject: [PATCH] keep track of history --- src/main/kotlin/com/restapi/config/Auth.kt | 12 +++++++++++- src/main/kotlin/com/restapi/domain/models.kt | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/restapi/config/Auth.kt b/src/main/kotlin/com/restapi/config/Auth.kt index deb7b6c..dbd505b 100644 --- a/src/main/kotlin/com/restapi/config/Auth.kt +++ b/src/main/kotlin/com/restapi/config/Auth.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.module.kotlin.readValue import com.restapi.config.AppConfig.Companion.appConfig import com.restapi.domain.AuthTokenCache import com.restapi.domain.Plant +import com.restapi.domain.RefreshHistory import com.restapi.domain.Session import com.restapi.domain.Session.database import com.restapi.domain.Session.objectMapper @@ -187,6 +188,7 @@ object Auth { this.expiresAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong()) this.refreshToken = atResponse.refreshToken this.refreshExpiresAt = LocalDateTime.now().plusSeconds(atResponse.refreshExpiresIn.toLong()) + this.refreshHistory = arrayListOf() }) ctx.result(atResponse.accessToken).contentType(ContentType.TEXT_PLAIN) } @@ -263,12 +265,20 @@ object Auth { ).header("Content-Type", "application/x-www-form-urlencoded").build() val message = httpClient.send(req, HttpResponse.BodyHandlers.ofString()).body() val atResponse = objectMapper.readValue(message) - val parsed = validateAuthToken(atResponse.accessToken) foundOldAt.authToken = atResponse.accessToken foundOldAt.expiresAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong()) foundOldAt.refreshExpiresAt = LocalDateTime.now().plusSeconds(atResponse.refreshExpiresIn.toLong()) foundOldAt.refreshToken = atResponse.refreshToken + foundOldAt.refreshHistory = (foundOldAt.refreshHistory ?: arrayListOf()).apply { + add(RefreshHistory( + oldAt = authUser.token, + oldExpiryAt = expiresAt, + newAt = atResponse.accessToken, + newExpiryAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong()), + createdAt = LocalDateTime.now() + )) + } database.update(foundOldAt) ctx.result(atResponse.accessToken).contentType(ContentType.TEXT_PLAIN) diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 3a4aa87..f7d1e3d 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -687,6 +687,13 @@ open class Plant : BaseModel() { var prefixes: MutableMap? = mutableMapOf() } +data class RefreshHistory( + val oldAt: String, + val oldExpiryAt: LocalDateTime, + val newAt: String, + val newExpiryAt: LocalDateTime, + val createdAt: LocalDateTime +) @Entity open class AuthTokenCache : BaseModel() { @Column(columnDefinition = "text") @@ -700,4 +707,7 @@ open class AuthTokenCache : BaseModel() { var userId: String = "" var expired: Boolean = false var loggedOut: Boolean = false + + @DbJsonB + var refreshHistory: MutableList? = arrayListOf() } \ No newline at end of file