-
Notifications
You must be signed in to change notification settings - Fork 6k
OAuth2AuthorizedClientManager implementation works outside of request #7122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jgrandja
wants to merge
2
commits into
spring-projects:master
from
jgrandja:gh-6683-authzclientmgr-outside-req
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
194 changes: 194 additions & 0 deletions
194
...ient/src/main/java/org/springframework/security/oauth2/client/OAuth2AuthorizeRequest.java
This file contains hidden or 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,194 @@ | ||
/* | ||
* Copyright 2002-2019 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.client; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents a request the {@link OAuth2AuthorizedClientManager} uses to | ||
* {@link OAuth2AuthorizedClientManager#authorize(OAuth2AuthorizeRequest) authorize} (or re-authorize) | ||
* the {@link ClientRegistration client} identified by the provided {@link #getClientRegistrationId() clientRegistrationId}. | ||
* | ||
* @author Joe Grandja | ||
* @since 5.2 | ||
* @see OAuth2AuthorizedClientManager | ||
*/ | ||
public final class OAuth2AuthorizeRequest { | ||
private String clientRegistrationId; | ||
private OAuth2AuthorizedClient authorizedClient; | ||
private Authentication principal; | ||
private Map<String, Object> attributes; | ||
|
||
private OAuth2AuthorizeRequest() { | ||
} | ||
|
||
/** | ||
* Returns the identifier for the {@link ClientRegistration client registration}. | ||
* | ||
* @return the identifier for the client registration | ||
*/ | ||
public String getClientRegistrationId() { | ||
return this.clientRegistrationId; | ||
} | ||
|
||
/** | ||
* Returns the {@link OAuth2AuthorizedClient authorized client} or {@code null} if it was not provided. | ||
* | ||
* @return the {@link OAuth2AuthorizedClient} or {@code null} if it was not provided | ||
*/ | ||
@Nullable | ||
public OAuth2AuthorizedClient getAuthorizedClient() { | ||
return this.authorizedClient; | ||
} | ||
|
||
/** | ||
* Returns the {@code Principal} (to be) associated to the authorized client. | ||
* | ||
* @return the {@code Principal} (to be) associated to the authorized client | ||
*/ | ||
public Authentication getPrincipal() { | ||
return this.principal; | ||
} | ||
|
||
/** | ||
* Returns the attributes associated to the request. | ||
* | ||
* @return a {@code Map} of the attributes associated to the request | ||
*/ | ||
public Map<String, Object> getAttributes() { | ||
return this.attributes; | ||
} | ||
|
||
/** | ||
* Returns the value of an attribute associated to the request or {@code null} if not available. | ||
* | ||
* @param name the name of the attribute | ||
* @param <T> the type of the attribute | ||
* @return the value of the attribute associated to the request | ||
*/ | ||
@Nullable | ||
@SuppressWarnings("unchecked") | ||
public <T> T getAttribute(String name) { | ||
return (T) this.getAttributes().get(name); | ||
} | ||
|
||
/** | ||
* Returns a new {@link Builder} initialized with the identifier for the {@link ClientRegistration client registration}. | ||
* | ||
* @param clientRegistrationId the identifier for the {@link ClientRegistration client registration} | ||
* @return the {@link Builder} | ||
*/ | ||
public static Builder withClientRegistrationId(String clientRegistrationId) { | ||
return new Builder(clientRegistrationId); | ||
} | ||
|
||
/** | ||
* Returns a new {@link Builder} initialized with the {@link OAuth2AuthorizedClient authorized client}. | ||
* | ||
* @param authorizedClient the {@link OAuth2AuthorizedClient authorized client} | ||
* @return the {@link Builder} | ||
*/ | ||
public static Builder withAuthorizedClient(OAuth2AuthorizedClient authorizedClient) { | ||
return new Builder(authorizedClient); | ||
} | ||
|
||
/** | ||
* A builder for {@link OAuth2AuthorizeRequest}. | ||
*/ | ||
public static class Builder { | ||
private String clientRegistrationId; | ||
private OAuth2AuthorizedClient authorizedClient; | ||
private Authentication principal; | ||
private Map<String, Object> attributes; | ||
|
||
private Builder(String clientRegistrationId) { | ||
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty"); | ||
this.clientRegistrationId = clientRegistrationId; | ||
} | ||
|
||
private Builder(OAuth2AuthorizedClient authorizedClient) { | ||
Assert.notNull(authorizedClient, "authorizedClient cannot be null"); | ||
this.authorizedClient = authorizedClient; | ||
} | ||
|
||
/** | ||
* Sets the {@code Principal} (to be) associated to the authorized client. | ||
* | ||
* @param principal the {@code Principal} (to be) associated to the authorized client | ||
* @return the {@link Builder} | ||
*/ | ||
public Builder principal(Authentication principal) { | ||
this.principal = principal; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the attributes associated to the request. | ||
* | ||
* @param attributes the attributes associated to the request | ||
* @return the {@link Builder} | ||
*/ | ||
public Builder attributes(Map<String, Object> attributes) { | ||
this.attributes = attributes; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets an attribute associated to the request. | ||
* | ||
* @param name the name of the attribute | ||
* @param value the value of the attribute | ||
* @return the {@link Builder} | ||
*/ | ||
public Builder attribute(String name, Object value) { | ||
if (this.attributes == null) { | ||
this.attributes = new HashMap<>(); | ||
} | ||
this.attributes.put(name, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a new {@link OAuth2AuthorizeRequest}. | ||
* | ||
* @return a {@link OAuth2AuthorizeRequest} | ||
*/ | ||
public OAuth2AuthorizeRequest build() { | ||
Assert.notNull(this.principal, "principal cannot be null"); | ||
OAuth2AuthorizeRequest authorizeRequest = new OAuth2AuthorizeRequest(); | ||
if (this.authorizedClient != null) { | ||
authorizeRequest.clientRegistrationId = this.authorizedClient.getClientRegistration().getRegistrationId(); | ||
authorizeRequest.authorizedClient = this.authorizedClient; | ||
} else { | ||
authorizeRequest.clientRegistrationId = this.clientRegistrationId; | ||
} | ||
authorizeRequest.principal = this.principal; | ||
authorizeRequest.attributes = Collections.unmodifiableMap( | ||
CollectionUtils.isEmpty(this.attributes) ? | ||
Collections.emptyMap() : new LinkedHashMap<>(this.attributes)); | ||
return authorizeRequest; | ||
} | ||
} | ||
} |
This file contains hidden or 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
142 changes: 142 additions & 0 deletions
142
...java/org/springframework/security/oauth2/client/OAuth2AuthorizedClientManagerService.java
This file contains hidden or 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,142 @@ | ||
/* | ||
* Copyright 2002-2019 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.client; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* An implementation of an {@link OAuth2AuthorizedClientManager} | ||
* that is capable of operating outside of a {@code HttpServletRequest} context, | ||
* e.g. in a scheduled/background thread and/or in the service-tier. | ||
* | ||
* @author Joe Grandja | ||
* @since 5.2 | ||
* @see OAuth2AuthorizedClientManager | ||
* @see OAuth2AuthorizedClientProvider | ||
* @see OAuth2AuthorizedClientService | ||
*/ | ||
public final class OAuth2AuthorizedClientManagerService implements OAuth2AuthorizedClientManager { | ||
private final ClientRegistrationRepository clientRegistrationRepository; | ||
private final OAuth2AuthorizedClientService authorizedClientService; | ||
private OAuth2AuthorizedClientProvider authorizedClientProvider = context -> null; | ||
private Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper = new DefaultContextAttributesMapper(); | ||
|
||
/** | ||
* Constructs an {@code OAuth2AuthorizedClientManagerService} using the provided parameters. | ||
* | ||
* @param clientRegistrationRepository the repository of client registrations | ||
* @param authorizedClientService the authorized client service | ||
*/ | ||
public OAuth2AuthorizedClientManagerService(ClientRegistrationRepository clientRegistrationRepository, | ||
OAuth2AuthorizedClientService authorizedClientService) { | ||
Assert.notNull(clientRegistrationRepository, "clientRegistrationRepository cannot be null"); | ||
Assert.notNull(authorizedClientService, "authorizedClientService cannot be null"); | ||
this.clientRegistrationRepository = clientRegistrationRepository; | ||
this.authorizedClientService = authorizedClientService; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public OAuth2AuthorizedClient authorize(OAuth2AuthorizeRequest authorizeRequest) { | ||
Assert.notNull(authorizeRequest, "authorizeRequest cannot be null"); | ||
|
||
String clientRegistrationId = authorizeRequest.getClientRegistrationId(); | ||
OAuth2AuthorizedClient authorizedClient = authorizeRequest.getAuthorizedClient(); | ||
Authentication principal = authorizeRequest.getPrincipal(); | ||
|
||
OAuth2AuthorizationContext.Builder contextBuilder; | ||
if (authorizedClient != null) { | ||
contextBuilder = OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient); | ||
} else { | ||
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId); | ||
Assert.notNull(clientRegistration, "Could not find ClientRegistration with id '" + clientRegistrationId + "'"); | ||
authorizedClient = this.authorizedClientService.loadAuthorizedClient(clientRegistrationId, principal.getName()); | ||
if (authorizedClient != null) { | ||
contextBuilder = OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient); | ||
} else { | ||
contextBuilder = OAuth2AuthorizationContext.withClientRegistration(clientRegistration); | ||
} | ||
} | ||
OAuth2AuthorizationContext authorizationContext = contextBuilder | ||
.principal(principal) | ||
.attributes(this.contextAttributesMapper.apply(authorizeRequest)) | ||
.build(); | ||
|
||
authorizedClient = this.authorizedClientProvider.authorize(authorizationContext); | ||
if (authorizedClient != null) { | ||
this.authorizedClientService.saveAuthorizedClient(authorizedClient, principal); | ||
} else { | ||
// In the case of re-authorization, the returned `authorizedClient` may be null if re-authorization is not supported. | ||
// For these cases, return the provided `authorizationContext.authorizedClient`. | ||
if (authorizationContext.getAuthorizedClient() != null) { | ||
return authorizationContext.getAuthorizedClient(); | ||
} | ||
} | ||
|
||
return authorizedClient; | ||
} | ||
|
||
/** | ||
* Sets the {@link OAuth2AuthorizedClientProvider} used for authorizing (or re-authorizing) an OAuth 2.0 Client. | ||
* | ||
* @param authorizedClientProvider the {@link OAuth2AuthorizedClientProvider} used for authorizing (or re-authorizing) an OAuth 2.0 Client | ||
*/ | ||
public void setAuthorizedClientProvider(OAuth2AuthorizedClientProvider authorizedClientProvider) { | ||
Assert.notNull(authorizedClientProvider, "authorizedClientProvider cannot be null"); | ||
this.authorizedClientProvider = authorizedClientProvider; | ||
} | ||
|
||
/** | ||
* Sets the {@code Function} used for mapping attribute(s) from the {@link OAuth2AuthorizeRequest} to a {@code Map} of attributes | ||
* to be associated to the {@link OAuth2AuthorizationContext#getAttributes() authorization context}. | ||
* | ||
* @param contextAttributesMapper the {@code Function} used for supplying the {@code Map} of attributes | ||
* to the {@link OAuth2AuthorizationContext#getAttributes() authorization context} | ||
*/ | ||
public void setContextAttributesMapper(Function<OAuth2AuthorizeRequest, Map<String, Object>> contextAttributesMapper) { | ||
Assert.notNull(contextAttributesMapper, "contextAttributesMapper cannot be null"); | ||
this.contextAttributesMapper = contextAttributesMapper; | ||
} | ||
|
||
/** | ||
* The default implementation of the {@link #setContextAttributesMapper(Function) contextAttributesMapper}. | ||
*/ | ||
public static class DefaultContextAttributesMapper implements Function<OAuth2AuthorizeRequest, Map<String, Object>> { | ||
|
||
@Override | ||
public Map<String, Object> apply(OAuth2AuthorizeRequest authorizeRequest) { | ||
Map<String, Object> contextAttributes = Collections.emptyMap(); | ||
String scope = authorizeRequest.getAttribute(OAuth2ParameterNames.SCOPE); | ||
if (StringUtils.hasText(scope)) { | ||
contextAttributes = new HashMap<>(); | ||
contextAttributes.put(OAuth2AuthorizationContext.REQUEST_SCOPE_ATTRIBUTE_NAME, | ||
StringUtils.delimitedListToStringArray(scope, " ")); | ||
} | ||
return contextAttributes; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please explain your reasoning for this name? Traditionally, I'd read this as "A class that serves OAuth2AuthorizedClientManagers", kind of like how
OAuth2AuthorizedClientService
servesOAuth2AuthorizedClient
s.Maybe
ServiceOAuth2AuthorizedClientManager
is better since it places the thing that it is (anOAuth2AuthorizedClientManager
) at the end and the thing it's reliant on (theOAuth2AuthorizedClientService
) at the beginning of the name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talked with @jgrandja and looks like we'll rename this to
AuthorizedClientServiceOAuth2AuthorizedClientManager
so that the interface it's implementing goes at the end.