cache the token
parent
f645477ded
commit
0e1ae654a7
|
|
@ -36,6 +36,9 @@ class XsCli : KoinComponent {
|
||||||
|
|
||||||
suspend fun run() {
|
suspend fun run() {
|
||||||
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
|
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
|
||||||
|
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
|
||||||
|
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
|
||||||
|
xsClient.analyse(Paths.get("assets/xs-reg/00001.jpg"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,4 +14,5 @@ dependencies {
|
||||||
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
|
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
|
||||||
implementation("com.akuleshov7:ktoml-core:0.5.1")
|
implementation("com.akuleshov7:ktoml-core:0.5.1")
|
||||||
implementation("com.akuleshov7:ktoml-file:0.5.1")
|
implementation("com.akuleshov7:ktoml-file:0.5.1")
|
||||||
|
implementation("com.github.ben-manes.caffeine:caffeine:3.1.8")
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package de.itkl.httpClient.implementation
|
package de.itkl.httpClient.implementation
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache
|
||||||
import de.itkl.httpClient.auth.*
|
import de.itkl.httpClient.auth.*
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
|
|
@ -10,17 +12,31 @@ import io.ktor.http.*
|
||||||
import io.ktor.util.*
|
import io.ktor.util.*
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
private val Log = KotlinLogging.logger { }
|
private val Log = KotlinLogging.logger { }
|
||||||
|
|
||||||
class SmartCloudAuthStrategy : AuthStrategy, KoinComponent {
|
class SmartCloudAuthStrategy : AuthStrategy, KoinComponent {
|
||||||
private val credentialsProvider: CredentialsProvider by inject()
|
private val credentialsProvider: CredentialsProvider by inject()
|
||||||
|
|
||||||
|
// Cache Helpers
|
||||||
|
private val tokenCache: Cache<String, AuthenticationToken> = Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(1, TimeUnit.HOURS)
|
||||||
|
.build()
|
||||||
|
|
||||||
override suspend fun login(httpClient: HttpClient, request: HttpRequestBuilder): AuthenticationToken? {
|
override suspend fun login(httpClient: HttpClient, request: HttpRequestBuilder): AuthenticationToken? {
|
||||||
Log.debug { "Attempting login..." }
|
Log.debug { "Attempting login..." }
|
||||||
val user = request.attributes.getOrNull(AttributeKey<String>("username")) ?: run {
|
val user = request.attributes.getOrNull(AttributeKey<String>("username")) ?: run {
|
||||||
Log.info { "No username is specified for this request" }
|
Log.info { "No username is specified for this request" }
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return cached token if present
|
||||||
|
tokenCache.getIfPresent(user)?.let {
|
||||||
|
Log.info { "Returning cached token for user: $user" }
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
|
||||||
val credentials: Credentials = credentialsProvider.lookupByUsername(user) ?: error("No credentials found for user: $user")
|
val credentials: Credentials = credentialsProvider.lookupByUsername(user) ?: error("No credentials found for user: $user")
|
||||||
val loginAndPassword: Credentials.LoginAndPassword = credentials as? Credentials.LoginAndPassword ?: error("Only username and password is supported by smartcloud auth login")
|
val loginAndPassword: Credentials.LoginAndPassword = credentials as? Credentials.LoginAndPassword ?: error("Only username and password is supported by smartcloud auth login")
|
||||||
|
|
||||||
|
|
@ -36,6 +52,10 @@ class SmartCloudAuthStrategy : AuthStrategy, KoinComponent {
|
||||||
}
|
}
|
||||||
val token = response.body<BearerToken>()
|
val token = response.body<BearerToken>()
|
||||||
Log.info { "Login successful. Valid until: ${token.validUntil}" }
|
Log.info { "Login successful. Valid until: ${token.validUntil}" }
|
||||||
|
|
||||||
|
// Cache the token after successful login
|
||||||
|
tokenCache.put(user, token)
|
||||||
|
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue