|
| 1 | +/* |
| 2 | + * Copyright 2020-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 | +package org.springframework.security.oauth2.server.authorization.oidc.authentication; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | + |
| 22 | +import org.springframework.http.converter.HttpMessageConverter; |
| 23 | +import org.springframework.http.server.ServletServerHttpRequest; |
| 24 | +import org.springframework.security.core.Authentication; |
| 25 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 29 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 30 | +import org.springframework.security.oauth2.core.oidc.OidcClientRegistration; |
| 31 | +import org.springframework.security.oauth2.core.oidc.http.converter.OidcClientRegistrationHttpMessageConverter; |
| 32 | +import org.springframework.security.oauth2.server.authorization.oidc.web.OidcClientRegistrationEndpointFilter; |
| 33 | +import org.springframework.security.web.authentication.AuthenticationConverter; |
| 34 | +import org.springframework.security.web.util.matcher.AndRequestMatcher; |
| 35 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 36 | +import org.springframework.util.StringUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * Attempts to extract an OpenID Connect Dynamic Client Registration 1.0 Request (or OpenID Connect Client Configuration 1.0 Request) from {@link HttpServletRequest} |
| 40 | + * for the OIDC Client Registration or Configuration flows and then converts it to |
| 41 | + * an {@link OidcClientRegistrationAuthenticationToken} used for authenticating the request. |
| 42 | + * |
| 43 | + * @author Ovidiu Popa |
| 44 | + * @since 0.2.1 |
| 45 | + * @see AuthenticationConverter |
| 46 | + * @see OidcClientRegistrationAuthenticationToken |
| 47 | + * @see OidcClientRegistrationEndpointFilter |
| 48 | + */ |
| 49 | +public class OidcClientRegistrationAuthenticationConverter implements AuthenticationConverter { |
| 50 | + |
| 51 | + private final HttpMessageConverter<OidcClientRegistration> clientRegistrationHttpMessageConverter = |
| 52 | + new OidcClientRegistrationHttpMessageConverter(); |
| 53 | + |
| 54 | + private static final RequestMatcher OIDC_CLIENT_CONFIGURATION_REQUEST_MATCHER = createOidcClientConfigurationRequestMatcher(); |
| 55 | + |
| 56 | + @Override |
| 57 | + public Authentication convert(HttpServletRequest request) { |
| 58 | + |
| 59 | + |
| 60 | + Authentication principal = SecurityContextHolder.getContext().getAuthentication(); |
| 61 | + |
| 62 | + if ("POST".equals(request.getMethod())) { |
| 63 | + return getOidcClientRegistrationAuthentication(request, principal); |
| 64 | + } |
| 65 | + if (OIDC_CLIENT_CONFIGURATION_REQUEST_MATCHER.matches(request)) { |
| 66 | + return getOidcClientConfigurationAuthentication(request, principal); |
| 67 | + } |
| 68 | + |
| 69 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST)); |
| 70 | + } |
| 71 | + |
| 72 | + private OidcClientRegistrationAuthenticationToken getOidcClientConfigurationAuthentication(HttpServletRequest request, Authentication principal) { |
| 73 | + String clientId = request.getParameter(OAuth2ParameterNames.CLIENT_ID); |
| 74 | + String[] clientIdParameters = request.getParameterValues(OAuth2ParameterNames.CLIENT_ID); |
| 75 | + if (!StringUtils.hasText(clientId) || clientIdParameters.length != 1) { |
| 76 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_CLIENT); |
| 77 | + } |
| 78 | + return |
| 79 | + OidcClientRegistrationAuthenticationToken.with(principal) |
| 80 | + .clientRegistrationRequest(false) |
| 81 | + .clientId(clientId) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + private Authentication getOidcClientRegistrationAuthentication(HttpServletRequest request, Authentication principal) { |
| 86 | + try { |
| 87 | + OidcClientRegistration clientRegistration = this.clientRegistrationHttpMessageConverter.read( |
| 88 | + OidcClientRegistration.class, new ServletServerHttpRequest(request)); |
| 89 | + return |
| 90 | + OidcClientRegistrationAuthenticationToken.with(principal) |
| 91 | + .clientRegistrationRequest(true) |
| 92 | + .clientRegistration(clientRegistration) |
| 93 | + .build(); |
| 94 | + } catch (IOException ex) { |
| 95 | + OAuth2Error error = new OAuth2Error( |
| 96 | + OAuth2ErrorCodes.INVALID_REQUEST, |
| 97 | + "OpenID Client Registration Error: " + ex.getMessage(), |
| 98 | + "https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationError"); |
| 99 | + throw new OAuth2AuthenticationException(error); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static RequestMatcher createOidcClientConfigurationRequestMatcher() { |
| 104 | + RequestMatcher clientConfigurationRequestGetMatcher = request -> "GET".equals(request.getMethod()); |
| 105 | + |
| 106 | + RequestMatcher clientIdMatcher = request -> { |
| 107 | + String clientId = request.getParameter(OAuth2ParameterNames.CLIENT_ID); |
| 108 | + return StringUtils.hasText(clientId); |
| 109 | + }; |
| 110 | + |
| 111 | + return |
| 112 | + new AndRequestMatcher(clientConfigurationRequestGetMatcher, clientIdMatcher); |
| 113 | + |
| 114 | + } |
| 115 | +} |
0 commit comments