forked from spring-projects/spring-authorization-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OAuth2TokenEndpointAuthenticationSuccessHandler
Fixes spring-projectsgh-925
- Loading branch information
Showing
6 changed files
with
500 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...ty/oauth2/server/authorization/authentication/OAuth2AccessTokenAuthenticationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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.springframework.security.oauth2.server.authorization.authentication; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; | ||
import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2AccessTokenResponseAuthenticationSuccessHandler; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* An {@link OAuth2AuthenticationContext} that holds an {@link OAuth2AccessTokenResponse.Builder} | ||
* and is used when customizing the building of the {@link OAuth2AccessTokenResponse}. | ||
* | ||
* @author Dmitriy Dubson | ||
* @see OAuth2AuthenticationContext | ||
* @see OAuth2AccessTokenResponse | ||
* @see OAuth2AccessTokenResponseAuthenticationSuccessHandler#setAccessTokenResponseCustomizer(Consumer) | ||
* @since 1.3 | ||
*/ | ||
public class OAuth2AccessTokenAuthenticationContext implements OAuth2AuthenticationContext { | ||
private final Map<Object, Object> context; | ||
|
||
private OAuth2AccessTokenAuthenticationContext(Map<Object, Object> context) { | ||
this.context = Collections.unmodifiableMap(new HashMap<>(context)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Nullable | ||
@Override | ||
public <V> V get(Object key) { | ||
return hasKey(key) ? (V) this.context.get(key) : null; | ||
} | ||
|
||
@Override | ||
public boolean hasKey(Object key) { | ||
Assert.notNull(key, "key cannot be null"); | ||
return this.context.containsKey(key); | ||
} | ||
|
||
/** | ||
* Returns the {@link OAuth2AccessTokenResponse.Builder} access token response builder | ||
* @return the {@link OAuth2AccessTokenResponse.Builder} | ||
*/ | ||
public OAuth2AccessTokenResponse.Builder getAccessTokenResponse() { | ||
return get(OAuth2AccessTokenResponse.Builder.class); | ||
} | ||
|
||
/** | ||
* Constructs a new {@link Builder} with the provided {@link OAuth2AccessTokenAuthenticationToken}. | ||
* | ||
* @param authentication the {@link OAuth2AccessTokenAuthenticationToken} | ||
* @return the {@link Builder} | ||
*/ | ||
public static OAuth2AccessTokenAuthenticationContext.Builder with(OAuth2AccessTokenAuthenticationToken authentication) { | ||
return new OAuth2AccessTokenAuthenticationContext.Builder(authentication); | ||
} | ||
|
||
/** | ||
* A builder for {@link OAuth2AccessTokenAuthenticationContext} | ||
*/ | ||
public static final class Builder extends AbstractBuilder<OAuth2AccessTokenAuthenticationContext, Builder> { | ||
private Builder(OAuth2AccessTokenAuthenticationToken authentication) { | ||
super(authentication); | ||
put(OAuth2AccessTokenAuthenticationToken.class, authentication); | ||
} | ||
|
||
/** | ||
* Sets the {@link OAuth2AccessTokenResponse.Builder} access token response builder | ||
* @param accessTokenResponse the {@link OAuth2AccessTokenResponse.Builder} | ||
* @return the {@link Builder} for further configuration | ||
*/ | ||
public Builder accessTokenResponse(OAuth2AccessTokenResponse.Builder accessTokenResponse) { | ||
return put(OAuth2AccessTokenResponse.Builder.class, accessTokenResponse); | ||
} | ||
|
||
/** | ||
* Builds a new {@link OAuth2AccessTokenAuthenticationContext}. | ||
* | ||
* @return the {@link OAuth2AccessTokenAuthenticationContext} | ||
*/ | ||
public OAuth2AccessTokenAuthenticationContext build() { | ||
Assert.notNull(get(OAuth2AccessTokenResponse.Builder.class), "accessTokenResponse cannot be null"); | ||
Assert.notNull(get(OAuth2AccessTokenAuthenticationToken.class), "accessTokenAuthenticationToken cannot be null"); | ||
|
||
return new OAuth2AccessTokenAuthenticationContext(getContext()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...thorization/web/authentication/OAuth2AccessTokenResponseAuthenticationSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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.springframework.security.oauth2.server.authorization.web.authentication; | ||
|
||
import java.io.IOException; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServletServerHttpResponse; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.core.*; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; | ||
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; | ||
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationContext; | ||
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
/** | ||
* An implementation of an {@link AuthenticationSuccessHandler} used for handling an {@link OAuth2AccessTokenAuthenticationToken} | ||
* and returning the {@link OAuth2AccessTokenResponse Access Token Response}. | ||
* | ||
* @author Dmitriy Dubson | ||
* @see AuthenticationSuccessHandler | ||
* @see OAuth2AccessTokenResponseHttpMessageConverter | ||
* @since 1.3 | ||
*/ | ||
public final class OAuth2AccessTokenResponseAuthenticationSuccessHandler implements AuthenticationSuccessHandler { | ||
private final Log logger = LogFactory.getLog(getClass()); | ||
|
||
private HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenResponseConverter = | ||
new OAuth2AccessTokenResponseHttpMessageConverter(); | ||
|
||
private Consumer<OAuth2AccessTokenAuthenticationContext> accessTokenResponseCustomizer; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
if (!(authentication instanceof OAuth2AccessTokenAuthenticationToken accessTokenAuthentication)) { | ||
if (this.logger.isErrorEnabled()) { | ||
this.logger.error(Authentication.class.getSimpleName() + " must be of type " + | ||
OAuth2AccessTokenAuthenticationToken.class.getName() + | ||
" but was " + authentication.getClass().getName()); | ||
} | ||
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, "Unable to process the access token response.", null); | ||
throw new OAuth2AuthenticationException(error); | ||
} | ||
|
||
OAuth2AccessToken accessToken = accessTokenAuthentication.getAccessToken(); | ||
OAuth2RefreshToken refreshToken = accessTokenAuthentication.getRefreshToken(); | ||
Map<String, Object> additionalParameters = accessTokenAuthentication.getAdditionalParameters(); | ||
|
||
OAuth2AccessTokenResponse.Builder builder = | ||
OAuth2AccessTokenResponse.withToken(accessToken.getTokenValue()) | ||
.tokenType(accessToken.getTokenType()) | ||
.scopes(accessToken.getScopes()); | ||
if (accessToken.getIssuedAt() != null && accessToken.getExpiresAt() != null) { | ||
builder.expiresIn(ChronoUnit.SECONDS.between(accessToken.getIssuedAt(), accessToken.getExpiresAt())); | ||
} | ||
if (refreshToken != null) { | ||
builder.refreshToken(refreshToken.getTokenValue()); | ||
} | ||
if (!CollectionUtils.isEmpty(additionalParameters)) { | ||
builder.additionalParameters(additionalParameters); | ||
} | ||
|
||
if (this.accessTokenResponseCustomizer != null) { | ||
// @formatter:off | ||
OAuth2AccessTokenAuthenticationContext accessTokenAuthenticationContext = | ||
OAuth2AccessTokenAuthenticationContext.with(accessTokenAuthentication) | ||
.accessTokenResponse(builder) | ||
.build(); | ||
// @formatter:on | ||
this.accessTokenResponseCustomizer.accept(accessTokenAuthenticationContext); | ||
if (this.logger.isTraceEnabled()) { | ||
this.logger.trace("Customized access token response"); | ||
} | ||
} | ||
|
||
OAuth2AccessTokenResponse accessTokenResponse = builder.build(); | ||
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); | ||
this.accessTokenResponseConverter.write(accessTokenResponse, null, httpResponse); | ||
} | ||
|
||
/** | ||
* Sets the {@link HttpMessageConverter} used for converting an {@link OAuth2AccessTokenResponse} to an HTTP response. | ||
* | ||
* @param accessTokenResponseConverter the {@link HttpMessageConverter} used for converting an {@link OAuth2AccessTokenResponse} to an HTTP response | ||
*/ | ||
public void setAccessTokenResponseConverter(HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenResponseConverter) { | ||
Assert.notNull(accessTokenResponseConverter, "accessTokenHttpResponseConverter cannot be null"); | ||
this.accessTokenResponseConverter = accessTokenResponseConverter; | ||
} | ||
|
||
/** | ||
* Sets the {@code Consumer} providing access to the {@link OAuth2AccessTokenAuthenticationContext} | ||
* containing an {@link OAuth2AccessTokenResponse.Builder} and additional context information. | ||
* | ||
* @param accessTokenResponseCustomizer the {@code Consumer} providing access to the {@link OAuth2AccessTokenAuthenticationContext} containing an {@link OAuth2AccessTokenResponse.Builder} | ||
*/ | ||
public void setAccessTokenResponseCustomizer(Consumer<OAuth2AccessTokenAuthenticationContext> accessTokenResponseCustomizer) { | ||
Assert.notNull(accessTokenResponseCustomizer, "accessTokenResponseCustomizer cannot be null"); | ||
this.accessTokenResponseCustomizer = accessTokenResponseCustomizer; | ||
} | ||
} |
Oops, something went wrong.