Example usage for org.springframework.security.oauth2.common OAuth2AccessToken getValue

List of usage examples for org.springframework.security.oauth2.common OAuth2AccessToken getValue

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common OAuth2AccessToken getValue.

Prototype

String getValue();

Source Link

Usage

From source file:com.haulmont.restapi.auth.ClientProxyTokenStore.java

@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    String authenticationKey = authenticationKeyGenerator.extractKey(authentication);
    String userLogin = authentication.getName();

    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    Locale locale = restAuthUtils.extractLocaleFromRequestHeader(request);
    String refreshTokenValue = token.getRefreshToken() != null ? token.getRefreshToken().getValue() : null;
    serverTokenStore.storeAccessToken(token.getValue(), serializeAccessToken(token), authenticationKey,
            serializeAuthentication(authentication), token.getExpiration(), userLogin, locale,
            refreshTokenValue);/*from  w  w w . ja  v a2 s . c o  m*/
    processSession(authentication, token.getValue());
    log.info("REST API access token stored: [{}] {}", authentication.getPrincipal(), token.getValue());
}

From source file:org.apigw.authserver.svc.impl.TokenServicesImplTest.java

@Test
@DirtiesContext//from w  w  w. jav a  2s  .c o m
public void testGetClientId() {
    OAuth2Authentication authentication = new OAuth2Authentication(
            createAuthorizationRequest(CLIENT, new HashSet<String>(Arrays.asList(READ_SCOPE))),
            new TestAuthentication(false));
    OAuth2AccessToken accessToken = services.createAccessToken(authentication);

    String clientId = services.getClientId(accessToken.getValue());
    assertEquals(CLIENT, clientId);
}

From source file:org.apigw.authserver.svc.impl.TokenServicesImplTest.java

@Test(expected = InvalidGrantException.class)
@DirtiesContext/*w w w.  java 2s  .c o m*/
public void testRevokeToken() {
    OAuth2Authentication authentication = new OAuth2Authentication(
            createAuthorizationRequest(CLIENT, new HashSet<String>(Arrays.asList(READ_SCOPE))),
            new TestAuthentication(false));
    OAuth2AccessToken accessToken = services.createAccessToken(authentication);

    boolean revoked = services.revokeToken(accessToken.getValue());
    assertTrue(revoked);

    services.getAccessToken(authentication);
}

From source file:com.iflytek.edu.cloud.frame.spring.RedisTokenStore.java

@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
    OAuth2AccessToken accessToken = null;
    String key = authenticationKeyGenerator.extractKey(authentication);
    Jedis jedis = jedisPool.getResource();
    try {// w  w  w.  ja v a2  s  .  co m
        byte[] tokenValue = jedis.hget(ACCESS_AUTH_ID_KEY, toBytes(key));
        if (tokenValue != null)
            accessToken = readAccessToken(new String(tokenValue, Charset.forName("UTF-8")));
    } finally {
        jedisPool.returnResource(jedis);
    }
    if (accessToken != null
            && !key.equals(authenticationKeyGenerator.extractKey(readAuthentication(accessToken.getValue())))) {
        removeAccessToken(accessToken.getValue());
        storeAccessToken(accessToken, authentication);
    }
    return accessToken;
}

From source file:com.create.security.oauth2.provider.token.SpringCacheTokenStoreImpl.java

@Override
public void storeAccessToken(final OAuth2AccessToken token, final OAuth2Authentication authentication) {
    tokenRepository.storeAccessToken(token);
    tokenRepository.storeAuthentication(token, authentication);
    tokenRepository.storeAuthenticationToAccessToken(authentication, token);
    if (!authentication.isClientOnly()) {
        final String userName = authentication.getUserAuthentication() == null ? ""
                : authentication.getUserAuthentication().getName();
        storeTokensByClientIdAndUserName(token, authentication, userName);
    }// w w w .j  a v  a2  s. c  om
    storeTokensByClientId(token, authentication);
    if (token.getRefreshToken() != null && token.getRefreshToken().getValue() != null) {
        tokenRepository.storeRefreshTokenToAccessToken(token.getRefreshToken(), token.getValue());
        tokenRepository.storeAccessTokenToRefreshToken(token, token.getRefreshToken().getValue());
    }
}

From source file:com.cedac.security.oauth2.provider.token.store.TokenStoreBaseTests.java

