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

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

Introduction

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

Prototype

public void setValue(String value) 

Source Link

Usage

From source file:com.ge.predix.uaa.token.lib.TestTokenUtil.java

private DefaultOAuth2AccessToken createAccessToken(final String issuerId, final String userId,
        final String username, final String userEmail, final int validitySeconds,
        final Collection<GrantedAuthority> clientScopes, final Set<String> requestedScopes,
        final String clientId, final Set<String> resourceIds, final String grantType, final String refreshToken,
        final Map<String, String> additionalAuthorizationAttributes, final Set<String> responseTypes,
        final String revocableHashSignature, final long issuedAtMillis, final String zoneId) {

    String tokenId = UUID.randomUUID().toString();
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(tokenId);
    if (validitySeconds > 0) {
        accessToken.setExpiration(new Date(issuedAtMillis + (validitySeconds * 1000L)));
    }/*from   w ww . j  a  va 2s . c  o  m*/
    accessToken.setRefreshToken(refreshToken == null ? null : new DefaultOAuth2RefreshToken(refreshToken));

    if (null == requestedScopes || requestedScopes.size() == 0) {
        // logger.debug("No scopes were granted");
        throw new InvalidTokenException("No scopes were granted");
    }

    accessToken.setScope(requestedScopes);

    Map<String, Object> info = new HashMap<String, Object>();
    info.put(JTI, accessToken.getValue());
    if (null != additionalAuthorizationAttributes) {
        info.put(ADDITIONAL_AZ_ATTR, additionalAuthorizationAttributes);
    }
    accessToken.setAdditionalInformation(info);

    String content;
    try {
        content = JsonUtils.writeValueAsString(createJWTAccessToken(accessToken, issuerId, userId, username,
                userEmail, clientScopes, requestedScopes, clientId, resourceIds, grantType, refreshToken,
                revocableHashSignature, issuedAtMillis, zoneId));
    } catch (JsonUtils.JsonUtilException e) {
        throw new IllegalStateException("Cannot convert access token to JSON", e);
    }
    String token = JwtHelper.encode(content, this.signer).getEncoded();

    // This setter copies the value and returns. Don't change.
    accessToken.setValue(token);

    return accessToken;

}

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

public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    String tokenId = result.getValue();
    result.setAdditionalInformation(Collections.<String, Object>singletonMap("token_id", tokenId));
    return result.setValue(createAccessTokenValue(accessToken, authentication));
}

From source file:org.springframework.security.oauth2.provider.token.JwtTokenEnhancer.java

public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());
    String tokenId = result.getValue();
    if (!info.containsKey(TOKEN_ID)) {
        info.put(TOKEN_ID, tokenId);//from   www.  j  a  v a  2 s .  c o m
    }
    result.setAdditionalInformation(info);
    return result.setValue(encode(result, authentication));
}

From source file:org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter.java

public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());
    String tokenId = result.getValue();
    if (!info.containsKey(TOKEN_ID)) {
        info.put(TOKEN_ID, tokenId);//from   w  w w.  j a  va  2  s . c om
    }
    result.setAdditionalInformation(info);
    result.setValue(encode(result, authentication));
    OAuth2RefreshToken refreshToken = result.getRefreshToken();
    if (refreshToken != null) {
        DefaultOAuth2AccessToken encodedRefreshToken = new DefaultOAuth2AccessToken(accessToken);
        DefaultOAuth2RefreshToken token = new DefaultOAuth2RefreshToken(
                encode(encodedRefreshToken, authentication));
        if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
            Date expiration = ((ExpiringOAuth2RefreshToken) refreshToken).getExpiration();
            encodedRefreshToken.setExpiration(expiration);
            token = new DefaultExpiringOAuth2RefreshToken(encode(encodedRefreshToken, authentication),
                    expiration);
        }
        result.setRefreshToken(token);
    }
    return result;
}