libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 2d87b329c99cb2b335df317fe00faab941055b1a
parent 482241ee0e43b9ee83157c7b3cdfe873ae4f36c2
Author: Antoine A <>
Date:   Thu, 31 Oct 2024 14:46:06 +0100

bank: improve create-token

Diffstat:
Mbank/src/main/kotlin/tech/libeufin/bank/cli/CreateToken.kt | 54++++++++++++++++++++++++++++++++----------------------
1 file changed, 32 insertions(+), 22 deletions(-)

diff --git a/bank/src/main/kotlin/tech/libeufin/bank/cli/CreateToken.kt b/bank/src/main/kotlin/tech/libeufin/bank/cli/CreateToken.kt @@ -41,7 +41,7 @@ class CreateToken : CliktCommand("create-token") { help = "Account username" ).required() private val scope by option("--scope", "-s", help = "Scope for the token").enum<TokenScope>().required() - private val duration by option("--duration", "-d", metavar = "<forever|micros>",help = "Custom token validity duration").convert { + private val duration by option("--duration", "-d", metavar = "<forever|micros>", help = "Custom token validity duration").convert { if (it == "forever") { ChronoUnit.FOREVER.duration } else { @@ -55,33 +55,43 @@ class CreateToken : CliktCommand("create-token") { }.default(TOKEN_DEFAULT_DURATION) private val description by option("--description", help = "Optional token description") private val refreshable by option("--refreshable", help = "Make the token refreshable into a new token").flag() + private val currentToken by option("--current-token", help = "Current token to reuse if still active").convert { + Base32Crockford.decode(it.removePrefix(TOKEN_PREFIX)) + } override fun run() = cliCmd(logger, common.log) { bankConfig(common.config).withDb { db, cfg -> - val creationTime = Instant.now() - val expirationTimestamp = - if (duration == ChronoUnit.FOREVER.duration) { - Instant.MAX - } else { - try { - creationTime.plus(duration) - } catch (e: Exception) { - throw Exception("Bad token duration: ${e.message}") + val now = Instant.now() + + val token = currentToken?.let { db.token.access(it, now) } + + if (token != null && token.expirationTime.isBefore(now) && validScope(scope.logical(), token.scope)) { + println("$TOKEN_PREFIX$currentToken") + } else { + val expirationTimestamp = + if (duration == ChronoUnit.FOREVER.duration) { + Instant.MAX + } else { + try { + now.plus(duration) + } catch (e: Exception) { + throw Exception("Bad token duration: ${e.message}") + } } + val token = Base32Crockford32B.secureRand() + if (!db.token.create( + username = username, + content = token.raw, + creationTime = now, + expirationTime = expirationTimestamp, + scope = scope, + isRefreshable = refreshable, + description = description + )) { + throw internalServerError("Unknown account $username") } - val token = Base32Crockford32B.secureRand() - if (!db.token.create( - username = username, - content = token.raw, - creationTime = creationTime, - expirationTime = expirationTimestamp, - scope = scope, - isRefreshable = refreshable, - description = description - )) { - throw internalServerError("Unknown account $username") + println("$TOKEN_PREFIX$token") } - println("$TOKEN_PREFIX$token") } } } \ No newline at end of file