Skip to content

Commit ce17a99

Browse files
authored
feat: Support custom generation of token validity period (#428)
1 parent 207e8e4 commit ce17a99

File tree

9 files changed

+30
-16
lines changed

9 files changed

+30
-16
lines changed

cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenCompositeAuthentication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TokenCompositeAuthentication(
4545
): Mono<out CompositeToken> {
4646
return authenticate(credentialsType, credentials)
4747
.map {
48-
tokenConverter.asToken(it)
48+
tokenConverter.toToken(it)
4949
}
5050
}
5151
}

cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenConverter.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ package me.ahoo.cosec.token
1414

1515
import me.ahoo.cosec.api.principal.CoSecPrincipal
1616
import me.ahoo.cosec.api.token.CompositeToken
17+
import java.time.Duration
1718

1819
/**
1920
* Token Converter.
2021
*
2122
* @author ahoo wang
2223
*/
23-
fun interface TokenConverter {
24-
fun asToken(principal: CoSecPrincipal): CompositeToken
24+
interface TokenConverter {
25+
fun toToken(principal: CoSecPrincipal): CompositeToken
26+
fun toToken(
27+
principal: CoSecPrincipal,
28+
accessTokenValidity: Duration,
29+
refreshTokenValidity: Duration
30+
): CompositeToken
2531
}

cosec-core/src/test/kotlin/me/ahoo/cosec/token/TokenCompositeAuthenticationTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TokenCompositeAuthenticationTest {
3434
val compositeAuthentication = CompositeAuthentication(DefaultAuthenticationProvider)
3535
val compositeToken = SimpleCompositeToken("accessToken", "refreshToken")
3636
val tokenConverter = mockk<TokenConverter> {
37-
every { asToken(any()) } returns compositeToken
37+
every { toToken(any()) } returns compositeToken
3838
}
3939
val tokenCompositeAuthentication = TokenCompositeAuthentication(compositeAuthentication, tokenConverter)
4040
assertThat(tokenCompositeAuthentication.supportCredentials, `is`(Credentials::class.java))

cosec-jwt/src/jmh/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterBenchmark.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ open class JwtTokenConverterBenchmark {
6363
algorithm = Algorithm.HMAC256("FyN0Igd80Gas8stTavArGKOYnS9uLWGA_")
6464
jwtTokenConverter = JwtTokenConverter(MockIdGenerator.INSTANCE, algorithm)
6565
jwtTokenVerifier = JwtTokenVerifier(algorithm)
66-
token = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS)
66+
token = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS)
6767
}
6868

6969
@Benchmark

