@Test public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() { Map<String, Object> claims = new HashMap<>(); claims.put("scp", Arrays.asList("message:read", "message:write")); claims.put("scope", "missive:read missive:write"); Jwt jwt = this.jwt(claims); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("SCOPE_missive:read"), new SimpleGrantedAuthority("SCOPE_missive:write")); }
@Test public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() { Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write")); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write")); }
@Test public void convertWhenDefaultGrantedAuthoritiesConverterSet() { Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write")); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write")); }
@Test public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() { Jwt jwt = this.jwt(Collections.singletonMap("scp", Arrays.asList("message:read", "message:write"))); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write")); }
@Test public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() { Map<String, Object> claims = new HashMap<>(); claims.put("scp", Arrays.asList("message:read", "message:write")); claims.put("scope", ""); Jwt jwt = this.jwt(claims); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly(); }
@Test public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() { Jwt jwt = this.jwt(Collections.singletonMap("scp", Arrays.asList())); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly(); }
@Test public void convertWithOverriddenGrantedAuthoritiesConverter() { Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write")); Converter<Jwt, Collection<GrantedAuthority>> grantedAuthoritiesConverter = token -> Arrays.asList(new SimpleGrantedAuthority("blah")); this.jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("blah")); }
@Test public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() { Jwt jwt = this.jwt(Collections.singletonMap("scope", "")); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly(); }
@Test public void convertWhenDefaultGrantedAuthoritiesConverterSet() { Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write")); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("SCOPE_message:read"), new SimpleGrantedAuthority("SCOPE_message:write")); }
@Test public void convertWithOverriddenGrantedAuthoritiesConverter() { Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write")); Converter<Jwt, Flux<GrantedAuthority>> grantedAuthoritiesConverter = token -> Flux.just(new SimpleGrantedAuthority("blah")); this.jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter); AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block(); Collection<GrantedAuthority> authorities = authentication.getAuthorities(); assertThat(authorities).containsExactly( new SimpleGrantedAuthority("blah")); }