-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add support for Resource Owner Password Credentials grant #7013
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
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
42f1e20
Add support for Resource Owner Password Credentials grant
jgrandja d946cc6
DefaultContextAttributesMapper reads query parameters only
jgrandja d640639
Fix test
jgrandja c7e4efa
PasswordOAuth2AuthorizedClientProvider supports re-authorization
jgrandja 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
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
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
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
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
96 changes: 96 additions & 0 deletions
96
...va/org/springframework/security/oauth2/client/PasswordOAuth2AuthorizedClientProvider.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,96 @@ | ||
/* | ||
* 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.oauth2.client.endpoint.DefaultPasswordTokenResponseClient; | ||
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; | ||
import org.springframework.security.oauth2.client.endpoint.OAuth2PasswordGrantRequest; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* An implementation of an {@link OAuth2AuthorizedClientProvider} | ||
* for the {@link AuthorizationGrantType#PASSWORD password} grant. | ||
* | ||
* @author Joe Grandja | ||
* @since 5.2 | ||
* @see OAuth2AuthorizedClientProvider | ||
* @see DefaultPasswordTokenResponseClient | ||
*/ | ||
public final class PasswordOAuth2AuthorizedClientProvider implements OAuth2AuthorizedClientProvider { | ||
private OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> accessTokenResponseClient = | ||
new DefaultPasswordTokenResponseClient(); | ||
|
||
/** | ||
* Attempt to authorize the {@link OAuth2AuthorizationContext#getClientRegistration() client} in the provided {@code context}. | ||
* Returns {@code null} if authorization is not supported, | ||
* e.g. the client's {@link ClientRegistration#getAuthorizationGrantType() authorization grant type} | ||
* is not {@link AuthorizationGrantType#PASSWORD password} OR the client is already authorized OR | ||
* the {@link OAuth2AuthorizationContext#USERNAME_ATTRIBUTE_NAME username} and/or | ||
* {@link OAuth2AuthorizationContext#PASSWORD_ATTRIBUTE_NAME password} attributes | ||
* are not available in the provided {@code context}. | ||
* | ||
* <p> | ||
* The following {@link OAuth2AuthorizationContext#getAttributes() context attributes} are supported: | ||
* <ol> | ||
* <li>{@link OAuth2AuthorizationContext#USERNAME_ATTRIBUTE_NAME} (required) - a {@code String} value for the resource owner's username</li> | ||
* <li>{@link OAuth2AuthorizationContext#PASSWORD_ATTRIBUTE_NAME} (required) - a {@code String} value for the resource owner's password</li> | ||
* </ol> | ||
* | ||
* @param context the context that holds authorization-specific state for the client | ||
* @return the {@link OAuth2AuthorizedClient} or {@code null} if authorization is not supported | ||
*/ | ||
@Override | ||
@Nullable | ||
public OAuth2AuthorizedClient authorize(OAuth2AuthorizationContext context) { | ||
Assert.notNull(context, "context cannot be null"); | ||
|
||
ClientRegistration clientRegistration = context.getClientRegistration(); | ||
OAuth2AuthorizedClient authorizedClient = context.getAuthorizedClient(); | ||
if (!AuthorizationGrantType.PASSWORD.equals(clientRegistration.getAuthorizationGrantType()) || | ||
authorizedClient != null) { | ||
return null; | ||
} | ||
|
||
String username = context.getAttribute(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME); | ||
String password = context.getAttribute(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME); | ||
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) { | ||
return null; | ||
} | ||
|
||
OAuth2PasswordGrantRequest passwordGrantRequest = | ||
new OAuth2PasswordGrantRequest(clientRegistration, username, password); | ||
OAuth2AccessTokenResponse tokenResponse = | ||
this.accessTokenResponseClient.getTokenResponse(passwordGrantRequest); | ||
|
||
return new OAuth2AuthorizedClient(clientRegistration, context.getPrincipal().getName(), | ||
tokenResponse.getAccessToken(), tokenResponse.getRefreshToken()); | ||
} | ||
|
||
/** | ||
* Sets the client used when requesting an access token credential at the Token Endpoint for the {@code password} grant. | ||
* | ||
* @param accessTokenResponseClient the client used when requesting an access token credential at the Token Endpoint for the {@code password} grant | ||
*/ | ||
public void setAccessTokenResponseClient(OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> accessTokenResponseClient) { | ||
Assert.notNull(accessTokenResponseClient, "accessTokenResponseClient cannot be null"); | ||
this.accessTokenResponseClient = accessTokenResponseClient; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...pringframework/security/oauth2/client/PasswordReactiveOAuth2AuthorizedClientProvider.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,94 @@ | ||
/* | ||
* 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.security.oauth2.client.endpoint.OAuth2PasswordGrantRequest; | ||
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient; | ||
import org.springframework.security.oauth2.client.endpoint.WebClientReactivePasswordTokenResponseClient; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* An implementation of a {@link ReactiveOAuth2AuthorizedClientProvider} | ||
* for the {@link AuthorizationGrantType#PASSWORD password} grant. | ||
* | ||
* @author Joe Grandja | ||
* @since 5.2 | ||
* @see ReactiveOAuth2AuthorizedClientProvider | ||
* @see WebClientReactivePasswordTokenResponseClient | ||
*/ | ||
public final class PasswordReactiveOAuth2AuthorizedClientProvider implements ReactiveOAuth2AuthorizedClientProvider { | ||
private ReactiveOAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> accessTokenResponseClient = | ||
new WebClientReactivePasswordTokenResponseClient(); | ||
|
||
/** | ||
* Attempt to authorize the {@link OAuth2AuthorizationContext#getClientRegistration() client} in the provided {@code context}. | ||
* Returns an empty {@code Mono} if authorization is not supported, | ||
* e.g. the client's {@link ClientRegistration#getAuthorizationGrantType() authorization grant type} | ||
* is not {@link AuthorizationGrantType#PASSWORD password} OR the client is already authorized OR | ||
* the {@link OAuth2AuthorizationContext#USERNAME_ATTRIBUTE_NAME username} and/or | ||
* {@link OAuth2AuthorizationContext#PASSWORD_ATTRIBUTE_NAME password} attributes | ||
* are not available in the provided {@code context}. | ||
* | ||
* <p> | ||
* The following {@link OAuth2AuthorizationContext#getAttributes() context attributes} are supported: | ||
* <ol> | ||
* <li>{@link OAuth2AuthorizationContext#USERNAME_ATTRIBUTE_NAME} (required) - a {@code String} value for the resource owner's username</li> | ||
* <li>{@link OAuth2AuthorizationContext#PASSWORD_ATTRIBUTE_NAME} (required) - a {@code String} value for the resource owner's password</li> | ||
* </ol> | ||
* | ||
* @param context the context that holds authorization-specific state for the client | ||
* @return the {@link OAuth2AuthorizedClient} or an empty {@code Mono} if authorization is not supported | ||
*/ | ||
@Override | ||
public Mono<OAuth2AuthorizedClient> authorize(OAuth2AuthorizationContext context) { | ||
Assert.notNull(context, "context cannot be null"); | ||
|
||
ClientRegistration clientRegistration = context.getClientRegistration(); | ||
OAuth2AuthorizedClient authorizedClient = context.getAuthorizedClient(); | ||
if (!AuthorizationGrantType.PASSWORD.equals(clientRegistration.getAuthorizationGrantType()) || | ||
authorizedClient != null) { | ||
return Mono.empty(); | ||
} | ||
|
||
String username = context.getAttribute(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME); | ||
String password = context.getAttribute(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME); | ||
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) { | ||
return Mono.empty(); | ||
} | ||
|
||
OAuth2PasswordGrantRequest passwordGrantRequest = | ||
new OAuth2PasswordGrantRequest(clientRegistration, username, password); | ||
|
||
return Mono.just(passwordGrantRequest) | ||
.flatMap(this.accessTokenResponseClient::getTokenResponse) | ||
.map(tokenResponse -> new OAuth2AuthorizedClient(clientRegistration, context.getPrincipal().getName(), | ||
tokenResponse.getAccessToken(), tokenResponse.getRefreshToken())); | ||
} | ||
|
||
/** | ||
* Sets the client used when requesting an access token credential at the Token Endpoint for the {@code password} grant. | ||
* | ||
* @param accessTokenResponseClient the client used when requesting an access token credential at the Token Endpoint for the {@code password} grant | ||
*/ | ||
public void setAccessTokenResponseClient(ReactiveOAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> accessTokenResponseClient) { | ||
Assert.notNull(accessTokenResponseClient, "accessTokenResponseClient cannot be null"); | ||
this.accessTokenResponseClient = accessTokenResponseClient; | ||
} | ||
} |
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.
Should it retrieve the OAuth2AuthorizedClient again if the token is expired?
Uh oh!
There was an error while loading. Please reload this page.
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.
The
RefreshTokenOAuth2AuthorizedClientProvider
will take care of this as long as it's configured/composed within theDefaultOAuth2AuthorizedClientManager
- which by default it is configured.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.
If the grant type is PASSWORD doesn't it need to use a OAuth2PasswordGrantRequest to request a new token?
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.
It does use a
OAuth2PasswordGrantRequest
when calling theOAuth2AccessTokenResponseClient
- see code just below this logic.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.
Sorry that was in context of my previous comment. If the token expires shouldn't a new token be requested with a OAuth2PasswordGrantRequest
Uh oh!
There was an error while loading. Please reload this page.
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.
No. For expired access tokens, the
RefreshTokenOAuth2AuthorizedClientProvider
handles this. ThePasswordOAuth2AuthorizedClientProvider
only handles getting a new access token (if client is not authorized)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.
Actually, looks like I missed a condition here. I'll update shortly.
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.
@rwinch Thanks for catching this. I've pushed the updates.