Skip to content

Commit

Permalink
Merge pull request #65 from jajimene/jwt-changes
Browse files Browse the repository at this point in the history
Modify JWT "scope" claim to hold a List<String>
  • Loading branch information
ssarmokadam authored Dec 18, 2017
2 parents 19b7760 + 696daa9 commit d1d5fdc
Showing 1 changed file with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -54,6 +53,8 @@ public class TokenAuthenticationService {

static final String CLAIM_SCOPE = "scope";

static final String CLAIM_ROLES = "roles";

/**
* This method returns the token once the Authentication has been successful
*
Expand Down Expand Up @@ -110,7 +111,8 @@ static String generateToken(Authentication auth) {
Map<String, Object> claims = new HashMap<>();
claims.put(CLAIM_ISSUER, ISSUER);
claims.put(CLAIM_SUBJECT, auth.getName());
claims.put(CLAIM_SCOPE, auth.getAuthorities());
claims.put(CLAIM_SCOPE, scopes);
claims.put(CLAIM_ROLES, scopes);
claims.put(CLAIM_CREATED, generateCreationDate() / 1000);
claims.put(CLAIM_EXPIRATION, generateExpirationDate() / 1000);
LOG.info(claims.toString());
Expand Down Expand Up @@ -162,15 +164,8 @@ public static UserDetailsClientTo getUserdetailsFromToken(String token) {

static List<String> getRolesFromToken(String token) {

List<LinkedHashMap> scopes = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
.getBody().get(CLAIM_SCOPE, List.class);

List<String> roles = new ArrayList<>();
for (LinkedHashMap<?, ?> scope : scopes) {
roles.add(scope.get("authority").toString());
}

return roles;
return Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody()
.get(CLAIM_SCOPE, List.class);
}

}

0 comments on commit d1d5fdc

Please sign in to comment.