16
16
package org .springframework .security .oauth2 .server .resource .authentication ;
17
17
18
18
import java .time .Instant ;
19
- import java .util .ArrayList ;
20
19
import java .util .Collection ;
21
- import java .util .Collections ;
22
- import java .util .List ;
23
- import java .util .Map ;
24
20
25
21
import org .springframework .http .HttpStatus ;
26
22
import org .springframework .security .authentication .AbstractAuthenticationToken ;
27
23
import org .springframework .security .authentication .AuthenticationProvider ;
28
24
import org .springframework .security .core .Authentication ;
29
25
import org .springframework .security .core .AuthenticationException ;
30
26
import org .springframework .security .core .GrantedAuthority ;
31
- import org .springframework .security .core .authority .SimpleGrantedAuthority ;
32
27
import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
28
+ import org .springframework .security .oauth2 .core .OAuth2AuthenticatedPrincipal ;
33
29
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
34
30
import org .springframework .security .oauth2 .core .OAuth2Error ;
35
- import org .springframework .security .oauth2 .core .OAuth2TokenAttributes ;
36
- import org .springframework .security .oauth2 .server .resource .introspection .OAuth2IntrospectionException ;
37
- import org .springframework .security .oauth2 .server .resource .introspection .OpaqueTokenIntrospector ;
38
31
import org .springframework .security .oauth2 .server .resource .BearerTokenAuthenticationToken ;
39
32
import org .springframework .security .oauth2 .server .resource .BearerTokenError ;
33
+ import org .springframework .security .oauth2 .server .resource .introspection .OAuth2IntrospectionException ;
34
+ import org .springframework .security .oauth2 .server .resource .introspection .OpaqueTokenIntrospector ;
40
35
import org .springframework .util .Assert ;
41
36
42
37
import static org .springframework .security .oauth2 .server .resource .introspection .OAuth2IntrospectionClaimNames .EXPIRES_AT ;
43
38
import static org .springframework .security .oauth2 .server .resource .introspection .OAuth2IntrospectionClaimNames .ISSUED_AT ;
44
- import static org .springframework .security .oauth2 .server .resource .introspection .OAuth2IntrospectionClaimNames .SCOPE ;
45
39
46
40
/**
47
41
* An {@link AuthenticationProvider} implementation for opaque
65
59
* @since 5.2
66
60
* @see AuthenticationProvider
67
61
*/
68
- public final class OAuth2IntrospectionAuthenticationProvider implements AuthenticationProvider {
62
+ public final class OpaqueTokenAuthenticationProvider implements AuthenticationProvider {
69
63
private static final BearerTokenError DEFAULT_INVALID_TOKEN =
70
64
invalidToken ("An error occurred while attempting to introspect the token: Invalid token" );
71
65
72
- private OpaqueTokenIntrospector introspectionClient ;
66
+ private OpaqueTokenIntrospector introspector ;
73
67
74
68
/**
75
- * Creates a {@code OAuth2IntrospectionAuthenticationProvider } with the provided parameters
69
+ * Creates a {@code OpaqueTokenAuthenticationProvider } with the provided parameters
76
70
*
77
- * @param introspectionClient The {@link OpaqueTokenIntrospector} to use
71
+ * @param introspector The {@link OpaqueTokenIntrospector} to use
78
72
*/
79
- public OAuth2IntrospectionAuthenticationProvider (OpaqueTokenIntrospector introspectionClient ) {
80
- Assert .notNull (introspectionClient , "introspectionClient cannot be null" );
81
- this .introspectionClient = introspectionClient ;
73
+ public OpaqueTokenAuthenticationProvider (OpaqueTokenIntrospector introspector ) {
74
+ Assert .notNull (introspector , "introspector cannot be null" );
75
+ this .introspector = introspector ;
82
76
}
83
77
84
78
/**
@@ -97,15 +91,15 @@ public Authentication authenticate(Authentication authentication) throws Authent
97
91
}
98
92
BearerTokenAuthenticationToken bearer = (BearerTokenAuthenticationToken ) authentication ;
99
93
100
- Map < String , Object > claims ;
94
+ OAuth2AuthenticatedPrincipal principal ;
101
95
try {
102
- claims = this .introspectionClient .introspect (bearer .getToken ());
96
+ principal = this .introspector .introspect (bearer .getToken ());
103
97
} catch (OAuth2IntrospectionException failed ) {
104
98
OAuth2Error invalidToken = invalidToken (failed .getMessage ());
105
99
throw new OAuth2AuthenticationException (invalidToken );
106
100
}
107
101
108
- AbstractAuthenticationToken result = convert (bearer .getToken (), claims );
102
+ AbstractAuthenticationToken result = convert (principal , bearer .getToken ());
109
103
result .setDetails (bearer .getDetails ());
110
104
return result ;
111
105
}
@@ -118,22 +112,12 @@ public boolean supports(Class<?> authentication) {
118
112
return BearerTokenAuthenticationToken .class .isAssignableFrom (authentication );
119
113
}
120
114
121
- private AbstractAuthenticationToken convert (String token , Map < String , Object > claims ) {
122
- Instant iat = ( Instant ) claims . get (ISSUED_AT );
123
- Instant exp = ( Instant ) claims . get (EXPIRES_AT );
124
- OAuth2AccessToken accessToken = new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER ,
115
+ private AbstractAuthenticationToken convert (OAuth2AuthenticatedPrincipal principal , String token ) {
116
+ Instant iat = principal . getAttribute (ISSUED_AT );
117
+ Instant exp = principal . getAttribute (EXPIRES_AT );
118
+ OAuth2AccessToken accessToken = new OAuth2AccessToken (OAuth2AccessToken .TokenType .BEARER ,
125
119
token , iat , exp );
126
- Collection <GrantedAuthority > authorities = extractAuthorities (claims );
127
- return new OAuth2IntrospectionAuthenticationToken (accessToken , new OAuth2TokenAttributes (claims ), authorities );
128
- }
129
-
130
- private Collection <GrantedAuthority > extractAuthorities (Map <String , Object > claims ) {
131
- Collection <String > scopes = (Collection <String >) claims .getOrDefault (SCOPE , Collections .emptyList ());
132
- List <GrantedAuthority > authorities = new ArrayList <>();
133
- for (String scope : scopes ) {
134
- authorities .add (new SimpleGrantedAuthority ("SCOPE_" + scope ));
135
- }
136
- return authorities ;
120
+ return new BearerTokenAuthentication (principal , accessToken , principal .getAuthorities ());
137
121
}
138
122
139
123
private static BearerTokenError invalidToken (String message ) {
0 commit comments