Example usage for org.springframework.security.oauth2.common DefaultOAuth2AccessToken DefaultOAuth2AccessToken

List of usage examples for org.springframework.security.oauth2.common DefaultOAuth2AccessToken DefaultOAuth2AccessToken

Introduction

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

Prototype

public DefaultOAuth2AccessToken(OAuth2AccessToken accessToken) 

Source Link

Document

Copy constructor for access token.

Usage

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test
public void testRevokeTokenForUserWithTokenId() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setAdditionalInformation(Collections.<String, Object>singletonMap(JwtTokenEnhancer.TOKEN_ID, "BAR"));
    Mockito.when(tokenServices.findTokensByUserName("marissa"))
            .thenReturn(Collections.<OAuth2AccessToken>singleton(token));
    Mockito.when(tokenServices.revokeToken("FOO")).thenReturn(true);
    SimpleMessage result = endpoints.revokeUserToken("marissa", "BAR",
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals("ok", result.getStatus());
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test(expected = NoSuchTokenException.class)
public void testRevokeInvalidTokenForUser() throws Exception {
    OAuth2AccessToken token = new DefaultOAuth2AccessToken("BAR");
    Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(Collections.singleton(token));
    SimpleMessage result = endpoints.revokeUserToken("marissa", "FOO",
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals("ok", result.getStatus());
}

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

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

    Collection<OAuth2AccessToken> actualOAuth2AccessTokens = getTokenStore()
            .findTokensByClientIdAndUserName("id", "test2");
    assertEquals(1, actualOAuth2AccessTokens.size());
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpoints.java

private Collection<OAuth2AccessToken> enhance(Collection<OAuth2AccessToken> tokens) {
    Collection<OAuth2AccessToken> result = new ArrayList<OAuth2AccessToken>();
    for (OAuth2AccessToken prototype : tokens) {
        DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(prototype);
        Map<String, Object> map = new HashMap<String, Object>(token.getAdditionalInformation());
        if (!map.containsKey(JwtTokenEnhancer.TOKEN_ID)) {
            // The token doesn't have an ID in the token service, but we need one for the endpoint, so add one here
            map.put(JwtTokenEnhancer.TOKEN_ID, encoder.encode(token.getValue()));
        }//from  w  ww.j av a  2 s  .com
        try {
            String clientId = tokenServices.getClientId(token.getValue());
            if (clientId != null) {
                map.put("client_id", clientId);
            }
        } catch (InvalidTokenException e) {
            // Ignore defensively in case of bugs in token services
        }
        token.setAdditionalInformation(map);
        result.add(token);
    }
    return result;
}

From source file:com.onedrive.api.OneDrive.java

private OAuth2AccessToken getOAuth2AccessToken() {
    if (existingToken != null) {
        DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(existingToken.getAccessToken());
        if (existingToken.getRefreshToken() != null) {
            accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(existingToken.getRefreshToken()));
        }/*from   w  w  w. j a  va2 s  .com*/
        accessToken.setExpiration(existingToken.getExpiration());
        accessToken.setScope(existingToken.getScope());
        accessToken.setTokenType(existingToken.getTokenType());
        return accessToken;
    }
    return null;
}

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

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

    Collection<OAuth2AccessToken> actualOAuth2AccessTokens = getTokenStore().findTokensByClientId("id");
    assertEquals(1, actualOAuth2AccessTokens.size());
}

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

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

    OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken("testToken");
    assertNotNull(actualOAuth2AccessToken.getRefreshToken());

    assertNull(getTokenStore().readRefreshToken("refreshToken"));
}

From source file:it.smartcommunitylab.aac.oauth.NonRemovingTokenServices.java

private OAuth2AccessToken createAccessToken(OAuth2Authentication authentication,
        OAuth2RefreshToken refreshToken) {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(UUID.randomUUID().toString());
    int validitySeconds = getAccessTokenValiditySeconds(authentication.getOAuth2Request());

    if (!authentication.isClientOnly()) {

        token.setExpiration(new Date(System.currentTimeMillis()
                + (getUserAccessTokenValiditySeconds(authentication.getOAuth2Request()) * 1000L)));
    } else if (validitySeconds > 0) {
        token.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));
    } else {//w  w  w .j  av a  2  s .co  m
        token.setExpiration(new Date(Long.MAX_VALUE));
    }

    token.setRefreshToken(refreshToken);
    token.setScope(authentication.getOAuth2Request().getScope());

    logger.info("Created token " + token.getValue() + " expires at " + token.getExpiration());
    return tokenEnhancer != null ? tokenEnhancer.enhance(token, authentication) : token;
}