From ce17a99ddd61e5e0461f5ebb82d521c109bf29d9 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Mon, 16 Dec 2024 12:03:30 +0800 Subject: [PATCH] feat: Support custom generation of token validity period (#428) --- .../ahoo/cosec/token/TokenCompositeAuthentication.kt | 2 +- .../main/kotlin/me/ahoo/cosec/token/TokenConverter.kt | 10 ++++++++-- .../cosec/token/TokenCompositeAuthenticationTest.kt | 2 +- .../me/ahoo/cosec/jwt/JwtTokenConverterBenchmark.kt | 2 +- .../main/kotlin/me/ahoo/cosec/jwt/JwtTokenConverter.kt | 10 +++++++++- .../kotlin/me/ahoo/cosec/jwt/JwtTokenConverterTest.kt | 10 +++++----- .../kotlin/me/ahoo/cosec/jwt/JwtTokenVerifierTest.kt | 6 +++--- .../cosec/jwt/SimpleRefreshTokenAuthenticationTest.kt | 2 +- .../cosec/webflux/ReactiveAuthorizationFilterTest.kt | 2 +- 9 files changed, 30 insertions(+), 16 deletions(-) diff --git a/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenCompositeAuthentication.kt b/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenCompositeAuthentication.kt index 6f45ff60..33ffd531 100644 --- a/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenCompositeAuthentication.kt +++ b/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenCompositeAuthentication.kt @@ -45,7 +45,7 @@ class TokenCompositeAuthentication( ): Mono { return authenticate(credentialsType, credentials) .map { - tokenConverter.asToken(it) + tokenConverter.toToken(it) } } } diff --git a/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenConverter.kt b/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenConverter.kt index 171cf480..04aad2bf 100644 --- a/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenConverter.kt +++ b/cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenConverter.kt @@ -14,12 +14,18 @@ package me.ahoo.cosec.token import me.ahoo.cosec.api.principal.CoSecPrincipal import me.ahoo.cosec.api.token.CompositeToken +import java.time.Duration /** * Token Converter. * * @author ahoo wang */ -fun interface TokenConverter { - fun asToken(principal: CoSecPrincipal): CompositeToken +interface TokenConverter { + fun toToken(principal: CoSecPrincipal): CompositeToken + fun toToken( + principal: CoSecPrincipal, + accessTokenValidity: Duration, + refreshTokenValidity: Duration + ): CompositeToken } diff --git a/cosec-core/src/test/kotlin/me/ahoo/cosec/token/TokenCompositeAuthenticationTest.kt b/cosec-core/src/test/kotlin/me/ahoo/cosec/token/TokenCompositeAuthenticationTest.kt index 6e95d725..0e74f870 100644 --- a/cosec-core/src/test/kotlin/me/ahoo/cosec/token/TokenCompositeAuthenticationTest.kt +++ b/cosec-core/src/test/kotlin/me/ahoo/cosec/token/TokenCompositeAuthenticationTest.kt @@ -34,7 +34,7 @@ class TokenCompositeAuthenticationTest { val compositeAuthentication = CompositeAuthentication(DefaultAuthenticationProvider) val compositeToken = SimpleCompositeToken("accessToken", "refreshToken") val tokenConverter = mockk { - every { asToken(any()) } returns compositeToken + every { toToken(any()) } returns compositeToken } val tokenCompositeAuthentication = TokenCompositeAuthentication(compositeAuthentication, tokenConverter) assertThat(tokenCompositeAuthentication.supportCredentials, `is`(Credentials::class.java)) diff --git a/cosec-jwt/src/jmh/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterBenchmark.kt b/cosec-jwt/src/jmh/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterBenchmark.kt index 1759e4c4..feba9a78 100644 --- a/cosec-jwt/src/jmh/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterBenchmark.kt +++ b/cosec-jwt/src/jmh/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterBenchmark.kt @@ -63,7 +63,7 @@ open class JwtTokenConverterBenchmark { algorithm = Algorithm.HMAC256("FyN0Igd80Gas8stTavArGKOYnS9uLWGA_") jwtTokenConverter = JwtTokenConverter(MockIdGenerator.INSTANCE, algorithm) jwtTokenVerifier = JwtTokenVerifier(algorithm) - token = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS) + token = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS) } @Benchmark diff --git a/cosec-jwt/src/main/kotlin/me/ahoo/cosec/jwt/JwtTokenConverter.kt b/cosec-jwt/src/main/kotlin/me/ahoo/cosec/jwt/JwtTokenConverter.kt index 621c7018..66f654a6 100644 --- a/cosec-jwt/src/main/kotlin/me/ahoo/cosec/jwt/JwtTokenConverter.kt +++ b/cosec-jwt/src/main/kotlin/me/ahoo/cosec/jwt/JwtTokenConverter.kt @@ -38,7 +38,15 @@ class JwtTokenConverter( private val refreshTokenValidity: Duration = Duration.ofDays(7) ) : TokenConverter { - override fun asToken(principal: CoSecPrincipal): CompositeToken { + override fun toToken(principal: CoSecPrincipal): CompositeToken { + return toToken(principal, accessTokenValidity, refreshTokenValidity) + } + + override fun toToken( + principal: CoSecPrincipal, + accessTokenValidity: Duration, + refreshTokenValidity: Duration + ): CompositeToken { val accessTokenId = idGenerator.generateAsString() val now = Date() val accessTokenExp = Date(System.currentTimeMillis() + accessTokenValidity.toMillis()) diff --git a/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterTest.kt b/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterTest.kt index 02e57e44..a7bff72c 100644 --- a/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterTest.kt +++ b/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterTest.kt @@ -30,13 +30,13 @@ internal class JwtTokenConverterTest { private val jwtTokenVerifier = JwtTokenVerifier(JwtFixture.ALGORITHM) @Test - fun anonymousAsToken() { - val token: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS) + fun anonymousToToken() { + val token: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS) assertThat(token, notNullValue()) } @Test - fun asToken() { + fun toToken() { val principal = SimplePrincipal( "id", @@ -46,12 +46,12 @@ internal class JwtTokenConverterTest { "attr_string" to "attr_string_value" ), ) - val token: CompositeToken = jwtTokenConverter.asToken(principal) + val token: CompositeToken = jwtTokenConverter.toToken(principal) assertThat(token, notNullValue()) val verified = jwtTokenVerifier.verify(token) assertThat(verified.id, equalTo(principal.id)) assertThat(verified.attributes["attr_string"], equalTo("attr_string_value")) - val token2 = jwtTokenConverter.asToken(verified) + val token2 = jwtTokenConverter.toToken(verified) assertThat(token2, notNullValue()) } } diff --git a/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenVerifierTest.kt b/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenVerifierTest.kt index 089e38e1..9e2f3a2f 100644 --- a/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenVerifierTest.kt +++ b/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenVerifierTest.kt @@ -33,14 +33,14 @@ class JwtTokenVerifierTest { @Test fun verify() { - val token: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS) + val token: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS) val principal: TokenTenantPrincipal = jwtTokenVerifier.verify(token) assertThat(principal.name, equalTo(CoSecPrincipal.ANONYMOUS_ID)) } @Test fun refresh() { - val oldToken: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS) + val oldToken: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS) val newTokenPrincipal = jwtTokenVerifier.refresh(oldToken) assertThat(newTokenPrincipal.id, equalTo(SimpleTenantPrincipal.ANONYMOUS.id)) assertThat(newTokenPrincipal.tenant.tenantId, equalTo(SimpleTenantPrincipal.ANONYMOUS.tenant.tenantId)) @@ -55,7 +55,7 @@ class JwtTokenVerifierTest { Duration.ofMillis(1), Duration.ofMillis(1) ) - val oldToken: CompositeToken = converter.asToken(SimpleTenantPrincipal.ANONYMOUS) + val oldToken: CompositeToken = converter.toToken(SimpleTenantPrincipal.ANONYMOUS) TimeUnit.SECONDS.sleep(1) assertThrows(TokenExpiredException::class.java) { jwtTokenVerifier.refresh(oldToken) } } diff --git a/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/SimpleRefreshTokenAuthenticationTest.kt b/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/SimpleRefreshTokenAuthenticationTest.kt index 79aed6e0..f8e46d9e 100644 --- a/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/SimpleRefreshTokenAuthenticationTest.kt +++ b/cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/SimpleRefreshTokenAuthenticationTest.kt @@ -33,7 +33,7 @@ class SimpleRefreshTokenAuthenticationTest { fun authenticate() { val refreshTokenAuthentication = SimpleRefreshTokenAuthentication(jwtTokenVerifier) assertThat(refreshTokenAuthentication.supportCredentials, `is`(RefreshTokenCredentials::class.java)) - val oldToken: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS) + val oldToken: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS) refreshTokenAuthentication.authenticate(object : RefreshTokenCredentials { override val accessToken: String diff --git a/cosec-webflux/src/test/kotlin/me/ahoo/cosec/webflux/ReactiveAuthorizationFilterTest.kt b/cosec-webflux/src/test/kotlin/me/ahoo/cosec/webflux/ReactiveAuthorizationFilterTest.kt index b7152128..f8267a48 100644 --- a/cosec-webflux/src/test/kotlin/me/ahoo/cosec/webflux/ReactiveAuthorizationFilterTest.kt +++ b/cosec-webflux/src/test/kotlin/me/ahoo/cosec/webflux/ReactiveAuthorizationFilterTest.kt @@ -48,7 +48,7 @@ internal class ReactiveAuthorizationFilterTest { val algorithm = Algorithm.HMAC256("FyN0Igd80Gas8stTavArGKOYnS9uLWGA_") val jwtTokenConverter = JwtTokenConverter(MockIdGenerator.INSTANCE, algorithm) fun createAccessToken(principal: SimplePrincipal): String { - return jwtTokenConverter.asToken(principal).accessToken + return jwtTokenConverter.toToken(principal).accessToken } }