-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new AuthoritiesProvider interface to adding custom GrantedAuthori…
…ties Converted existing code to use this new interface (scopes and groups) Fixes: #136
- Loading branch information
Showing
11 changed files
with
225 additions
and
37 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
oauth2/src/main/java/com/okta/spring/boot/oauth/AuthoritiesProvider.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,49 @@ | ||
/* | ||
* Copyright 2019-Present Okta, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.okta.spring.boot.oauth; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Allows for custom {@link GrantedAuthority}s to be added to the current OAuth Principal. Multiple implementations | ||
* are allowed, by default OAuth scopes are converted to Authorities with the format {@code SCOPE_<scope-name>} and | ||
* if a `groups` claim exists in the access or id token, those are converted as well. | ||
* | ||
* Example usage: | ||
* | ||
* <pre><code> | ||
* @Bean | ||
* AuthoritiesProvider myCustomAuthoritiesProvider() { | ||
* return (user, userRequest) -> lookupExtraAuthoritesByName(user.getAttributes().get("email")); | ||
* } | ||
* </code></pre> | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
public interface AuthoritiesProvider { | ||
|
||
Collection<? extends GrantedAuthority> getAuthorities(OAuth2User user, OAuth2UserRequest userRequest); | ||
|
||
default Collection<? extends GrantedAuthority> getAuthorities(OidcUser user, OidcUserRequest userRequest) { | ||
return getAuthorities((OAuth2User) user, userRequest); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
oauth2/src/main/java/com/okta/spring/boot/oauth/AuthorityProvidersConfig.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,37 @@ | ||
/* | ||
* Copyright 2019-Present Okta, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.okta.spring.boot.oauth; | ||
|
||
import com.okta.spring.boot.oauth.config.OktaOAuth2Properties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @since 1.4.0 | ||
*/ | ||
@Configuration | ||
class AuthorityProvidersConfig { | ||
|
||
@Bean | ||
AuthoritiesProvider tokenScopesAuthoritiesProvider() { | ||
return (user, userRequest) -> TokenUtil.tokenScopesToAuthorities(userRequest.getAccessToken()); | ||
} | ||
|
||
@Bean | ||
AuthoritiesProvider groupClaimsAuthoritiesProvider(OktaOAuth2Properties oktaOAuth2Properties) { | ||
return (user, userRequest) -> TokenUtil.tokenClaimsToAuthorities(user.getAttributes(), oktaOAuth2Properties.getGroupsClaim()); | ||
} | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.