-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endrer maskinporten oauth provider så den er likere de andre
Co-authored-by: Robin Tordly <[email protected]> Co-authored-by: Sturle Helland <[email protected]> Co-authored-by: Vetle Hollund <[email protected]>
- Loading branch information
1 parent
0febb34
commit 3b0991d
Showing
2 changed files
with
64 additions
and
17 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
51 changes: 51 additions & 0 deletions
51
ktor-auth-maskinporten/main/no/nav/aap/ktor/client/maskinporten/client/Token.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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package no.nav.aap.ktor.client.maskinporten.client | ||
|
||
import com.nimbusds.jwt.SignedJWT | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import org.slf4j.Logger | ||
import java.time.Instant | ||
|
||
internal data class Token(val access_token: String) { | ||
private val signedJwt = SignedJWT.parse(access_token) | ||
private val expiry = signedJwt.jwtClaimsSet.expirationTime.toInstant().minusSeconds(LEEWAY_SECONDS) | ||
|
||
fun hasExpired() = expiry < Instant.now() | ||
} | ||
|
||
internal class TokenCache { | ||
private val tokens: HashMap<String, Token> = hashMapOf() | ||
private val mutex = Mutex() | ||
|
||
internal 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: String): Token? { | ||
mutex.withLock { | ||
tokens[key] | ||
}?.let { | ||
if (it.hasExpired()) { | ||
rm(key) | ||
} | ||
} | ||
|
||
return mutex.withLock { | ||
tokens[key] | ||
} | ||
} | ||
|
||
private suspend fun rm(key: String) { | ||
mutex.withLock { | ||
tokens.remove(key) | ||
} | ||
} | ||
} |