@Test public void customUserPreservedWhenTokenDecoded() { DefaultAccessTokenConverter tokenConverter = new DefaultAccessTokenConverter(); tokenConverter.setUserTokenConverter( new UserAuthenticationConverter() { @Override public Authentication extractAuthentication(Map<String, ?> map) { return new FooAuthentication((String) map.get("user")); } @Override public Map<String, ?> convertUserAuthentication(Authentication userAuthentication) { Map<String, Object> map = new HashMap<String, Object>(); map.put("user", userAuthentication.getName()); map.put("foo", "bar"); return map; } }); jwtTokenEnhancer.setAccessTokenConverter(tokenConverter); OAuth2AccessToken token = tokenServices.createAccessToken(authentication); assertEquals( "bob", tokenServices.loadAuthentication(token.getValue()).getUserAuthentication().getName()); }
@Test public void scopePreservedWhenTokenDecoded() { OAuth2AccessToken token = tokenServices.createAccessToken(authentication); assertEquals( "[read]", tokenServices .loadAuthentication(token.getValue()) .getOAuth2Request() .getScope() .toString()); }
@Before public void init() throws Exception { tokenServices.setClientDetailsService( new InMemoryClientDetailsServiceBuilder() .withClient("client") .authorizedGrantTypes("authorization_code") .scopes("read") .secret("secret") .and() .build()); enhancer.setTokenEnhancers(Arrays.<TokenEnhancer>asList(jwtTokenEnhancer)); jwtTokenEnhancer.afterPropertiesSet(); tokenServices.setTokenStore(new JwtTokenStore(jwtTokenEnhancer)); tokenServices.setTokenEnhancer(enhancer); }
@Test public void additionalInfoPreservedWhenTokenDecoded() { TokenEnhancer info = new TokenEnhancer() { @Override public OAuth2AccessToken enhance( OAuth2AccessToken accessToken, OAuth2Authentication authentication) { DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken); result.getAdditionalInformation().put("foo", "bar"); return result; } }; enhancer.setTokenEnhancers(Arrays.<TokenEnhancer>asList(info, jwtTokenEnhancer)); OAuth2AccessToken token = tokenServices.createAccessToken(authentication); assertEquals("bar", token.getAdditionalInformation().get("foo")); assertEquals( "bar", tokenServices.readAccessToken(token.getValue()).getAdditionalInformation().get("foo")); }
@Test public void scopePreservedWhenTokenCreated() { assertEquals("[read]", tokenServices.createAccessToken(authentication).getScope().toString()); tokenServices.getAccessToken(authentication); }