|
| 1 | +package io.github.patternknife.securityhelper.oauth2.api.config.security.converter.auth.endpoint; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.provider.auth.introspectionendpoint.KnifeOauth2OpaqueTokenAuthenticationProvider; |
| 7 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.persistence.authorization.OAuth2AuthorizationServiceImpl; |
| 8 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.userdetail.ConditionalDetailsService; |
| 9 | +import io.github.patternknife.securityhelper.oauth2.api.config.util.KnifeOAuth2EndpointUtils; |
| 10 | +import jakarta.servlet.http.HttpServletRequest; |
| 11 | + |
| 12 | +import org.apache.commons.logging.Log; |
| 13 | +import org.apache.commons.logging.LogFactory; |
| 14 | +import org.springframework.security.core.Authentication; |
| 15 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 16 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 17 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 18 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 19 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 20 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 21 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenIntrospection; |
| 22 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; |
| 23 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2TokenIntrospectionAuthenticationToken; |
| 24 | +import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenIntrospectionEndpointFilter; |
| 25 | + |
| 26 | +import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenAuthenticationConverter; |
| 27 | +import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector; |
| 28 | +import org.springframework.security.web.authentication.AuthenticationConverter; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import org.springframework.util.MultiValueMap; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Attempts to extract an Introspection Request from {@link HttpServletRequest} and then |
| 35 | + * converts it to an {@link OAuth2TokenIntrospectionAuthenticationToken} used for |
| 36 | + * authenticating the request. |
| 37 | + * |
| 38 | + * @author Gerardo Roza |
| 39 | + * @author Joe Grandja |
| 40 | + * @since 0.4.0 |
| 41 | + * @see AuthenticationConverter |
| 42 | + * @see OAuth2TokenIntrospectionAuthenticationToken |
| 43 | + * @see OAuth2TokenIntrospectionEndpointFilter |
| 44 | + */ |
| 45 | +public final class KnifeOAuth2TokenIntrospectionAuthenticationConverter implements AuthenticationConverter { |
| 46 | + |
| 47 | + /* |
| 48 | + * Now, this only takes "access_token". |
| 49 | + * */ |
| 50 | + @Override |
| 51 | + public Authentication convert(HttpServletRequest request) { |
| 52 | + Authentication clientPrincipal = SecurityContextHolder.getContext().getAuthentication(); |
| 53 | + |
| 54 | + MultiValueMap<String, String> parameters = KnifeOAuth2EndpointUtils.getFormParameters(request); |
| 55 | + |
| 56 | + // token (REQUIRED) |
| 57 | + String token = parameters.getFirst(OAuth2ParameterNames.TOKEN); |
| 58 | + if (!StringUtils.hasText(token) || parameters.get(OAuth2ParameterNames.TOKEN).size() != 1) { |
| 59 | + throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.TOKEN); |
| 60 | + } |
| 61 | + |
| 62 | + // token_type_hint (OPTIONAL) |
| 63 | + String tokenTypeHint = parameters.getFirst(OAuth2ParameterNames.TOKEN_TYPE_HINT); |
| 64 | + if (StringUtils.hasText(tokenTypeHint) && parameters.get(OAuth2ParameterNames.TOKEN_TYPE_HINT).size() != 1) { |
| 65 | + throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.TOKEN_TYPE_HINT); |
| 66 | + } |
| 67 | + |
| 68 | + Map<String, Object> additionalParameters = new HashMap<>(); |
| 69 | + parameters.forEach((key, value) -> { |
| 70 | + if (!key.equals(OAuth2ParameterNames.TOKEN) && !key.equals(OAuth2ParameterNames.TOKEN_TYPE_HINT)) { |
| 71 | + additionalParameters.put(key, (value.size() == 1) ? value.get(0) : value.toArray(new String[0])); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + return new OAuth2TokenIntrospectionAuthenticationToken(token, clientPrincipal, tokenTypeHint, |
| 76 | + additionalParameters); |
| 77 | + } |
| 78 | + |
| 79 | + private static void throwError(String errorCode, String parameterName) { |
| 80 | + OAuth2Error error = new OAuth2Error(errorCode, "OAuth 2.0 Token Introspection Parameter: " + parameterName, |
| 81 | + "https://datatracker.ietf.org/doc/html/rfc7662#section-2.1"); |
| 82 | + throw new OAuth2AuthenticationException(error); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments