From 0a058c973a4aba0a6855568e07a4b1decf45f9e0 Mon Sep 17 00:00:00 2001 From: Andreas Falk Date: Sun, 18 Aug 2019 16:40:34 +0200 Subject: [PATCH] Add setter for authorities claim name in JwtGrantedAuthoritiesConverter Prior to this change authorities are always mapped using well known claim names ('scope' or 'scp'). To change this default behaviour the converter had to be replaced completely with a custom one. This commit adds an additional setter to configure a custom claim name like e.g. 'roles'. Without specifying a custom claim name the default claims to be used still remains to the well known ones. This way the authorities can be mapped according to customized token claims. Fixes gh-7100 --- .../JwtGrantedAuthoritiesConverter.java | 19 ++++++++ .../JwtGrantedAuthoritiesConverterTests.java | 43 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java b/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java index 341f7fb1b0..2803483a3e 100644 --- a/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java +++ b/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java @@ -43,6 +43,8 @@ public final class JwtGrantedAuthoritiesConverter implements Converter claims = new HashMap<>(); + claims.put("roles", Arrays.asList("message:read", "message:write")); + claims.put("scope", "missive:read missive:write"); + Jwt jwt = this.jwt(claims); + + JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); + jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles"); + Collection authorities = jwtGrantedAuthoritiesConverter.convert(jwt); + + assertThat(authorities).containsExactly( + new SimpleGrantedAuthority("SCOPE_message:read"), + new SimpleGrantedAuthority("SCOPE_message:write")); + } + + @Test + public void convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() { + Map claims = new HashMap<>(); + claims.put("roles", Collections.emptyList()); + claims.put("scope", "missive:read missive:write"); + Jwt jwt = this.jwt(claims); + + JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); + jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles"); + Collection authorities = jwtGrantedAuthoritiesConverter.convert(jwt); + + assertThat(authorities).isEmpty(); + } + + @Test + public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() { + Map claims = new HashMap<>(); + claims.put("scope", "missive:read missive:write"); + Jwt jwt = this.jwt(claims); + + JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); + jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles"); + Collection authorities = jwtGrantedAuthoritiesConverter.convert(jwt); + + assertThat(authorities).isEmpty(); + } + private Jwt jwt(Map claims) { Map headers = new HashMap<>(); headers.put("alg", JwsAlgorithms.RS256);