-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For AAD resource-server, create grantedAuthority by both "roles" and …
…"claims" by default. (#19412)
- Loading branch information
1 parent
fdb49c9
commit 0373b50
Showing
3 changed files
with
118 additions
and
15 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
58 changes: 58 additions & 0 deletions
58
...ing-boot/src/main/java/com/azure/spring/aad/webapi/AADJwtGrantedAuthoritiesConverter.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,58 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.spring.aad.webapi; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Extracts the {@link GrantedAuthority}s from scope attributes typically found in a {@link Jwt}. | ||
*/ | ||
public class AADJwtGrantedAuthoritiesConverter implements Converter<Jwt, Collection<GrantedAuthority>> { | ||
|
||
private static final String DEFAULT_SCP_AUTHORITY_PREFIX = "SCOPE_"; | ||
|
||
private static final String DEFAULT_ROLES_AUTHORITY_PREFIX = "APPROLE_"; | ||
|
||
private static final Collection<String> WELL_KNOWN_AUTHORITIES_CLAIM_NAMES = Arrays.asList("scp", "roles"); | ||
|
||
@Override | ||
public Collection<GrantedAuthority> convert(Jwt jwt) { | ||
Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>(); | ||
for (String authority : getAuthorities(jwt)) { | ||
grantedAuthorities.add(new SimpleGrantedAuthority(authority)); | ||
} | ||
return grantedAuthorities; | ||
} | ||
|
||
private Collection<String> getAuthorities(Jwt jwt) { | ||
Collection<String> authoritiesList = new ArrayList<String>(); | ||
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) { | ||
if (jwt.containsClaim(claimName)) { | ||
if (jwt.getClaim(claimName) instanceof String) { | ||
if (StringUtils.hasText(jwt.getClaim(claimName))) { | ||
authoritiesList.addAll(Arrays.asList(((String) jwt.getClaim(claimName)).split(" ")) | ||
.stream() | ||
.map(s -> DEFAULT_SCP_AUTHORITY_PREFIX + s) | ||
.collect(Collectors.toList())); | ||
} | ||
} else if (jwt.getClaim(claimName) instanceof Collection) { | ||
authoritiesList.addAll(((Collection<?>) jwt.getClaim(claimName)) | ||
.stream() | ||
.filter(s -> StringUtils.hasText((String) s)) | ||
.map(s -> DEFAULT_ROLES_AUTHORITY_PREFIX + s) | ||
.collect(Collectors.toList())); | ||
} | ||
} | ||
} | ||
return authoritiesList; | ||
} | ||
} |
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