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