keep track of history

This commit is contained in:
gowthaman.b 2024-05-08 14:20:49 +05:30
parent c4985f9690
commit 18afa76d56
2 changed files with 21 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.module.kotlin.readValue
import com.restapi.config.AppConfig.Companion.appConfig import com.restapi.config.AppConfig.Companion.appConfig
import com.restapi.domain.AuthTokenCache import com.restapi.domain.AuthTokenCache
import com.restapi.domain.Plant import com.restapi.domain.Plant
import com.restapi.domain.RefreshHistory
import com.restapi.domain.Session import com.restapi.domain.Session
import com.restapi.domain.Session.database import com.restapi.domain.Session.database
import com.restapi.domain.Session.objectMapper import com.restapi.domain.Session.objectMapper
@ -187,6 +188,7 @@ object Auth {
this.expiresAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong()) this.expiresAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong())
this.refreshToken = atResponse.refreshToken this.refreshToken = atResponse.refreshToken
this.refreshExpiresAt = LocalDateTime.now().plusSeconds(atResponse.refreshExpiresIn.toLong()) this.refreshExpiresAt = LocalDateTime.now().plusSeconds(atResponse.refreshExpiresIn.toLong())
this.refreshHistory = arrayListOf()
}) })
ctx.result(atResponse.accessToken).contentType(ContentType.TEXT_PLAIN) ctx.result(atResponse.accessToken).contentType(ContentType.TEXT_PLAIN)
} }
@ -263,12 +265,20 @@ object Auth {
).header("Content-Type", "application/x-www-form-urlencoded").build() ).header("Content-Type", "application/x-www-form-urlencoded").build()
val message = httpClient.send(req, HttpResponse.BodyHandlers.ofString()).body() val message = httpClient.send(req, HttpResponse.BodyHandlers.ofString()).body()
val atResponse = objectMapper.readValue<AuthTokenResponse>(message) val atResponse = objectMapper.readValue<AuthTokenResponse>(message)
val parsed = validateAuthToken(atResponse.accessToken)
foundOldAt.authToken = atResponse.accessToken foundOldAt.authToken = atResponse.accessToken
foundOldAt.expiresAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong()) foundOldAt.expiresAt = LocalDateTime.now().plusSeconds(atResponse.expiresIn.toLong())
foundOldAt.refreshExpiresAt = LocalDateTime.now().plusSeconds(atResponse.refreshExpiresIn.toLong()) foundOldAt.refreshExpiresAt = LocalDateTime.now().plusSeconds(atResponse.refreshExpiresIn.toLong())
foundOldAt.refreshToken = atResponse.refreshToken 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) database.update(foundOldAt)
ctx.result(atResponse.accessToken).contentType(ContentType.TEXT_PLAIN) ctx.result(atResponse.accessToken).contentType(ContentType.TEXT_PLAIN)

View File

@ -687,6 +687,13 @@ open class Plant : BaseModel() {
var prefixes: MutableMap<String, String>? = mutableMapOf() var prefixes: MutableMap<String, String>? = mutableMapOf()
} }
data class RefreshHistory(
val oldAt: String,
val oldExpiryAt: LocalDateTime,
val newAt: String,
val newExpiryAt: LocalDateTime,
val createdAt: LocalDateTime
)
@Entity @Entity
open class AuthTokenCache : BaseModel() { open class AuthTokenCache : BaseModel() {
@Column(columnDefinition = "text") @Column(columnDefinition = "text")
@ -700,4 +707,7 @@ open class AuthTokenCache : BaseModel() {
var userId: String = "" var userId: String = ""
var expired: Boolean = false var expired: Boolean = false
var loggedOut: Boolean = false var loggedOut: Boolean = false
@DbJsonB
var refreshHistory: MutableList<RefreshHistory>? = arrayListOf()
} }