|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 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 | + |
| 17 | +package org.springframework.security.oauth2.client; |
| 18 | + |
| 19 | +import java.time.Clock; |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.Instant; |
| 22 | + |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | +import org.springframework.security.oauth2.client.endpoint.DefaultJwtBearerTokenResponseClient; |
| 25 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
| 26 | +import org.springframework.security.oauth2.client.endpoint.OAuth2JwtBearerGrantRequest; |
| 27 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 28 | +import org.springframework.security.oauth2.core.AbstractOAuth2Token; |
| 29 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 30 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 31 | +import org.springframework.util.Assert; |
| 32 | + |
| 33 | +/** |
| 34 | + * An implementation of an {@link OAuth2AuthorizedClientProvider} for the |
| 35 | + * {@link OAuth2JwtBearerGrantRequest#JWT_BEARER_GRANT_TYPE jwt-bearer} grant. |
| 36 | + * |
| 37 | + * @author Joe Grandja |
| 38 | + * @since 5.5 |
| 39 | + * @see OAuth2AuthorizedClientProvider |
| 40 | + * @see DefaultJwtBearerTokenResponseClient |
| 41 | + */ |
| 42 | +public final class JwtBearerOAuth2AuthorizedClientProvider implements OAuth2AuthorizedClientProvider { |
| 43 | + |
| 44 | + private OAuth2AccessTokenResponseClient<OAuth2JwtBearerGrantRequest> accessTokenResponseClient = new DefaultJwtBearerTokenResponseClient(); |
| 45 | + |
| 46 | + private Duration clockSkew = Duration.ofSeconds(60); |
| 47 | + |
| 48 | + private Clock clock = Clock.systemUTC(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Attempt to authorize the {@link OAuth2AuthorizationContext#getClientRegistration() |
| 52 | + * client} in the provided {@code context}. Returns {@code null} if authorization is |
| 53 | + * not supported, e.g. the client's |
| 54 | + * {@link ClientRegistration#getAuthorizationGrantType() authorization grant type} is |
| 55 | + * not {@link OAuth2JwtBearerGrantRequest#JWT_BEARER_GRANT_TYPE jwt-bearer}. |
| 56 | + * @param context the context that holds authorization-specific state for the client |
| 57 | + * @return the {@link OAuth2AuthorizedClient} or {@code null} if authorization is not |
| 58 | + * supported |
| 59 | + */ |
| 60 | + @Override |
| 61 | + @Nullable |
| 62 | + public OAuth2AuthorizedClient authorize(OAuth2AuthorizationContext context) { |
| 63 | + Assert.notNull(context, "context cannot be null"); |
| 64 | + |
| 65 | + ClientRegistration clientRegistration = context.getClientRegistration(); |
| 66 | + if (!OAuth2JwtBearerGrantRequest.JWT_BEARER_GRANT_TYPE.equals(clientRegistration.getAuthorizationGrantType())) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + Jwt jwt = context.getAttribute(OAuth2AuthorizationContext.JWT_ATTRIBUTE_NAME); |
| 71 | + if (jwt == null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + OAuth2AuthorizedClient authorizedClient = context.getAuthorizedClient(); |
| 76 | + if (authorizedClient != null && !hasTokenExpired(authorizedClient.getAccessToken())) { |
| 77 | + // If client is already authorized but access token is NOT expired than no |
| 78 | + // need for re-authorization |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + OAuth2JwtBearerGrantRequest jwtBearerGrantRequest = new OAuth2JwtBearerGrantRequest(clientRegistration, jwt); |
| 83 | + OAuth2AccessTokenResponse tokenResponse = this.accessTokenResponseClient |
| 84 | + .getTokenResponse(jwtBearerGrantRequest); |
| 85 | + |
| 86 | + return new OAuth2AuthorizedClient(clientRegistration, context.getPrincipal().getName(), |
| 87 | + tokenResponse.getAccessToken()); |
| 88 | + } |
| 89 | + |
| 90 | + private boolean hasTokenExpired(AbstractOAuth2Token token) { |
| 91 | + return this.clock.instant().isAfter(token.getExpiresAt().minus(this.clockSkew)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Sets the client used when requesting an access token credential at the Token |
| 96 | + * Endpoint for the {@code jwt-bearer} grant. |
| 97 | + * @param accessTokenResponseClient the client used when requesting an access token |
| 98 | + * credential at the Token Endpoint for the {@code jwt-bearer} grant |
| 99 | + */ |
| 100 | + public void setAccessTokenResponseClient( |
| 101 | + OAuth2AccessTokenResponseClient<OAuth2JwtBearerGrantRequest> accessTokenResponseClient) { |
| 102 | + Assert.notNull(accessTokenResponseClient, "accessTokenResponseClient cannot be null"); |
| 103 | + this.accessTokenResponseClient = accessTokenResponseClient; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets the maximum acceptable clock skew, which is used when checking the |
| 108 | + * {@link OAuth2AuthorizedClient#getAccessToken() access token} expiry. The default is |
| 109 | + * 60 seconds. An access token is considered expired if it's before |
| 110 | + * {@code Instant.now(this.clock) - clockSkew}. |
| 111 | + * @param clockSkew the maximum acceptable clock skew |
| 112 | + */ |
| 113 | + public void setClockSkew(Duration clockSkew) { |
| 114 | + Assert.notNull(clockSkew, "clockSkew cannot be null"); |
| 115 | + Assert.isTrue(clockSkew.getSeconds() >= 0, "clockSkew must be >= 0"); |
| 116 | + this.clockSkew = clockSkew; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the {@link Clock} used in {@link Instant#now(Clock)} when checking the access |
| 121 | + * token expiry. |
| 122 | + * @param clock the clock |
| 123 | + */ |
| 124 | + public void setClock(Clock clock) { |
| 125 | + Assert.notNull(clock, "clock cannot be null"); |
| 126 | + this.clock = clock; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments