Java Code Examples for org.springframework.security.oauth2.provider.OAuth2Authentication#getAuthorities()

The following examples show how to use org.springframework.security.oauth2.provider.OAuth2Authentication#getAuthorities() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoleChecker.java    From multi-tenant-rest-api with MIT License 6 votes vote down vote up
public static boolean hasValidRole(Principal principal, String company, String user) {
	OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
	
	LOGGER.info("Super role is {}", SUPERADMIN);
	
	if (company != null) {
		LOGGER.info("Required company role is {}", String.format(COMPANYADMIN, company.toUpperCase()));
	}
	
	if (user != null) {
		LOGGER.info("Required user role is {}", String.format(USER, user.toUpperCase()));
	}
	
	for(GrantedAuthority ga : oAuth2Authentication.getAuthorities()) {
		LOGGER.info("Checking {}", ga.getAuthority());
		
		if (ga.getAuthority().equalsIgnoreCase(SUPERADMIN)) {
			return true;
		} else if (company != null && ga.getAuthority().equalsIgnoreCase(String.format(COMPANYADMIN, company.toUpperCase()))) {
			return true;
		} else if (user != null && ga.getAuthority().equalsIgnoreCase(String.format(USER, user.toUpperCase()))) {
			return true;
		}
	}
	throw new ResourceUnauthorizedException();
}
 
Example 2
Source File: ShibbolethAcrAwareTokenService.java    From shibboleth-oidc with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate amr and acr claims.
 *
 * @param accessToken the access token
 * @param idClaims    the id claims
 */
private void calculateAmrAndAcrClaims(final OAuth2AccessTokenEntity accessToken,
                                      final JWTClaimsSet.Builder idClaims) {
    final OAuth2Authentication authN = accessToken.getAuthenticationHolder().getAuthentication();
    final Collection<GrantedAuthority> authorities = authN.getAuthorities();
    for (final GrantedAuthority authority : authorities) {
        log.debug("Evaluating authority {} of the authentication", authority);
        final AuthenticationClassRefAuthority acr =
                AuthenticationClassRefAuthority.getAuthenticationClassRefAuthority(authority);
        if (acr != null) {
            idClaims.claim(OIDCConstants.ACR, acr.getAuthority());
            log.debug("Added {} claim as {}", OIDCConstants.ACR, acr.getAuthority());
        }
        final AuthenticationMethodRefAuthority amr =
                AuthenticationMethodRefAuthority.getAuthenticationClassRefAuthority(authority);
        if (amr != null) {
            idClaims.claim(OIDCConstants.AMR, amr.getAuthority());
            log.debug("Added {} claim as {}", OIDCConstants.AMR, amr.getAuthority());
        }
    }
}
 
Example 3
Source File: CustomTokenEnhancer.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    final Map<String, Object> additionalInfo = new HashMap<>();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    Object[] ga = authorities.toArray();
    SimpleGrantedAuthority sga = (SimpleGrantedAuthority) ga[0];
    String role = sga.getAuthority();
    additionalInfo.put("role", role);
    ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    return accessToken;
}
 
Example 4
Source File: CustomTokenEnhancer.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    final Map<String, Object> additionalInfo = new HashMap<>();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    Object[] ga = authorities.toArray();
    SimpleGrantedAuthority sga = (SimpleGrantedAuthority) ga[0];
    String role = sga.getAuthority();
    additionalInfo.put("role", role);
    ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    return accessToken;
}
 
Example 5
Source File: ResourcesController.java    From multi-tenant-rest-api with MIT License 5 votes vote down vote up
@RequestMapping(value="/foo", method=RequestMethod.GET, produces=MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> foo(Principal principal) {
	StringBuilder sb = new StringBuilder();
	OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
	sb.append("Name=");
	sb.append(oAuth2Authentication.getName());
	sb.append("\r\n");
	sb.append("Authorities:");
	for(GrantedAuthority ga : oAuth2Authentication.getAuthorities()) {
		sb.append(ga.getAuthority());
		sb.append("\r\n");
	}
	return new ResponseEntity<String>(sb.toString(), HttpStatus.OK);
}
 
Example 6
Source File: UserServiceImpl.java    From spring-oauth-server with GNU General Public License v2.0 5 votes vote down vote up
private UserJsonDto loadOauthUserJsonDto(OAuth2Authentication oAuth2Authentication) {
    UserJsonDto userJsonDto = new UserJsonDto();
    userJsonDto.setUsername(oAuth2Authentication.getName());

    final Collection<GrantedAuthority> authorities = oAuth2Authentication.getAuthorities();
    for (GrantedAuthority authority : authorities) {
        userJsonDto.getPrivileges().add(authority.getAuthority());
    }

    return userJsonDto;
}
 
Example 7
Source File: OAuth2Configuration.java    From spring-boot-oauth2-jwt with MIT License 4 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
	User user = (User) authentication.getPrincipal();

	Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());

	info.put("email", user.getEmail());

	DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);

	// Get the authorities from the user
	Set<GrantedAuthority> authoritiesSet = new HashSet<>(authentication.getAuthorities());

	// Generate String array
	String[] authorities = new String[authoritiesSet.size()];

	int i = 0;
	for (GrantedAuthority authority : authoritiesSet)
		authorities[i++] = authority.getAuthority();

	info.put("authorities", authorities);
	customAccessToken.setAdditionalInformation(info);

	return super.enhance(customAccessToken, authentication);
}