### Question:
TokenAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> { @Override public AbstractAuthenticationToken convert(Jwt jwt) { return new AuthenticationToken(jwt, authoritiesExtractor.getAuthorities(new XsuaaToken(jwt))); } TokenAuthenticationConverter(AuthoritiesExtractor authoritiesExtractor);  TokenAuthenticationConverter(String appId);  TokenAuthenticationConverter(XsuaaServiceConfiguration xsuaaServiceConfiguration); @Override AbstractAuthenticationToken convert(Jwt jwt); TokenAuthenticationConverter setLocalScopeAsAuthorities(boolean extractLocalScopesOnly);  }### Answer:
@Test public void extractAuthoritiesWithoutScopes() { Jwt jwt = new JwtGenerator().getToken(); AbstractAuthenticationToken authenticationToken = tokenConverterDefault.convert(jwt); assertThat(authenticationToken.getAuthorities().size(), is(0)); }
@Test public void extractAuthoritiesIgnoresForeignScopes() { Jwt jwt = new JwtGenerator().addScopes(scopeAdmin, scopeOther, scopeRead).getToken(); AbstractAuthenticationToken authenticationToken = tokenConverterLocalScopesOnly.convert(jwt); assertThat(authenticationToken.getAuthorities().size(), is(2)); assertThat(authenticationToken.getAuthorities(), not(hasItem(new SimpleGrantedAuthority("Other")))); }
@Test public void extractAuthoritiesWithScopes() { Jwt jwt = new JwtGenerator().addScopes(scopeAdmin, scopeRead, scopeOther).getToken(); AbstractAuthenticationToken authenticationToken = tokenConverterDefault.convert(jwt); assertThat(authenticationToken.getAuthorities().size(), is(3)); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority(scopeRead))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority(scopeAdmin))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority(scopeOther))); }
@Test public void extractCustomAuthoritiesWithScopes() { TokenAuthenticationConverter tokenConverterCustom = new TokenAuthenticationConverter( new MyAuthoritiesExtractor(xsAppName, "cost-center", "country")); Jwt jwt = new JwtGenerator() .addScopes(scopeAdmin) .addAttribute("cost-center", new String[] { "0815" }) .addAttribute("country", new String[] { "DE", "IL" }) .getToken(); AbstractAuthenticationToken authenticationToken = tokenConverterCustom.convert(jwt); assertThat(authenticationToken.getAuthorities().size(), is(4)); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority("ATTR:COST-CENTER=0815"))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority("ATTR:COUNTRY=DE"))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority("ATTR:COUNTRY=IL"))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority(scopeAdmin))); }
@Test public void authoritiesHaveLocalScopesWithoutAppIdPrefix() { String scopeWithNamespace = xsAppName + ".iot.Delete"; String scopeWithOtherAppId = "anyAppId!t200." + xsAppName + ".Delete"; Jwt jwt = new JwtGenerator() .addScopes(xsAppName + "." + scopeAdmin, scopeRead, scopeWithNamespace, scopeWithOtherAppId) .getToken(); AbstractAuthenticationToken authenticationToken = tokenConverterLocalScopesOnly.convert(jwt); assertThat(authenticationToken.getAuthorities().size(), is(3)); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority(scopeAdmin))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority("iot.Delete"))); assertThat(authenticationToken.getAuthorities(), hasItem(new SimpleGrantedAuthority("Read"))); }