Example usage for org.springframework.security.oauth2.provider.token AccessTokenConverter extractAuthentication

List of usage examples for org.springframework.security.oauth2.provider.token AccessTokenConverter extractAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider.token AccessTokenConverter extractAuthentication.

Prototype

OAuth2Authentication extractAuthentication(Map<String, ?> map);

Source Link

Document

Recover an OAuth2Authentication from the converted access token.

Usage

From source file:com.netflix.genie.web.security.oauth2.pingfederate.PingFederateRemoteTokenServicesUnitTests.java

/**
 * Make sure we can validate a token./*ww  w  . j a va 2  s. c o m*/
 */
@Test
public void canLoadAuthentication() {
    final AccessTokenConverter converter = Mockito.mock(AccessTokenConverter.class);
    final RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
    final PingFederateRemoteTokenServices services = new PingFederateRemoteTokenServices(
            this.resourceServerProperties, converter, this.registry);
    services.setRestTemplate(restTemplate);
    final String accessToken = UUID.randomUUID().toString();

    final String clientId = UUID.randomUUID().toString();
    final String scope1 = UUID.randomUUID().toString();
    final String scope2 = UUID.randomUUID().toString();

    final Map<String, Object> map = Maps.newHashMap();
    map.put(PingFederateRemoteTokenServices.CLIENT_ID_KEY, clientId);
    map.put(PingFederateRemoteTokenServices.SCOPE_KEY, scope1 + " " + scope2);

    @SuppressWarnings("unchecked")
    final ResponseEntity<Map> response = Mockito.mock(ResponseEntity.class);

    Mockito.when(restTemplate.exchange(Mockito.eq(CHECK_TOKEN_ENDPOINT_URL), Mockito.eq(HttpMethod.POST),
            Mockito.any(HttpEntity.class), Mockito.eq(Map.class))).thenReturn(response);

    Mockito.when(response.getBody()).thenReturn(map);

    final SimpleGrantedAuthority scope1Authority = new SimpleGrantedAuthority(scope1);
    final SimpleGrantedAuthority scope2Authority = new SimpleGrantedAuthority(scope2);
    final Set<GrantedAuthority> authorities = Sets.newHashSet(scope1Authority, scope2Authority);

    final Authentication authentication = new UsernamePasswordAuthenticationToken(clientId, "NA", authorities);

    final OAuth2Authentication oauth2Authentication = new OAuth2Authentication(
            Mockito.mock(OAuth2Request.class), authentication);

    Mockito.when(converter.extractAuthentication(Mockito.eq(map))).thenReturn(oauth2Authentication);

    final OAuth2Authentication result = services.loadAuthentication(accessToken);
    Assert.assertThat(result, Matchers.is(oauth2Authentication));
    Assert.assertThat(result.getPrincipal(), Matchers.is(clientId));
    Assert.assertThat(result.getAuthorities().size(), Matchers.is(2));
    Assert.assertTrue(result.getAuthorities().contains(scope1Authority));
    Assert.assertTrue(result.getAuthorities().contains(scope2Authority));
    Mockito.verify(this.authenticationTimer, Mockito.times(1)).record(Mockito.anyLong(),
            Mockito.eq(TimeUnit.NANOSECONDS));
}