|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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.client; |
| 17 | + |
| 18 | +import org.springframework.lang.Nullable; |
| 19 | +import org.springframework.security.oauth2.client.endpoint.DefaultClientCredentialsTokenResponseClient; |
| 20 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
| 21 | +import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest; |
| 22 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 23 | +import org.springframework.security.oauth2.core.AbstractOAuth2Token; |
| 24 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 25 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +import java.time.Duration; |
| 29 | +import java.time.Instant; |
| 30 | + |
| 31 | +/** |
| 32 | + * An implementation of an {@link OAuth2AuthorizedClientProvider} |
| 33 | + * for the {@link AuthorizationGrantType#CLIENT_CREDENTIALS client_credentials} grant. |
| 34 | + * |
| 35 | + * @author Joe Grandja |
| 36 | + * @since 5.2 |
| 37 | + * @see OAuth2AuthorizedClientProvider |
| 38 | + * @see DefaultClientCredentialsTokenResponseClient |
| 39 | + */ |
| 40 | +public final class ClientCredentialsOAuth2AuthorizedClientProvider implements OAuth2AuthorizedClientProvider { |
| 41 | + private OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> accessTokenResponseClient = |
| 42 | + new DefaultClientCredentialsTokenResponseClient(); |
| 43 | + private Duration clockSkew = Duration.ofSeconds(60); |
| 44 | + |
| 45 | + /** |
| 46 | + * Attempt to authorize (or re-authorize) the {@link OAuth2AuthorizationContext#getClientRegistration() client} in the provided {@code context}. |
| 47 | + * Returns {@code null} if authorization (or re-authorization) is not supported, |
| 48 | + * e.g. the client's {@link ClientRegistration#getAuthorizationGrantType() authorization grant type} |
| 49 | + * is not {@link AuthorizationGrantType#CLIENT_CREDENTIALS client_credentials} OR |
| 50 | + * the {@link OAuth2AuthorizedClient#getAccessToken() access token} is not expired. |
| 51 | + * |
| 52 | + * @param context the context that holds authorization-specific state for the client |
| 53 | + * @return the {@link OAuth2AuthorizedClient} or {@code null} if authorization (or re-authorization) is not supported |
| 54 | + */ |
| 55 | + @Override |
| 56 | + @Nullable |
| 57 | + public OAuth2AuthorizedClient authorize(OAuth2AuthorizationContext context) { |
| 58 | + Assert.notNull(context, "context cannot be null"); |
| 59 | + |
| 60 | + ClientRegistration clientRegistration = context.getClientRegistration(); |
| 61 | + if (!AuthorizationGrantType.CLIENT_CREDENTIALS.equals(clientRegistration.getAuthorizationGrantType())) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + OAuth2AuthorizedClient authorizedClient = context.getAuthorizedClient(); |
| 66 | + if (authorizedClient != null && !hasTokenExpired(authorizedClient.getAccessToken())) { |
| 67 | + // If client is already authorized but access token is NOT expired than no need for re-authorization |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + // As per spec, in section 4.4.3 Access Token Response |
| 72 | + // https://tools.ietf.org/html/rfc6749#section-4.4.3 |
| 73 | + // A refresh token SHOULD NOT be included. |
| 74 | + // |
| 75 | + // Therefore, renewing an expired access token (re-authorization) |
| 76 | + // is the same as acquiring a new access token (authorization). |
| 77 | + |
| 78 | + OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest = |
| 79 | + new OAuth2ClientCredentialsGrantRequest(clientRegistration); |
| 80 | + OAuth2AccessTokenResponse tokenResponse = |
| 81 | + this.accessTokenResponseClient.getTokenResponse(clientCredentialsGrantRequest); |
| 82 | + |
| 83 | + return new OAuth2AuthorizedClient(clientRegistration, context.getPrincipal().getName(), tokenResponse.getAccessToken()); |
| 84 | + } |
| 85 | + |
| 86 | + private boolean hasTokenExpired(AbstractOAuth2Token token) { |
| 87 | + return token.getExpiresAt().isBefore(Instant.now().minus(this.clockSkew)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Sets the client used when requesting an access token credential at the Token Endpoint for the {@code client_credentials} grant. |
| 92 | + * |
| 93 | + * @param accessTokenResponseClient the client used when requesting an access token credential at the Token Endpoint for the {@code client_credentials} grant |
| 94 | + */ |
| 95 | + public void setAccessTokenResponseClient(OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> accessTokenResponseClient) { |
| 96 | + Assert.notNull(accessTokenResponseClient, "accessTokenResponseClient cannot be null"); |
| 97 | + this.accessTokenResponseClient = accessTokenResponseClient; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Sets the maximum acceptable clock skew, which is used when checking the |
| 102 | + * {@link OAuth2AuthorizedClient#getAccessToken() access token} expiry. The default is 60 seconds. |
| 103 | + * An access token is considered expired if it's before {@code Instant.now() - clockSkew}. |
| 104 | + * |
| 105 | + * @param clockSkew the maximum acceptable clock skew |
| 106 | + */ |
| 107 | + public void setClockSkew(Duration clockSkew) { |
| 108 | + Assert.notNull(clockSkew, "clockSkew cannot be null"); |
| 109 | + Assert.isTrue(clockSkew.getSeconds() >= 0, "clockSkew must be >= 0"); |
| 110 | + this.clockSkew = clockSkew; |
| 111 | + } |
| 112 | +} |
0 commit comments