cosec-jwt/src/main/kotlin/me/ahoo/cosec/jwt/JwtTokenConverter.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ class JwtTokenConverter(
3838
private val refreshTokenValidity: Duration = Duration.ofDays(7)
3939
) : TokenConverter {
4040

41-
override fun asToken(principal: CoSecPrincipal): CompositeToken {
41+
override fun toToken(principal: CoSecPrincipal): CompositeToken {
42+
return toToken(principal, accessTokenValidity, refreshTokenValidity)
43+
}
44+
45+
override fun toToken(
46+
principal: CoSecPrincipal,
47+
accessTokenValidity: Duration,
48+
refreshTokenValidity: Duration
49+
): CompositeToken {
4250
val accessTokenId = idGenerator.generateAsString()
4351
val now = Date()
4452
val accessTokenExp = Date(System.currentTimeMillis() + accessTokenValidity.toMillis())

cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenConverterTest.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ internal class JwtTokenConverterTest {
3030
private val jwtTokenVerifier = JwtTokenVerifier(JwtFixture.ALGORITHM)
3131

3232
@Test
33-
fun anonymousAsToken() {
34-
val token: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS)
33+
fun anonymousToToken() {
34+
val token: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS)
3535
assertThat(token, notNullValue())
3636
}
3737

3838
@Test
39-
fun asToken() {
39+
fun toToken() {
4040
val principal =
4141
SimplePrincipal(
4242
"id",
@@ -46,12 +46,12 @@ internal class JwtTokenConverterTest {
4646
"attr_string" to "attr_string_value"
4747
),
4848
)
49-
val token: CompositeToken = jwtTokenConverter.asToken(principal)
49+
val token: CompositeToken = jwtTokenConverter.toToken(principal)
5050
assertThat(token, notNullValue())
5151
val verified = jwtTokenVerifier.verify<TokenPrincipal>(token)
5252
assertThat(verified.id, equalTo(principal.id))
5353
assertThat(verified.attributes["attr_string"], equalTo("attr_string_value"))
54-
val token2 = jwtTokenConverter.asToken(verified)
54+
val token2 = jwtTokenConverter.toToken(verified)
5555
assertThat(token2, notNullValue())
5656
}
5757
}

cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/JwtTokenVerifierTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class JwtTokenVerifierTest {
3333

3434
@Test
3535
fun verify() {
36-
val token: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS)
36+
val token: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS)
3737
val principal: TokenTenantPrincipal = jwtTokenVerifier.verify(token)
3838
assertThat(principal.name, equalTo(CoSecPrincipal.ANONYMOUS_ID))
3939
}
4040

4141
@Test
4242
fun refresh() {
43-
val oldToken: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS)
43+
val oldToken: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS)
4444
val newTokenPrincipal = jwtTokenVerifier.refresh<TokenTenantPrincipal>(oldToken)
4545
assertThat(newTokenPrincipal.id, equalTo(SimpleTenantPrincipal.ANONYMOUS.id))
4646
assertThat(newTokenPrincipal.tenant.tenantId, equalTo(SimpleTenantPrincipal.ANONYMOUS.tenant.tenantId))
@@ -55,7 +55,7 @@ class JwtTokenVerifierTest {
5555
Duration.ofMillis(1),
5656
Duration.ofMillis(1)
5757
)
58-
val oldToken: CompositeToken = converter.asToken(SimpleTenantPrincipal.ANONYMOUS)
58+
val oldToken: CompositeToken = converter.toToken(SimpleTenantPrincipal.ANONYMOUS)
5959
TimeUnit.SECONDS.sleep(1)
6060
assertThrows(TokenExpiredException::class.java) { jwtTokenVerifier.refresh<TokenPrincipal>(oldToken) }
6161
}

cosec-jwt/src/test/kotlin/me/ahoo/cosec/jwt/SimpleRefreshTokenAuthenticationTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SimpleRefreshTokenAuthenticationTest {
3333
fun authenticate() {
3434
val refreshTokenAuthentication = SimpleRefreshTokenAuthentication(jwtTokenVerifier)
3535
assertThat(refreshTokenAuthentication.supportCredentials, `is`(RefreshTokenCredentials::class.java))
36-
val oldToken: CompositeToken = jwtTokenConverter.asToken(SimpleTenantPrincipal.ANONYMOUS)
36+
val oldToken: CompositeToken = jwtTokenConverter.toToken(SimpleTenantPrincipal.ANONYMOUS)
3737

3838
refreshTokenAuthentication.authenticate(object : RefreshTokenCredentials {
3939
override val accessToken: String

cosec-webflux/src/test/kotlin/me/ahoo/cosec/webflux/ReactiveAuthorizationFilterTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal class ReactiveAuthorizationFilterTest {
4848
val algorithm = Algorithm.HMAC256("FyN0Igd80Gas8stTavArGKOYnS9uLWGA_")
4949
val jwtTokenConverter = JwtTokenConverter(MockIdGenerator.INSTANCE, algorithm)
5050
fun createAccessToken(principal: SimplePrincipal): String {
51-
return jwtTokenConverter.asToken(principal).accessToken
51+
return jwtTokenConverter.toToken(principal).accessToken
5252
}
5353
}
5454

0 commit comments

Comments
 (0)