Skip to content

Commit dcf3d5c

Browse files
iYOUR_USERNAMEnjecting clock when we are generating the token
Signed-off-by: AlessandroMinoccheri <[email protected]>
1 parent 40d503a commit dcf3d5c

File tree

13 files changed

+45
-3
lines changed

13 files changed

+45
-3
lines changed

docs/modules/ROOT/pages/core-model-components.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ The following example shows how to register an `OAuth2TokenGenerator` `@Bean`:
393393
public OAuth2TokenGenerator<?> tokenGenerator() {
394394
JwtEncoder jwtEncoder = ...
395395
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
396+
jwtGenerator.setClock(Clock.systemUTC());
396397
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
397398
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
398399
return new DelegatingOAuth2TokenGenerator(
@@ -441,6 +442,7 @@ The following example shows how to implement an `OAuth2TokenCustomizer<OAuth2Tok
441442
public OAuth2TokenGenerator<?> tokenGenerator() {
442443
JwtEncoder jwtEncoder = ...
443444
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
445+
jwtGenerator.setClock(Clock.systemUTC());
444446
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
445447
accessTokenGenerator.setAccessTokenCustomizer(accessTokenCustomizer());
446448
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
@@ -473,6 +475,7 @@ The following example shows how to implement an `OAuth2TokenCustomizer<JwtEncodi
473475
public OAuth2TokenGenerator<?> tokenGenerator() {
474476
JwtEncoder jwtEncoder = ...
475477
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
478+
jwtGenerator.setClock(Clock.systemUTC());
476479
jwtGenerator.setJwtCustomizer(jwtCustomizer());
477480
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
478481
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();

docs/src/main/java/sample/extgrant/SecurityConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package sample.extgrant;
1717

18+
import java.time.Clock;
1819
import java.util.UUID;
1920

2021
import com.nimbusds.jose.jwk.source.JWKSource;
@@ -100,6 +101,7 @@ OAuth2AuthorizationService authorizationService() {
100101
@Bean
101102
OAuth2TokenGenerator<?> tokenGenerator(JWKSource<SecurityContext> jwkSource) {
102103
JwtGenerator jwtGenerator = new JwtGenerator(new NimbusJwtEncoder(jwkSource));
104+
jwtGenerator.setClock(Clock.systemUTC());
103105
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
104106
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
105107
return new DelegatingOAuth2TokenGenerator(

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2ConfigurerUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers;
1717

18+
import java.time.Clock;
1819
import java.util.Map;
1920

2021
import com.nimbusds.jose.jwk.source.JWKSource;
@@ -128,6 +129,7 @@ private static JwtGenerator getJwtGenerator(HttpSecurity httpSecurity) {
128129
JwtEncoder jwtEncoder = getJwtEncoder(httpSecurity);
129130
if (jwtEncoder != null) {
130131
jwtGenerator = new JwtGenerator(jwtEncoder);
132+
jwtGenerator.setClock(Clock.systemUTC());
131133
jwtGenerator.setJwtCustomizer(getJwtCustomizer(httpSecurity));
132134
httpSecurity.setSharedObject(JwtGenerator.class, jwtGenerator);
133135
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/JwtGenerator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.token;
1717

18+
import java.time.Clock;
1819
import java.time.Instant;
1920
import java.time.temporal.ChronoUnit;
2021
import java.util.Collections;
@@ -61,6 +62,7 @@
6162
public final class JwtGenerator implements OAuth2TokenGenerator<Jwt> {
6263

6364
private final JwtEncoder jwtEncoder;
65+
private Clock clock;
6466

6567
private OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer;
6668

@@ -94,7 +96,7 @@ public Jwt generate(OAuth2TokenContext context) {
9496
}
9597
RegisteredClient registeredClient = context.getRegisteredClient();
9698

97-
Instant issuedAt = Instant.now();
99+
Instant issuedAt = (clock == null) ? Instant.now() : clock.instant();
98100
Instant expiresAt;
99101
JwsAlgorithm jwsAlgorithm = SignatureAlgorithm.RS256;
100102
if (OidcParameterNames.ID_TOKEN.equals(context.getTokenType().getValue())) {
@@ -207,4 +209,8 @@ public void setJwtCustomizer(OAuth2TokenCustomizer<JwtEncodingContext> jwtCustom
207209
this.jwtCustomizer = jwtCustomizer;
208210
}
209211

212+
public void setClock(Clock clock) {
213+
this.clock = clock;
214+
}
215+
210216
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/OAuth2AccessTokenGenerator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.token;
1717

18+
import java.time.Clock;
1819
import java.time.Instant;
1920
import java.util.Base64;
2021
import java.util.Collections;
@@ -52,6 +53,7 @@ public final class OAuth2AccessTokenGenerator implements OAuth2TokenGenerator<OA
5253

5354
private final StringKeyGenerator accessTokenGenerator = new Base64StringKeyGenerator(
5455
Base64.getUrlEncoder().withoutPadding(), 96);
56+
private Clock clock;
5557

5658
private OAuth2TokenCustomizer<OAuth2TokenClaimsContext> accessTokenCustomizer;
5759

@@ -71,7 +73,7 @@ public OAuth2AccessToken generate(OAuth2TokenContext context) {
7173
}
7274
RegisteredClient registeredClient = context.getRegisteredClient();
7375

74-
Instant issuedAt = Instant.now();
76+
Instant issuedAt = (clock == null) ? Instant.now() : clock.instant();
7577
Instant expiresAt = issuedAt.plus(registeredClient.getTokenSettings().getAccessTokenTimeToLive());
7678

7779
// @formatter:off
@@ -156,4 +158,8 @@ public Map<String, Object> getClaims() {
156158

157159
}
158160

161+
public void setClock(Clock clock) {
162+
this.clock = clock;
163+
}
164+
159165
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/OAuth2RefreshTokenGenerator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.token;
1717

18+
import java.time.Clock;
1819
import java.time.Instant;
1920
import java.util.Base64;
2021

@@ -39,6 +40,7 @@ public final class OAuth2RefreshTokenGenerator implements OAuth2TokenGenerator<O
3940

4041
private final StringKeyGenerator refreshTokenGenerator = new Base64StringKeyGenerator(
4142
Base64.getUrlEncoder().withoutPadding(), 96);
43+
private Clock clock;
4244

4345
@Nullable
4446
@Override
@@ -51,7 +53,8 @@ public OAuth2RefreshToken generate(OAuth2TokenContext context) {
5153
return null;
5254
}
5355

54-
Instant issuedAt = Instant.now();
56+
Instant issuedAt = (clock == null) ? Instant.now() : clock.instant();
57+
5558
Instant expiresAt = issuedAt.plus(context.getRegisteredClient().getTokenSettings().getRefreshTokenTimeToLive());
5659
return new OAuth2RefreshToken(this.refreshTokenGenerator.generateKey(), issuedAt, expiresAt);
5760
}
@@ -66,4 +69,8 @@ private static boolean isPublicClientForAuthorizationCodeGrant(OAuth2TokenContex
6669
return false;
6770
}
6871

72+
public void setClock(Clock clock) {
73+
this.clock = clock;
74+
}
75+
6976
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.security.MessageDigest;
2020
import java.security.NoSuchAlgorithmException;
2121
import java.security.Principal;
22+
import java.time.Clock;
2223
import java.time.Duration;
2324
import java.time.Instant;
2425
import java.time.temporal.ChronoUnit;
@@ -133,6 +134,7 @@ public void setUp() {
133134
this.jwtEncoder = mock(JwtEncoder.class);
134135
this.jwtCustomizer = mock(OAuth2TokenCustomizer.class);
135136
JwtGenerator jwtGenerator = new JwtGenerator(this.jwtEncoder);
137+
jwtGenerator.setClock(Clock.systemUTC());
136138
jwtGenerator.setJwtCustomizer(this.jwtCustomizer);
137139
this.accessTokenCustomizer = mock(OAuth2TokenCustomizer.class);
138140
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2ClientCredentialsAuthenticationProviderTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.authentication;
1717

18+
import java.time.Clock;
1819
import java.time.Instant;
1920
import java.time.temporal.ChronoUnit;
2021
import java.util.Collections;
@@ -105,6 +106,7 @@ public void setUp() {
105106
this.jwtEncoder = mock(JwtEncoder.class);
106107
this.jwtCustomizer = mock(OAuth2TokenCustomizer.class);
107108
JwtGenerator jwtGenerator = new JwtGenerator(this.jwtEncoder);
109+
jwtGenerator.setClock(Clock.systemUTC());
108110
jwtGenerator.setJwtCustomizer(this.jwtCustomizer);
109111
this.accessTokenCustomizer = mock(OAuth2TokenCustomizer.class);
110112
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2RefreshTokenAuthenticationProviderTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.security.oauth2.server.authorization.authentication;
1717

1818
import java.security.Principal;
19+
import java.time.Clock;
1920
import java.time.Instant;
2021
import java.time.temporal.ChronoUnit;
2122
import java.util.Collections;
@@ -120,6 +121,7 @@ public void setUp() {
120121
given(this.jwtEncoder.encode(any())).willReturn(createJwt(Collections.singleton("scope1")));
121122
this.jwtCustomizer = mock(OAuth2TokenCustomizer.class);
122123
JwtGenerator jwtGenerator = new JwtGenerator(this.jwtEncoder);
124+
jwtGenerator.setClock(Clock.systemUTC());
123125
jwtGenerator.setJwtCustomizer(this.jwtCustomizer);
124126
this.accessTokenCustomizer = mock(OAuth2TokenCustomizer.class);
125127
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationCodeGrantTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.security.Principal;
2323
import java.text.MessageFormat;
24+
import java.time.Clock;
2425
import java.time.Instant;
2526
import java.time.temporal.ChronoUnit;
2627
import java.util.Arrays;
@@ -1234,6 +1235,7 @@ JwtEncoder jwtEncoder() {
12341235
@Bean
12351236
OAuth2TokenGenerator<?> tokenGenerator() {
12361237
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder());
1238+
jwtGenerator.setClock(Clock.systemUTC());
12371239
jwtGenerator.setJwtCustomizer(jwtCustomizer());
12381240
OAuth2TokenGenerator<OAuth2RefreshToken> refreshTokenGenerator = new CustomRefreshTokenGenerator();
12391241
return new DelegatingOAuth2TokenGenerator(jwtGenerator, refreshTokenGenerator);
@@ -1296,6 +1298,7 @@ JwtEncoder jwtEncoder() {
12961298
@Bean
12971299
OAuth2TokenGenerator<?> tokenGenerator() {
12981300
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder());
1301+
jwtGenerator.setClock(Clock.systemUTC());
12991302
jwtGenerator.setJwtCustomizer(jwtCustomizer());
13001303
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
13011304
OAuth2TokenGenerator<OAuth2Token> delegatingTokenGenerator = new DelegatingOAuth2TokenGenerator(

0 commit comments

Comments
 (0)