Skip to content

Commit f201022

Browse files
committed
Introduce OAuth2TokenGenerator
Closes gh-414
1 parent 8b32ace commit f201022

21 files changed

+1229
-438
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
import org.springframework.security.core.Authentication;
3838
import org.springframework.security.core.Transient;
3939
import org.springframework.security.core.context.SecurityContext;
40+
import org.springframework.security.oauth2.core.OAuth2Token;
4041
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
4142
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
43+
import org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator;
4244
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenIntrospectionAuthenticationProvider;
4345
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
4446
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
@@ -146,6 +148,19 @@ public OAuth2AuthorizationServerConfigurer<B> providerSettings(ProviderSettings
146148
return this;
147149
}
148150

151+
/**
152+
* Sets the token generator.
153+
*
154+
* @param tokenGenerator the token generator
155+
* @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration
156+
* @since 0.2.3
157+
*/
158+
public OAuth2AuthorizationServerConfigurer<B> tokenGenerator(OAuth2TokenGenerator<? extends OAuth2Token> tokenGenerator) {
159+
Assert.notNull(tokenGenerator, "tokenGenerator cannot be null");
160+
getBuilder().setSharedObject(OAuth2TokenGenerator.class, tokenGenerator);
161+
return this;
162+
}
163+
149164
/**
150165
* Configures OAuth 2.0 Client Authentication.
151166
*

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
import org.springframework.context.ApplicationContext;
2727
import org.springframework.core.ResolvableType;
2828
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
29+
import org.springframework.security.oauth2.core.OAuth2Token;
2930
import org.springframework.security.oauth2.jwt.JwtEncoder;
3031
import org.springframework.security.oauth2.jwt.NimbusJwsEncoder;
3132
import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationConsentService;
3233
import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService;
3334
import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
35+
import org.springframework.security.oauth2.server.authorization.JwtGenerator;
3436
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
3537
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
3638
import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
39+
import org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator;
3740
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
3841
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
3942
import org.springframework.util.StringUtils;
@@ -82,7 +85,25 @@ static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationConsentService getA
8285
return authorizationConsentService;
8386
}
8487

85-
static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
88+
@SuppressWarnings("unchecked")
89+
static <B extends HttpSecurityBuilder<B>> OAuth2TokenGenerator<? extends OAuth2Token> getTokenGenerator(B builder) {
90+
OAuth2TokenGenerator<? extends OAuth2Token> tokenGenerator = builder.getSharedObject(OAuth2TokenGenerator.class);
91+
if (tokenGenerator == null) {
92+
tokenGenerator = getOptionalBean(builder, OAuth2TokenGenerator.class);
93+
if (tokenGenerator == null) {
94+
JwtGenerator jwtGenerator = new JwtGenerator(getJwtEncoder(builder));
95+
OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer = getJwtCustomizer(builder);
96+
if (jwtCustomizer != null) {
97+
jwtGenerator.setJwtCustomizer(jwtCustomizer);
98+
}
99+
tokenGenerator = jwtGenerator;
100+
}
101+
builder.setSharedObject(OAuth2TokenGenerator.class, tokenGenerator);
102+
}
103+
return tokenGenerator;
104+
}
105+
106+
private static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
86107
JwtEncoder jwtEncoder = builder.getSharedObject(JwtEncoder.class);
87108
if (jwtEncoder == null) {
88109
jwtEncoder = getOptionalBean(builder, JwtEncoder.class);
@@ -107,7 +128,7 @@ static <B extends HttpSecurityBuilder<B>> JWKSource<SecurityContext> getJwkSourc
107128
}
108129

109130
@SuppressWarnings("unchecked")
110-
static <B extends HttpSecurityBuilder<B>> OAuth2TokenCustomizer<JwtEncodingContext> getJwtCustomizer(B builder) {
131+
private static <B extends HttpSecurityBuilder<B>> OAuth2TokenCustomizer<JwtEncodingContext> getJwtCustomizer(B builder) {
111132
OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer = builder.getSharedObject(OAuth2TokenCustomizer.class);
112133
if (jwtCustomizer == null) {
113134
ResolvableType type = ResolvableType.forClassWithGenerics(OAuth2TokenCustomizer.class, JwtEncodingContext.class);

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
2929
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
3030
import org.springframework.security.oauth2.core.OAuth2Error;
31+
import org.springframework.security.oauth2.core.OAuth2Token;
3132
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
32-
import org.springframework.security.oauth2.jwt.JwtEncoder;
33-
import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
34-
import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
33+
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
34+
import org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator;
3535
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken;
3636
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationProvider;
3737
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationGrantAuthenticationToken;
@@ -160,34 +160,19 @@ RequestMatcher getRequestMatcher() {
160160
private <B extends HttpSecurityBuilder<B>> List<AuthenticationProvider> createDefaultAuthenticationProviders(B builder) {
161161
List<AuthenticationProvider> authenticationProviders = new ArrayList<>();
162162

163-
JwtEncoder jwtEncoder = OAuth2ConfigurerUtils.getJwtEncoder(builder);
164-
OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer = OAuth2ConfigurerUtils.getJwtCustomizer(builder);
163+
OAuth2AuthorizationService authorizationService = OAuth2ConfigurerUtils.getAuthorizationService(builder);
164+
OAuth2TokenGenerator<? extends OAuth2Token> tokenGenerator = OAuth2ConfigurerUtils.getTokenGenerator(builder);
165165

166166
OAuth2AuthorizationCodeAuthenticationProvider authorizationCodeAuthenticationProvider =
167-
new OAuth2AuthorizationCodeAuthenticationProvider(
168-
OAuth2ConfigurerUtils.getAuthorizationService(builder),
169-
jwtEncoder);
170-
if (jwtCustomizer != null) {
171-
authorizationCodeAuthenticationProvider.setJwtCustomizer(jwtCustomizer);
172-
}
167+
new OAuth2AuthorizationCodeAuthenticationProvider(authorizationService, tokenGenerator);
173168
authenticationProviders.add(authorizationCodeAuthenticationProvider);
174169

175170
OAuth2RefreshTokenAuthenticationProvider refreshTokenAuthenticationProvider =
176-
new OAuth2RefreshTokenAuthenticationProvider(
177-
OAuth2ConfigurerUtils.getAuthorizationService(builder),
178-
jwtEncoder);
179-
if (jwtCustomizer != null) {
180-
refreshTokenAuthenticationProvider.setJwtCustomizer(jwtCustomizer);
181-
}
171+
new OAuth2RefreshTokenAuthenticationProvider(authorizationService, tokenGenerator);
182172
authenticationProviders.add(refreshTokenAuthenticationProvider);
183173

184174
OAuth2ClientCredentialsAuthenticationProvider clientCredentialsAuthenticationProvider =
185-
new OAuth2ClientCredentialsAuthenticationProvider(
186-
OAuth2ConfigurerUtils.getAuthorizationService(builder),
187-
jwtEncoder);
188-
if (jwtCustomizer != null) {
189-
clientCredentialsAuthenticationProvider.setJwtCustomizer(jwtCustomizer);
190-
}
175+
new OAuth2ClientCredentialsAuthenticationProvider(authorizationService, tokenGenerator);
191176
authenticationProviders.add(clientCredentialsAuthenticationProvider);
192177

193178
return authenticationProviders;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ <B extends HttpSecurityBuilder<B>> void init(B builder) {
5757
new OidcClientRegistrationAuthenticationProvider(
5858
OAuth2ConfigurerUtils.getRegisteredClientRepository(builder),
5959
OAuth2ConfigurerUtils.getAuthorizationService(builder),
60-
OAuth2ConfigurerUtils.getJwtEncoder(builder));
60+
OAuth2ConfigurerUtils.getTokenGenerator(builder));
6161
builder.authenticationProvider(postProcess(oidcClientRegistrationAuthenticationProvider));
6262
}
6363

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2020-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
import java.util.Collections;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.lang.Nullable;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* Default implementation of {@link OAuth2TokenContext}.
27+
*
28+
* @author Joe Grandja
29+
* @since 0.2.3
30+
* @see OAuth2TokenContext
31+
*/
32+
public final class DefaultOAuth2TokenContext implements OAuth2TokenContext {
33+
private final Map<Object, Object> context;
34+
35+
private DefaultOAuth2TokenContext(Map<Object, Object> context) {
36+
this.context = Collections.unmodifiableMap(new HashMap<>(context));
37+
}
38+
39+
@SuppressWarnings("unchecked")
40+
@Nullable
41+
@Override
42+
public <V> V get(Object key) {
43+
return hasKey(key) ? (V) this.context.get(key) : null;
44+
}
45+
46+
@Override
47+
public boolean hasKey(Object key) {
48+
Assert.notNull(key, "key cannot be null");
49+
return this.context.containsKey(key);
50+
}
51+
52+
/**
53+
* Returns a new {@link Builder}.
54+
*
55+
* @return the {@link Builder}
56+
*/
57+
public static Builder builder() {
58+
return new Builder();
59+
}
60+
61+
/**
62+
* A builder for {@link DefaultOAuth2TokenContext}.
63+
*/
64+
public static final class Builder extends AbstractBuilder<DefaultOAuth2TokenContext, Builder> {
65+
66+
private Builder() {
67+
}
68+
69+
/**
70+
* Builds a new {@link DefaultOAuth2TokenContext}.
71+
*
72+
* @return the {@link DefaultOAuth2TokenContext}
73+
*/
74+
public DefaultOAuth2TokenContext build() {
75+
return new DefaultOAuth2TokenContext(getContext());
76+
}
77+
78+
}
79+
80+
}

0 commit comments

Comments
 (0)