Skip to content

Commit fd79dbb

Browse files
committed
Reclassify to Verifiers and Formats
First, it appears to me that OAuth2 Access Tokens can be expressed in a number of formats. From the JWT spec: JSON Web Token (JWT) is a compact claims format And in the case of an opaque token, the format is "opaque" to us until, perhaps, it is processed. For now, the DSL supports the configuration of JWT and Opaque as formats supported by this resource server instance. Roughly speaking, each format configured implies an additional AuthenticationProvider. In configuring each format, the DSL supports custom ways to process tokens of that format. Second, verification strategies stand independent of the token's original format. Once any claims have actually been made, then they can be verified. At the very least, a token's claims to its own validity are confirmed. For example, a token's iat and exp. This is not configurable. Additionally, there are two additional standard verification steps, decrypting the claims and verifying the attached signature. The DSL, then, supports configuration of these two verification steps, though they will not result in instances of OAuth2AccessTokenVerifier. The DSL also allows for the specification of custom verification on top of basic verification. Issue: spring-projects/spring-security#5226
1 parent cf956c5 commit fd79dbb

File tree

1 file changed

+115
-8
lines changed

1 file changed

+115
-8
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/resourceserver/OAuth2ResourceServerConfigurer.java

+115-8
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,138 @@
1717

1818
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
1919
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
20+
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
2021
import org.springframework.security.oauth2.core.OAuth2TokenVerifier;
22+
import org.springframework.security.oauth2.jwt.Jwt;
2123
import org.springframework.security.oauth2.resourceserver.web.BearerTokenResolver;
2224

25+
/**
26+
* Example configuration:
27+
*
28+
* oauth2().resourceServer().accessToken()
29+
* .formats()
30+
* .jwt()
31+
* .verifiers()
32+
* .signature().keys("http://jwk.url")
33+
*
34+
* Or:
35+
*
36+
* oauth2().resourceServer().accessToken()
37+
* .formats()
38+
* .jwt().processor(auth0AccessTokenProcessor())
39+
* .opaque().processor(auth0AccessTokenProcessor())
40+
* .verifiers()
41+
* .addVerifier(claims -> {
42+
* if ( claims.get("iss") == null ) {
43+
* throw new OAuth2AuthenticationException(...);
44+
* }
45+
* })
46+
*
47+
* @author Josh Cummings
48+
*/
2349
public class OAuth2ResourceServerConfigurer<B extends HttpSecurityBuilder<B>> extends
2450
AbstractHttpConfigurer<OAuth2ResourceServerConfigurer<B>, B> {
2551

52+
private AccessTokenFormatsConfigurer accessTokenFormatsConfigurer;
53+
private AccessTokenVerifiersConfigurer accessTokenVerifiersConfigurer;
54+
2655
public OAuth2ResourceServerConfigurer<B> bearerTokenResolver(BearerTokenResolver resolver) {
2756
return this;
2857
}
2958

30-
public OAuth2ResourceServerConfigurer<B> accessTokenVerifier(OAuth2TokenVerifier... verifiers) {
31-
return this;
59+
public AccessTokenConfigurer accessToken(OAuth2TokenVerifier... verifiers) {
60+
return new AccessTokenConfigurer();
3261
}
3362

34-
public JwtConfigurer jwt() {
35-
return null;
36-
}
63+
public class AccessTokenConfigurer {
64+
public AccessTokenVerifiersConfigurer verifiers() {
65+
return new AccessTokenVerifiersConfigurer();
66+
}
3767

38-
public class JwtConfigurer {
39-
public JwtConfigurer jwkSetUrl(String location) {
40-
return this;
68+
public AccessTokenFormatsConfigurer formats() {
69+
return new AccessTokenFormatsConfigurer();
4170
}
4271

4372
public OAuth2ResourceServerConfigurer<B> and() {
4473
return OAuth2ResourceServerConfigurer.this;
4574
}
4675
}
76+
77+
public class AccessTokenVerifiersConfigurer {
78+
public SignatureVerificationConfigurer signature() {
79+
return new SignatureVerificationConfigurer();
80+
}
81+
82+
public EncryptionVerificationConfigurer encryption() {
83+
return new EncryptionVerificationConfigurer();
84+
}
85+
86+
public AccessTokenVerifiersConfigurer addVerifier(OAuth2TokenVerifier verifier) {
87+
return this;
88+
}
89+
90+
public AccessTokenConfigurer and() {
91+
return null;
92+
}
93+
}
94+
95+
public class SignatureVerificationConfigurer {
96+
public SignatureVerificationConfigurer keys(String uri) {
97+
return this;
98+
}
99+
100+
public AccessTokenVerifiersConfigurer and() {
101+
return OAuth2ResourceServerConfigurer.this.accessTokenVerifiersConfigurer;
102+
}
103+
}
104+
105+
public class EncryptionVerificationConfigurer {
106+
public EncryptionVerificationConfigurer keys(String uri) {
107+
return this;
108+
}
109+
110+
public AccessTokenVerifiersConfigurer and() {
111+
return OAuth2ResourceServerConfigurer.this.accessTokenVerifiersConfigurer;
112+
}
113+
}
114+
115+
public class AccessTokenFormatsConfigurer {
116+
public OpaqueAccessTokenFormatConfigurer opaque() {
117+
return new OpaqueAccessTokenFormatConfigurer();
118+
}
119+
120+
public JwtAccessTokenFormatConfigurer jwt() {
121+
return new JwtAccessTokenFormatConfigurer();
122+
}
123+
124+
public AccessTokenConfigurer and() {
125+
return null;
126+
}
127+
}
128+
129+
public class OpaqueAccessTokenFormatConfigurer {
130+
public OpaqueAccessTokenFormatConfigurer processor
131+
(OAuth2AccessTokenProcessor<? extends AbstractOAuth2Token> processor) {
132+
return this;
133+
}
134+
135+
public AccessTokenFormatsConfigurer and() {
136+
return OAuth2ResourceServerConfigurer.this.accessTokenFormatsConfigurer;
137+
}
138+
}
139+
140+
public class JwtAccessTokenFormatConfigurer {
141+
public JwtAccessTokenFormatConfigurer processor
142+
(OAuth2AccessTokenProcessor<Jwt> processor) {
143+
return this;
144+
}
145+
146+
public AccessTokenFormatsConfigurer and() {
147+
return OAuth2ResourceServerConfigurer.this.accessTokenFormatsConfigurer;
148+
}
149+
}
150+
151+
private interface OAuth2AccessTokenProcessor<T extends AbstractOAuth2Token> {
152+
T process(String token);
153+
}
47154
}

0 commit comments

Comments
 (0)