Skip to content

Add the time-to-live config for an authorization code at TokenSettings #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.security.Principal;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -565,8 +564,10 @@ public OAuth2AuthorizationCode generate(OAuth2TokenContext context) {
!OAuth2ParameterNames.CODE.equals(context.getTokenType().getValue())) {
return null;
}
RegisteredClient registeredClient = context.getRegisteredClient();

Instant issuedAt = Instant.now();
Instant expiresAt = issuedAt.plus(5, ChronoUnit.MINUTES); // TODO Allow configuration for authorization code time-to-live
Instant expiresAt = issuedAt.plus(registeredClient.getTokenSettings().getAuthorizationCodeTimeToLive());
return new OAuth2AuthorizationCode(this.authorizationCodeGenerator.generateKey(), issuedAt, expiresAt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ private Provider() {
public static final class Token {
private static final String TOKEN_SETTINGS_NAMESPACE = SETTINGS_NAMESPACE.concat("token.");

/**
* Set the time-to-live for an authorization code.
*/
public static final String AUTHORIZATION_CODE_TIME_TO_LIVE = TOKEN_SETTINGS_NAMESPACE.concat("authorization-code-time-to-live");

/**
* Set the time-to-live for an access token.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ private TokenSettings(Map<String, Object> settings) {
super(settings);
}

/**
* Returns the time-to-live for an authorization code. The default is 5 minutes.
*
* @return the time-to-live for an authorization code
*/
public Duration getAuthorizationCodeTimeToLive() {
return getSetting(ConfigurationSettingNames.Token.AUTHORIZATION_CODE_TIME_TO_LIVE);
}

/**
* Returns the time-to-live for an access token. The default is 5 minutes.
*
Expand Down Expand Up @@ -91,6 +100,7 @@ public SignatureAlgorithm getIdTokenSignatureAlgorithm() {
*/
public static Builder builder() {
return new Builder()
.authorizationCodeTimeToLive(Duration.ofMinutes(5))
.accessTokenTimeToLive(Duration.ofMinutes(5))
.accessTokenFormat(OAuth2TokenFormat.SELF_CONTAINED)
.reuseRefreshTokens(true)
Expand Down Expand Up @@ -118,6 +128,19 @@ public static class Builder extends AbstractBuilder<TokenSettings, Builder> {
private Builder() {
}

/**
* Set the time-to-live for an access token. Must be greater than {@code Duration.ZERO}.
* A maximum authorization code lifetime of 10 minutes is RECOMMENDED
*
* @param authorizationCodeTimeToLive the time-to-live for an authorization code
* @return the {@link Builder} for further configuration
*/
public Builder authorizationCodeTimeToLive(Duration authorizationCodeTimeToLive) {
Assert.notNull(authorizationCodeTimeToLive, "authorizationCodeTimeToLive cannot be null");
Assert.isTrue(authorizationCodeTimeToLive.getSeconds() > 0, "authorizationCodeTimeToLive must be greater than Duration.ZERO");
return setting(ConfigurationSettingNames.Token.AUTHORIZATION_CODE_TIME_TO_LIVE, authorizationCodeTimeToLive);
}

/**
* Set the time-to-live for an access token. Must be greater than {@code Duration.ZERO}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,42 @@ public class TokenSettingsTests {
@Test
public void buildWhenDefaultThenDefaultsAreSet() {
TokenSettings tokenSettings = TokenSettings.builder().build();
assertThat(tokenSettings.getSettings()).hasSize(5);
assertThat(tokenSettings.getSettings()).hasSize(6);
assertThat(tokenSettings.getAuthorizationCodeTimeToLive()).isEqualTo(Duration.ofMinutes(5));
assertThat(tokenSettings.getAccessTokenTimeToLive()).isEqualTo(Duration.ofMinutes(5));
assertThat(tokenSettings.getAccessTokenFormat()).isEqualTo(OAuth2TokenFormat.SELF_CONTAINED);
assertThat(tokenSettings.isReuseRefreshTokens()).isTrue();
assertThat(tokenSettings.getRefreshTokenTimeToLive()).isEqualTo(Duration.ofMinutes(60));
assertThat(tokenSettings.getIdTokenSignatureAlgorithm()).isEqualTo(SignatureAlgorithm.RS256);
}

@Test
public void authorizationCodeTimeToLiveWhenProvidedThenSet() {
Duration authorizationCodeTimeToLive = Duration.ofMinutes(10);
TokenSettings tokenSettings = TokenSettings.builder()
.authorizationCodeTimeToLive(authorizationCodeTimeToLive)
.build();
assertThat(tokenSettings.getAuthorizationCodeTimeToLive()).isEqualTo(authorizationCodeTimeToLive);
}

@Test
public void authorizationCodeTimeToLiveWhenNullOrZeroOrNegativeThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> TokenSettings.builder().authorizationCodeTimeToLive(null))
.isInstanceOf(IllegalArgumentException.class)
.extracting(Throwable::getMessage)
.isEqualTo("authorizationCodeTimeToLive cannot be null");

assertThatThrownBy(() -> TokenSettings.builder().authorizationCodeTimeToLive(Duration.ZERO))
.isInstanceOf(IllegalArgumentException.class)
.extracting(Throwable::getMessage)
.isEqualTo("authorizationCodeTimeToLive must be greater than Duration.ZERO");

assertThatThrownBy(() -> TokenSettings.builder().authorizationCodeTimeToLive(Duration.ofSeconds(-10)))
.isInstanceOf(IllegalArgumentException.class)
.extracting(Throwable::getMessage)
.isEqualTo("authorizationCodeTimeToLive must be greater than Duration.ZERO");
}

@Test
public void accessTokenTimeToLiveWhenProvidedThenSet() {
Duration accessTokenTimeToLive = Duration.ofMinutes(10);
Expand Down Expand Up @@ -136,7 +164,7 @@ public void settingWhenCustomThenSet() {
.setting("name1", "value1")
.settings(settings -> settings.put("name2", "value2"))
.build();
assertThat(tokenSettings.getSettings()).hasSize(7);
assertThat(tokenSettings.getSettings()).hasSize(8);
assertThat(tokenSettings.<String>getSetting("name1")).isEqualTo("value1");
assertThat(tokenSettings.<String>getSetting("name2")).isEqualTo("value2");
}
Expand Down