Skip to content

Commit

Permalink
Fjerner generic på token og logger innhold i cache.
Browse files Browse the repository at this point in the history
Co-authored-by: Robin Tordly <[email protected]>
Co-authored-by: Halvor Grizzly Bjørn <[email protected]>
Co-authored-by: Vetle Hollund <[email protected]>
  • Loading branch information
4 people committed Jan 26, 2024
1 parent 40b6aac commit 5d4fd70
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ class AzureAdTokenProvider(
"client_id=${config.clientId}&client_secret=${config.clientSecret}&assertion=$accessToken&scope=$scope&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&requested_token_use=on_behalf_of"
}

private val cache = TokenCache<String>()
private val cache = TokenCache()

private suspend fun getAccessToken(cacheKey: String, body: () -> String): String {
cache.logg(secureLog)
val token = cache.get(cacheKey)
?: client.post(config.tokenEndpoint) {
accept(ContentType.Application.Json)
Expand Down
22 changes: 16 additions & 6 deletions ktor-auth-azuread/main/no/nav/aap/ktor/client/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package no.nav.aap.ktor.client

import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.slf4j.Logger
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap

internal data class Token(val expires_in: Long, val access_token: String) {
private val expiry: Instant = Instant.now().plusSeconds(expires_in - LEEWAY_SECONDS)
Expand All @@ -13,19 +13,29 @@ internal data class Token(val expires_in: Long, val access_token: String) {
private companion object {
const val LEEWAY_SECONDS = 60
}

override fun toString(): String {
return "($expires_in, $access_token)"
}
}

internal class TokenCache<K> {
private val tokens: HashMap<K, Token> = hashMapOf()
internal class TokenCache {
private val tokens: HashMap<String, Token> = hashMapOf()
private val mutex = Mutex()

internal suspend fun add(key: K, token: Token) {
internal suspend fun logg(logger: Logger) {
tokens.forEach { (key, value) ->
logger.info("Key: $key, Value: $value")
}
}

internal suspend fun add(key: String, token: Token) {
mutex.withLock {
tokens[key] = token
}
}

internal suspend fun get(key: K): Token? {
internal suspend fun get(key: String): Token? {
mutex.withLock {
tokens[key]
}?.let {
Expand All @@ -39,7 +49,7 @@ internal class TokenCache<K> {
}
}

private suspend fun rm(key: K) {
private suspend fun rm(key: String) {
mutex.withLock {
tokens.remove(key)
}
Expand Down
26 changes: 20 additions & 6 deletions ktor-auth-tokenx/main/no/nav/aap/ktor/client/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.aap.ktor.client

import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.slf4j.Logger
import java.time.Instant

internal data class Token(val expires_in: Long, val access_token: String) {
Expand All @@ -12,30 +13,43 @@ internal data class Token(val expires_in: Long, val access_token: String) {
private companion object {
const val LEEWAY_SECONDS = 60
}

override fun toString(): String {
return "($expires_in, $access_token)"
}
}

internal class TokenCache<K> {
private val tokens: HashMap<K, Token> = hashMapOf()
internal class TokenCache {
private val tokens: HashMap<String, Token> = hashMapOf()
private val mutex = Mutex()

internal suspend fun add(key: K, token: Token) {
internal suspend fun logg(logger: Logger) {
tokens.forEach { (key, value) ->
logger.info("Key: $key, Value: $value")
}
}

internal suspend fun add(key: String, token: Token) {
mutex.withLock {
tokens[key] = token
}
}

internal suspend fun get(key: K): Token? {
tokens[key]?.let {
internal suspend fun get(key: String): Token? {
mutex.withLock {
tokens[key]
}?.let {
if (it.expired()) {
rm(key)
}
}

return mutex.withLock {
tokens[key]
}
}

private suspend fun rm(key: K) {
private suspend fun rm(key: String) {
mutex.withLock {
tokens.remove(key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class TokenXTokenProvider(
.replace("\n", "")
}

private val cache = TokenCache<String>()
private val cache = TokenCache()

private suspend fun getAccessToken(cacheKey: String, body: () -> String): String {
cache.logg(secureLog)
val token = cache.get(cacheKey)
?: client.post(config.tokenEndpoint) {
accept(io.ktor.http.ContentType.Application.Json)
Expand Down

0 comments on commit 5d4fd70

Please sign in to comment.