-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
829520a
commit 864ddaf
Showing
6 changed files
with
111 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
azure-token-client/src/main/kotlin/com/github/navikt/tbd_libs/azure/AzureTokenProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.github.navikt.tbd_libs.azure | ||
|
||
interface AzureTokenProvider { | ||
fun onBehalfOfToken(scope: String, token: String): AzureToken | ||
fun bearerToken(scope: String): AzureToken | ||
} |
25 changes: 20 additions & 5 deletions
25
...-token-client/src/main/kotlin/com/github/navikt/tbd_libs/azure/InMemoryAzureTokenCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,29 @@ | ||
package com.github.navikt.tbd_libs.azure | ||
|
||
import java.security.MessageDigest | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class InMemoryAzureTokenCache(private val other: AzureTokenProvider) : AzureTokenProvider { | ||
private val cache = ConcurrentHashMap<String, AzureToken>() | ||
|
||
override fun bearerToken(scope: String): AzureToken { | ||
return cachedToken(scope) ?: lagreNy(scope) | ||
} | ||
override fun onBehalfOfToken(scope: String, token: String) = | ||
fraCacheEllerHentNy(oboCacheKey(token, scope)) { other.onBehalfOfToken(scope, token) } | ||
|
||
override fun bearerToken(scope: String) = | ||
fraCacheEllerHentNy(scope) { other.bearerToken(scope) } | ||
|
||
private fun fraCacheEllerHentNy(cacheKey: String, hentNy: () -> AzureToken) = | ||
cachedToken(cacheKey) ?: lagreNy(cacheKey, hentNy()) | ||
|
||
private fun cachedToken(scope: String) = cache[scope]?.takeUnless { it.isExpired } | ||
private fun lagreNy(scope: String) = other.bearerToken(scope).also { cache[scope] = it } | ||
private fun cachedToken(cacheKey: String) = cache[cacheKey]?.takeUnless { it.isExpired } | ||
private fun lagreNy(cacheKey: String, token: AzureToken) = token.also { cache[cacheKey] = it } | ||
|
||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
private fun oboCacheKey(token: String, scope: String): String { | ||
val nøkkel = "${token}${scope}".toByteArray() | ||
val md = MessageDigest.getInstance("SHA-256") | ||
val digest = md.digest(nøkkel) | ||
return digest.toHexString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters