IntrospectionAuthenticationProvider.java

/*
 * Copyright 2022 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.spring.security;

import org.genesys.spring.security.service.JwtTokenIdExtractor;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import org.springframework.security.oauth2.core.OAuth2Token;
import org.springframework.security.oauth2.core.OAuth2TokenIntrospectionClaimNames;
import org.springframework.security.oauth2.core.converter.ClaimConversionService;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.OAuth2TokenIntrospection;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenIntrospectionAuthenticationToken;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IntrospectionAuthenticationProvider implements AuthenticationProvider {
	private static final TypeDescriptor OBJECT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Object.class);
	private static final TypeDescriptor LIST_STRING_TYPE_DESCRIPTOR =
		TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
	private final RegisteredClientRepository registeredClientRepository;
	private final OAuth2AuthorizationService authorizationService;
	private final JwtTokenIdExtractor jwtTokenIdExtractor;
	
	public IntrospectionAuthenticationProvider(RegisteredClientRepository registeredClientRepository,
		OAuth2AuthorizationService authorizationService, JwtTokenIdExtractor jwtTokenIdExtractor) {
		Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null");
		Assert.notNull(authorizationService, "authorizationService cannot be null");
		this.registeredClientRepository = registeredClientRepository;
		this.authorizationService = authorizationService;
		this.jwtTokenIdExtractor = jwtTokenIdExtractor;
	}

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		OAuth2TokenIntrospectionAuthenticationToken tokenIntrospectionAuthentication =
			(OAuth2TokenIntrospectionAuthenticationToken) authentication;

		OAuth2ClientAuthenticationToken clientPrincipal = null;
		if (OAuth2ClientAuthenticationToken.class.isAssignableFrom(authentication.getPrincipal().getClass())) {
			clientPrincipal = (OAuth2ClientAuthenticationToken) authentication.getPrincipal();
		}
		if (clientPrincipal == null || !clientPrincipal.isAuthenticated()) {
			throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT);
		}

		OAuth2Authorization authorization = this.authorizationService.findByToken(
			tokenIntrospectionAuthentication.getToken(), null);
		if (authorization == null) {
			// Return the authentication request when token not found
			return tokenIntrospectionAuthentication;
		}

		OAuth2Authorization.Token<OAuth2Token> authorizedToken = getToken(tokenIntrospectionAuthentication.getToken(), authorization);
		if (!authorizedToken.isActive()) {
			return new OAuth2TokenIntrospectionAuthenticationToken(tokenIntrospectionAuthentication.getToken(),
				clientPrincipal, OAuth2TokenIntrospection.builder().build());
		}

		RegisteredClient authorizedClient = this.registeredClientRepository.findById(authorization.getRegisteredClientId());
		OAuth2TokenIntrospection tokenClaims = withActiveTokenClaims(authorizedToken, authorizedClient);

		return new OAuth2TokenIntrospectionAuthenticationToken(authorizedToken.getToken().getTokenValue(),
			clientPrincipal, tokenClaims);
	}

	private  <T extends OAuth2Token> OAuth2Authorization.Token<T> getToken(String tokenValue, OAuth2Authorization authorization) {
		Assert.hasText(tokenValue, "tokenValue cannot be empty");
		String tokenId = jwtTokenIdExtractor.getJwtTokenId(tokenValue);
		if (tokenId == null) {
			return null;
		}
		OAuth2Authorization.Token<T> token = (OAuth2Authorization.Token<T>) authorization.getAccessToken();
		if (token != null && token.getToken().getTokenValue().equals(tokenId)) {
			return token;
		}

		token = (OAuth2Authorization.Token<T>) authorization.getToken(OAuth2RefreshToken.class);
		if (token != null && token.getToken().getTokenValue().equals(tokenId)) {
			return token;
		}

		token = (OAuth2Authorization.Token<T>) authorization.getToken(OidcIdToken.class);
		if (token != null && token.getToken().getTokenValue().equals(tokenId)) {
			return token;
		}
		
		return null;
	}

	@Override
	public boolean supports(Class<?> authentication) {
		return OAuth2TokenIntrospectionAuthenticationToken.class.isAssignableFrom(authentication);
	}

	private static OAuth2TokenIntrospection withActiveTokenClaims(
		OAuth2Authorization.Token<OAuth2Token> authorizedToken, RegisteredClient authorizedClient) {

		OAuth2TokenIntrospection.Builder tokenClaims;
		if (!CollectionUtils.isEmpty(authorizedToken.getClaims())) {
			Map<String, Object> claims = convertClaimsIfNecessary(authorizedToken.getClaims());
			tokenClaims = OAuth2TokenIntrospection.withClaims(claims).active(true);
		} else {
			tokenClaims = OAuth2TokenIntrospection.builder(true);
		}

		tokenClaims.clientId(authorizedClient.getClientId());

		OAuth2Token token = authorizedToken.getToken();
		if (token.getIssuedAt() != null) {
			tokenClaims.issuedAt(token.getIssuedAt());
		}
		if (token.getExpiresAt() != null) {
			tokenClaims.expiresAt(token.getExpiresAt());
		}

		if (OAuth2AccessToken.class.isAssignableFrom(token.getClass())) {
			OAuth2AccessToken accessToken = (OAuth2AccessToken) token;
			tokenClaims.tokenType(accessToken.getTokenType().getValue());
		}

		return tokenClaims.build();
	}

	private static Map<String, Object> convertClaimsIfNecessary(Map<String, Object> claims) {
		Map<String, Object> convertedClaims = new HashMap<>(claims);

		Object value = claims.get(OAuth2TokenIntrospectionClaimNames.ISS);
		if (value != null && !(value instanceof URL)) {
			URL convertedValue = ClaimConversionService.getSharedInstance()
				.convert(value, URL.class);
			if (convertedValue != null) {
				convertedClaims.put(OAuth2TokenIntrospectionClaimNames.ISS, convertedValue);
			}
		}

		value = claims.get(OAuth2TokenIntrospectionClaimNames.SCOPE);
		if (value != null && !(value instanceof List)) {
			Object convertedValue = ClaimConversionService.getSharedInstance()
				.convert(value, OBJECT_TYPE_DESCRIPTOR, LIST_STRING_TYPE_DESCRIPTOR);
			if (convertedValue != null) {
				convertedClaims.put(OAuth2TokenIntrospectionClaimNames.SCOPE, convertedValue);
			}
		}

		value = claims.get(OAuth2TokenIntrospectionClaimNames.AUD);
		if (value != null && !(value instanceof List)) {
			Object convertedValue = ClaimConversionService.getSharedInstance()
				.convert(value, OBJECT_TYPE_DESCRIPTOR, LIST_STRING_TYPE_DESCRIPTOR);
			if (convertedValue != null) {
				convertedClaims.put(OAuth2TokenIntrospectionClaimNames.AUD, convertedValue);
			}
		}

		return convertedClaims;
	}
}