|
| 1 | +/* |
| 2 | + * Copyright 2020 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.server.authorization.authentication; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.time.temporal.ChronoUnit; |
| 20 | +import java.util.Base64; |
| 21 | + |
| 22 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 23 | +import org.springframework.security.core.Authentication; |
| 24 | +import org.springframework.security.core.AuthenticationException; |
| 25 | +import org.springframework.security.crypto.keygen.Base64StringKeyGenerator; |
| 26 | +import org.springframework.security.crypto.keygen.StringKeyGenerator; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 29 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 30 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 31 | + |
| 32 | +/** |
| 33 | + * An {@link AuthenticationProvider} that converts give {@link OAuth2ClientAuthenticationToken} to a {@link OAuth2AccessTokenAuthenticationToken} |
| 34 | + * using the provided token generator. |
| 35 | + * |
| 36 | + * @author Alexey Nesterov |
| 37 | + */ |
| 38 | +public class OAuth2ClientCredentialsAuthenticationProvider implements AuthenticationProvider { |
| 39 | + |
| 40 | + private final StringKeyGenerator accessTokenGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder()); |
| 41 | + |
| 42 | + public OAuth2ClientCredentialsAuthenticationProvider() { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 48 | + OAuth2ClientCredentialsAuthenticationToken clientCredentialsAuthenticationToken = |
| 49 | + (OAuth2ClientCredentialsAuthenticationToken) authentication; |
| 50 | + |
| 51 | + OAuth2ClientAuthenticationToken clientPrincipal = null; |
| 52 | + if (OAuth2ClientAuthenticationToken.class.isAssignableFrom(clientCredentialsAuthenticationToken.getPrincipal().getClass())) { |
| 53 | + clientPrincipal = (OAuth2ClientAuthenticationToken) clientCredentialsAuthenticationToken.getPrincipal(); |
| 54 | + } |
| 55 | + |
| 56 | + if (clientPrincipal == null || !clientPrincipal.isAuthenticated()) { |
| 57 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT)); |
| 58 | + } |
| 59 | + |
| 60 | + String tokenValue = this.accessTokenGenerator.generateKey(); |
| 61 | + Instant issuedAt = Instant.now(); |
| 62 | + Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS); // TODO Allow configuration for access token lifespan |
| 63 | + OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, |
| 64 | + tokenValue, issuedAt, expiresAt, clientCredentialsAuthenticationToken.getScopes()); |
| 65 | + |
| 66 | + return new OAuth2AccessTokenAuthenticationToken( |
| 67 | + clientPrincipal.getRegisteredClient(), clientPrincipal, accessToken); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean supports(Class<?> authentication) { |
| 72 | + return OAuth2ClientCredentialsAuthenticationToken.class.isAssignableFrom(authentication); |
| 73 | + } |
| 74 | +} |
0 commit comments