@Test
public void testStoreAccessTokenTwice() {
    OAuth2Authentication expectedAuthentication = new OAuth2Authentication(
            RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false));
    OAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken("testToken");
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);

    OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken("testToken");
    assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken);
    assertEquals(expectedAuthentication, getTokenStore().readAuthentication(expectedOAuth2AccessToken));
    getTokenStore().removeAccessToken(expectedOAuth2AccessToken);
    assertNull(getTokenStore().readAccessToken("testToken"));
    assertNull(getTokenStore().readAuthentication(expectedOAuth2AccessToken.getValue()));
}

From source file:org.cloudfoundry.identity.uaa.integration.CheckTokenEndpointIntegrationTests.java

@Test
public void testAddidionalAttributes() {
    OAuth2AccessToken accessToken = getUserToken(
            "{\"az_attr\":{\"external_group\":\"domain\\\\group1\",\"external_id\":\"abcd1234\"}}");

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    HttpHeaders tokenHeaders = new HttpHeaders();
    ClientCredentialsResourceDetails resource = testAccounts.getClientCredentialsResource("app", null, "app",
            "appclientsecret");
    tokenHeaders.set("Authorization",
            testAccounts.getAuthorizationHeader(resource.getClientId(), resource.getClientSecret()));
    formData.add("token", accessToken.getValue());

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> tokenResponse = serverRunning.postForMap("/check_token", formData, tokenHeaders);
    assertEquals(HttpStatus.OK, tokenResponse.getStatusCode());
    assertNotNull(tokenResponse.getBody());
    System.out.println(tokenResponse.getBody());

    @SuppressWarnings("unchecked")
    Map<String, String> map = tokenResponse.getBody();
    assertNotNull(map.get("iss"));
    assertEquals(testAccounts.getUserName(), map.get("user_name"));
    assertEquals(testAccounts.getEmail(), map.get("email"));
}

From source file:org.cloudfoundry.identity.uaa.integration.CheckTokenEndpointIntegrationTests.java

@Test
public void testInvalidAddidionalAttributes() {
    OAuth2AccessToken accessToken = getUserToken(
            "{\"az_attr\":{\"external_group\":true,\"external_id\":{\"nested_group\":true,\"nested_id\":1234}} }");

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    HttpHeaders tokenHeaders = new HttpHeaders();
    ClientCredentialsResourceDetails resource = testAccounts.getClientCredentialsResource("app", null, "app",
            "appclientsecret");
    tokenHeaders.set("Authorization",
            testAccounts.getAuthorizationHeader(resource.getClientId(), resource.getClientSecret()));
    formData.add("token", accessToken.getValue());

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> tokenResponse = serverRunning.postForMap("/check_token", formData, tokenHeaders);
    assertEquals(HttpStatus.OK, tokenResponse.getStatusCode());

    @SuppressWarnings("unchecked")
    Map<String, String> map = tokenResponse.getBody();
    assertNull(map.get("az_attr"));
}

From source file:org.apigw.authserver.svc.impl.TokenServicesImplTest.java

@Test
@DirtiesContext/*from   ww w.j ava2s  . c om*/
public void testFindTokensByClientId() {
    OAuth2Authentication authentication = new OAuth2Authentication(
            createAuthorizationRequest(CLIENT, new HashSet<String>(Arrays.asList(READ_SCOPE))),
            new TestAuthentication(false));
    OAuth2AccessToken accessToken = services.createAccessToken(authentication);

    Collection<OAuth2AccessToken> tokens = services.findTokensByClientId(CLIENT);
    assertEquals(1, tokens.size());
    assertEquals(accessToken.getValue(), tokens.iterator().next().getValue());
}

From source file:org.apigw.authserver.svc.impl.TokenServicesImplTest.java

@Test
@DirtiesContext//from  w ww.j  a v a 2  s.com
public void testFindTokensByUsername() {
    OAuth2Authentication authentication = new OAuth2Authentication(
            createAuthorizationRequest(CLIENT, new HashSet<String>(Arrays.asList(READ_SCOPE))),
            new TestAuthentication(false));
    OAuth2AccessToken accessToken = services.createAccessToken(authentication);

    Collection<OAuth2AccessToken> tokens = services.findTokensByUserName("1912121212");
    assertEquals(1, tokens.size());
    assertEquals(accessToken.getValue(), tokens.iterator().next().getValue());
}