diff --git a/apps/xsCli/src/main/kotlin/de/itkl/xsCli/App.kt b/apps/xsCli/src/main/kotlin/de/itkl/xsCli/App.kt index bd60254..bf67d48 100644 --- a/apps/xsCli/src/main/kotlin/de/itkl/xsCli/App.kt +++ b/apps/xsCli/src/main/kotlin/de/itkl/xsCli/App.kt @@ -36,6 +36,9 @@ class XsCli : KoinComponent { 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")) } } diff --git a/libraries/httpClient/build.gradle.kts b/libraries/httpClient/build.gradle.kts index 86ef97e..c64ce5f 100644 --- a/libraries/httpClient/build.gradle.kts +++ b/libraries/httpClient/build.gradle.kts @@ -14,4 +14,5 @@ dependencies { implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") implementation("com.akuleshov7:ktoml-core:0.5.1") implementation("com.akuleshov7:ktoml-file:0.5.1") + implementation("com.github.ben-manes.caffeine:caffeine:3.1.8") } \ No newline at end of file diff --git a/libraries/httpClient/src/main/kotlin/de/itkl/httpClient/implementation/SmartCloudAuthStrategy.kt b/libraries/httpClient/src/main/kotlin/de/itkl/httpClient/implementation/SmartCloudAuthStrategy.kt index e371301..6546105 100644 --- a/libraries/httpClient/src/main/kotlin/de/itkl/httpClient/implementation/SmartCloudAuthStrategy.kt +++ b/libraries/httpClient/src/main/kotlin/de/itkl/httpClient/implementation/SmartCloudAuthStrategy.kt @@ -1,5 +1,7 @@ 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 io.github.oshai.kotlinlogging.KotlinLogging import io.ktor.client.* @@ -10,17 +12,31 @@ import io.ktor.http.* import io.ktor.util.* import org.koin.core.component.KoinComponent import org.koin.core.component.inject +import java.util.concurrent.TimeUnit private val Log = KotlinLogging.logger { } class SmartCloudAuthStrategy : AuthStrategy, KoinComponent { private val credentialsProvider: CredentialsProvider by inject() + + // Cache Helpers + private val tokenCache: Cache = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .build() + override suspend fun login(httpClient: HttpClient, request: HttpRequestBuilder): AuthenticationToken? { Log.debug { "Attempting login..." } val user = request.attributes.getOrNull(AttributeKey("username")) ?: run { Log.info { "No username is specified for this request" } 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 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() Log.info { "Login successful. Valid until: ${token.validUntil}" } + + // Cache the token after successful login + tokenCache.put(user, token) + return token } } \ No newline at end of file