20
20
import org .springframework .security .oauth2 .client .endpoint .OAuth2AccessTokenResponseClient ;
21
21
import org .springframework .security .oauth2 .client .endpoint .OAuth2ClientCredentialsGrantRequest ;
22
22
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
23
- import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
24
- import org .springframework .security .oauth2 .client .web .OAuth2AuthorizedClientRepository ;
25
23
import org .springframework .security .oauth2 .core .AbstractOAuth2Token ;
26
24
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
27
25
import org .springframework .security .oauth2 .core .endpoint .OAuth2AccessTokenResponse ;
28
26
import org .springframework .util .Assert ;
29
27
30
- import javax .servlet .http .HttpServletRequest ;
31
- import javax .servlet .http .HttpServletResponse ;
32
28
import java .time .Duration ;
33
29
import java .time .Instant ;
34
30
42
38
* @see DefaultClientCredentialsTokenResponseClient
43
39
*/
44
40
public final class ClientCredentialsOAuth2AuthorizedClientProvider implements OAuth2AuthorizedClientProvider {
45
- private static final String HTTP_SERVLET_REQUEST_ATTRIBUTE_NAME = HttpServletRequest .class .getName ();
46
- private static final String HTTP_SERVLET_RESPONSE_ATTRIBUTE_NAME = HttpServletResponse .class .getName ();
47
- private final ClientRegistrationRepository clientRegistrationRepository ;
48
- private final OAuth2AuthorizedClientRepository authorizedClientRepository ;
49
41
private OAuth2AccessTokenResponseClient <OAuth2ClientCredentialsGrantRequest > accessTokenResponseClient =
50
42
new DefaultClientCredentialsTokenResponseClient ();
51
43
private Duration clockSkew = Duration .ofSeconds (60 );
52
44
53
- /**
54
- * Constructs a {@code ClientCredentialsOAuth2AuthorizedClientProvider} using the provided parameters.
55
- *
56
- * @param clientRegistrationRepository the repository of client registrations
57
- * @param authorizedClientRepository the repository of authorized clients
58
- */
59
- public ClientCredentialsOAuth2AuthorizedClientProvider (ClientRegistrationRepository clientRegistrationRepository ,
60
- OAuth2AuthorizedClientRepository authorizedClientRepository ) {
61
- Assert .notNull (clientRegistrationRepository , "clientRegistrationRepository cannot be null" );
62
- Assert .notNull (authorizedClientRepository , "authorizedClientRepository cannot be null" );
63
- this .clientRegistrationRepository = clientRegistrationRepository ;
64
- this .authorizedClientRepository = authorizedClientRepository ;
45
+ public ClientCredentialsOAuth2AuthorizedClientProvider () {
65
46
}
66
47
67
48
/**
68
- * Attempt to authorize (or re-authorize) the {@link OAuth2AuthorizationContext#getClientRegistrationId () client} in the provided {@code context}.
49
+ * Attempt to authorize (or re-authorize) the {@link OAuth2AuthorizationContext#getClientRegistration () client} in the provided {@code context}.
69
50
* Returns {@code null} if authorization (or re-authorization) is not supported,
70
51
* e.g. the client's {@link ClientRegistration#getAuthorizationGrantType() authorization grant type}
71
52
* is not {@link AuthorizationGrantType#CLIENT_CREDENTIALS client_credentials} OR
72
53
* the {@link OAuth2AuthorizedClient#getAccessToken() access token} is not expired.
73
54
*
74
- * <p>
75
- * The following {@link OAuth2AuthorizationContext#getAttributes() context attributes} are supported:
76
- * <ol>
77
- * <li>{@code "javax.servlet.http.HttpServletRequest"} (required) - the {@code HttpServletRequest}</li>
78
- * <li>{@code "javax.servlet.http.HttpServletResponse"} (required) - the {@code HttpServletResponse}</li>
79
- * </ol>
80
- *
81
55
* @param context the context that holds authorization-specific state for the client
82
56
* @return the {@link OAuth2AuthorizedClient} or {@code null} if authorization (or re-authorization) is not supported
83
57
*/
@@ -86,22 +60,10 @@ public ClientCredentialsOAuth2AuthorizedClientProvider(ClientRegistrationReposit
86
60
public OAuth2AuthorizedClient authorize (OAuth2AuthorizationContext context ) {
87
61
Assert .notNull (context , "context cannot be null" );
88
62
89
- HttpServletRequest request = context .getAttribute (HTTP_SERVLET_REQUEST_ATTRIBUTE_NAME );
90
- HttpServletResponse response = context .getAttribute (HTTP_SERVLET_RESPONSE_ATTRIBUTE_NAME );
91
- Assert .notNull (request , "The context attribute cannot be null '" + HTTP_SERVLET_REQUEST_ATTRIBUTE_NAME + "'" );
92
- Assert .notNull (response , "The context attribute cannot be null '" + HTTP_SERVLET_RESPONSE_ATTRIBUTE_NAME + "'" );
93
-
94
- String clientRegistrationId = context .getClientRegistrationId ();
95
- ClientRegistration clientRegistration = this .clientRegistrationRepository .findByRegistrationId (clientRegistrationId );
96
- Assert .notNull (clientRegistration , "Could not find ClientRegistration with id '" + clientRegistrationId + "'" );
97
-
98
- if (!AuthorizationGrantType .CLIENT_CREDENTIALS .equals (clientRegistration .getAuthorizationGrantType ())) {
99
- return null ;
100
- }
101
-
102
- OAuth2AuthorizedClient authorizedClient = this .authorizedClientRepository .loadAuthorizedClient (
103
- clientRegistrationId , context .getPrincipal (), request );
104
- if (authorizedClient != null && !hasTokenExpired (authorizedClient .getAccessToken ())) {
63
+ ClientRegistration clientRegistration = context .getClientRegistration ();
64
+ OAuth2AuthorizedClient authorizedClient = context .getAuthorizedClient ();
65
+ if (!AuthorizationGrantType .CLIENT_CREDENTIALS .equals (clientRegistration .getAuthorizationGrantType ()) ||
66
+ (authorizedClient != null && !hasTokenExpired (authorizedClient .getAccessToken ()))) {
105
67
return null ;
106
68
}
107
69
@@ -117,13 +79,7 @@ public OAuth2AuthorizedClient authorize(OAuth2AuthorizationContext context) {
117
79
OAuth2AccessTokenResponse tokenResponse =
118
80
this .accessTokenResponseClient .getTokenResponse (clientCredentialsGrantRequest );
119
81
120
- authorizedClient = new OAuth2AuthorizedClient (
121
- clientRegistration , context .getPrincipal ().getName (), tokenResponse .getAccessToken ());
122
-
123
- this .authorizedClientRepository .saveAuthorizedClient (
124
- authorizedClient , context .getPrincipal (), request , response );
125
-
126
- return authorizedClient ;
82
+ return new OAuth2AuthorizedClient (clientRegistration , context .getPrincipal ().getName (), tokenResponse .getAccessToken ());
127
83
}
128
84
129
85
private boolean hasTokenExpired (AbstractOAuth2Token token ) {
0 commit comments