|
| 1 | +/* |
| 2 | + * Copyright 2020-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 | +package org.springframework.security.oauth2.server.authorization.web.authentication; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.time.temporal.ChronoUnit; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Consumer; |
| 22 | + |
| 23 | +import jakarta.servlet.ServletException; |
| 24 | +import jakarta.servlet.http.HttpServletRequest; |
| 25 | +import jakarta.servlet.http.HttpServletResponse; |
| 26 | +import org.apache.commons.logging.Log; |
| 27 | +import org.apache.commons.logging.LogFactory; |
| 28 | +import org.springframework.http.converter.HttpMessageConverter; |
| 29 | +import org.springframework.http.server.ServletServerHttpResponse; |
| 30 | +import org.springframework.security.core.Authentication; |
| 31 | +import org.springframework.security.oauth2.core.*; |
| 32 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 33 | +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; |
| 34 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationContext; |
| 35 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; |
| 36 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
| 37 | +import org.springframework.util.Assert; |
| 38 | +import org.springframework.util.CollectionUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * An implementation of an {@link AuthenticationSuccessHandler} used for handling an {@link OAuth2AccessTokenAuthenticationToken} |
| 42 | + * and returning the {@link OAuth2AccessTokenResponse Access Token Response}. |
| 43 | + * |
| 44 | + * @author Dmitriy Dubson |
| 45 | + * @see AuthenticationSuccessHandler |
| 46 | + * @see OAuth2AccessTokenResponseHttpMessageConverter |
| 47 | + * @since 1.3 |
| 48 | + */ |
| 49 | +public final class OAuth2AccessTokenResponseAuthenticationSuccessHandler implements AuthenticationSuccessHandler { |
| 50 | + private final Log logger = LogFactory.getLog(getClass()); |
| 51 | + |
| 52 | + private HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenResponseConverter = |
| 53 | + new OAuth2AccessTokenResponseHttpMessageConverter(); |
| 54 | + |
| 55 | + private Consumer<OAuth2AccessTokenAuthenticationContext> accessTokenResponseCustomizer; |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { |
| 59 | + if (!(authentication instanceof OAuth2AccessTokenAuthenticationToken accessTokenAuthentication)) { |
| 60 | + if (this.logger.isErrorEnabled()) { |
| 61 | + this.logger.error(Authentication.class.getSimpleName() + " must be of type " + |
| 62 | + OAuth2AccessTokenAuthenticationToken.class.getName() + |
| 63 | + " but was " + authentication.getClass().getName()); |
| 64 | + } |
| 65 | + OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, "Unable to process the access token response.", null); |
| 66 | + throw new OAuth2AuthenticationException(error); |
| 67 | + } |
| 68 | + |
| 69 | + OAuth2AccessToken accessToken = accessTokenAuthentication.getAccessToken(); |
| 70 | + OAuth2RefreshToken refreshToken = accessTokenAuthentication.getRefreshToken(); |
| 71 | + Map<String, Object> additionalParameters = accessTokenAuthentication.getAdditionalParameters(); |
| 72 | + |
| 73 | + OAuth2AccessTokenResponse.Builder builder = |
| 74 | + OAuth2AccessTokenResponse.withToken(accessToken.getTokenValue()) |
| 75 | + .tokenType(accessToken.getTokenType()) |
| 76 | + .scopes(accessToken.getScopes()); |
| 77 | + if (accessToken.getIssuedAt() != null && accessToken.getExpiresAt() != null) { |
| 78 | + builder.expiresIn(ChronoUnit.SECONDS.between(accessToken.getIssuedAt(), accessToken.getExpiresAt())); |
| 79 | + } |
| 80 | + if (refreshToken != null) { |
| 81 | + builder.refreshToken(refreshToken.getTokenValue()); |
| 82 | + } |
| 83 | + if (!CollectionUtils.isEmpty(additionalParameters)) { |
| 84 | + builder.additionalParameters(additionalParameters); |
| 85 | + } |
| 86 | + |
| 87 | + if (this.accessTokenResponseCustomizer != null) { |
| 88 | + // @formatter:off |
| 89 | + OAuth2AccessTokenAuthenticationContext accessTokenAuthenticationContext = |
| 90 | + OAuth2AccessTokenAuthenticationContext.with(accessTokenAuthentication) |
| 91 | + .accessTokenResponse(builder) |
| 92 | + .build(); |
| 93 | + // @formatter:on |
| 94 | + this.accessTokenResponseCustomizer.accept(accessTokenAuthenticationContext); |
| 95 | + if (this.logger.isTraceEnabled()) { |
| 96 | + this.logger.trace("Customized access token response"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + OAuth2AccessTokenResponse accessTokenResponse = builder.build(); |
| 101 | + ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); |
| 102 | + this.accessTokenResponseConverter.write(accessTokenResponse, null, httpResponse); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets the {@link HttpMessageConverter} used for converting an {@link OAuth2AccessTokenResponse} to an HTTP response. |
| 107 | + * |
| 108 | + * @param accessTokenResponseConverter the {@link HttpMessageConverter} used for converting an {@link OAuth2AccessTokenResponse} to an HTTP response |
| 109 | + */ |
| 110 | + public void setAccessTokenResponseConverter(HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenResponseConverter) { |
| 111 | + Assert.notNull(accessTokenResponseConverter, "accessTokenHttpResponseConverter cannot be null"); |
| 112 | + this.accessTokenResponseConverter = accessTokenResponseConverter; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the {@code Consumer} providing access to the {@link OAuth2AccessTokenAuthenticationContext} |
| 117 | + * containing an {@link OAuth2AccessTokenResponse.Builder} and additional context information. |
| 118 | + * |
| 119 | + * @param accessTokenResponseCustomizer the {@code Consumer} providing access to the {@link OAuth2AccessTokenAuthenticationContext} containing an {@link OAuth2AccessTokenResponse.Builder} |
| 120 | + */ |
| 121 | + public void setAccessTokenResponseCustomizer(Consumer<OAuth2AccessTokenAuthenticationContext> accessTokenResponseCustomizer) { |
| 122 | + Assert.notNull(accessTokenResponseCustomizer, "accessTokenResponseCustomizer cannot be null"); |
| 123 | + this.accessTokenResponseCustomizer = accessTokenResponseCustomizer; |
| 124 | + } |
| 125 | +} |
0 commit comments