Skip to content

Commit

Permalink
Pass authorities through from claims in client credentials token
Browse files Browse the repository at this point in the history
A client credentials token has authorities, but these were not being
passed on from the claims to the OAuthAuthentication in the
DefaultAccessTokenConverter.

Fixes spring-atticgh-431
  • Loading branch information
Dave Syer committed Mar 17, 2015
1 parent 8a1fb13 commit 9823898
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
Expand Down Expand Up @@ -131,7 +132,14 @@ public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
@SuppressWarnings("unchecked")
Set<String> resourceIds = new LinkedHashSet<String>(map.containsKey(AUD) ? (Collection<String>) map.get(AUD)
: Collections.<String>emptySet());
OAuth2Request request = new OAuth2Request(parameters, clientId, null, true, scope, resourceIds, null, null,

Collection<? extends GrantedAuthority> authorities = null;
if (user==null && map.containsKey(AUTHORITIES)) {
@SuppressWarnings("unchecked")
String[] roles = ((Collection<String>)map.get(AUTHORITIES)).toArray(new String[0]);
authorities = AuthorityUtils.createAuthorityList(roles);
}
OAuth2Request request = new OAuth2Request(parameters, clientId, authorities, true, scope, resourceIds, null, null,
null);
return new OAuth2Authentication(request, user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void extractAuthentication() {
assertEquals(singleton(ROLE_USER), map.get(AccessTokenConverter.AUTHORITIES));
OAuth2Authentication extracted = converter.extractAuthentication(map);
assertTrue(extracted.getOAuth2Request().getResourceIds().contains("resource"));
assertEquals("[ROLE_USER]", extracted.getAuthorities().toString());
}

@Test
Expand All @@ -77,6 +78,7 @@ public void extractAuthenticationFromClientToken() {
assertEquals(singleton(ROLE_CLIENT), map.get(AccessTokenConverter.AUTHORITIES));
OAuth2Authentication extracted = converter.extractAuthentication(map);
assertTrue(extracted.getOAuth2Request().getResourceIds().contains("resource"));
assertEquals("[ROLE_CLIENT]", extracted.getAuthorities().toString());
}

}

0 comments on commit 9823898

Please sign in to comment.