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

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

Introduction

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

Prototype

public void setAdditionalInformation(Map<String, Object> additionalInformation) 

Source Link

Document

Additional information that token granters would like to add to the token, e.g.

Usage

From source file:org.osiam.auth.token.OsiamTokenEnhancer.java

@Override
public OAuth2AccessToken enhance(final OAuth2AccessToken accessToken,
        final OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
    Map<String, Object> additionalInformation = new HashMap<>();
    additionalInformation.put("expires_at", token.getExpiration());

    if (token.getRefreshToken() != null) {
        DefaultExpiringOAuth2RefreshToken refreshToken = (DefaultExpiringOAuth2RefreshToken) token
                .getRefreshToken();/*from  w  w w  .ja  va 2 s  . c o m*/
        additionalInformation.put("refresh_token_expires_at", refreshToken.getExpiration());
    }

    additionalInformation.put("client_id", authentication.getOAuth2Request().getClientId());

    if (authentication.getUserAuthentication() != null && authentication.getPrincipal() instanceof User) {
        User user = (User) authentication.getPrincipal();
        additionalInformation.put("user_name", user.getUserName());
        additionalInformation.put("user_id", user.getId());
    }

    token.setAdditionalInformation(additionalInformation);

    return accessToken;
}

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  w  w  .  jav a 2  s .co 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.apigw.authserver.svc.impl.TokenServicesImpl.java

/**
 * Returns a new access token, shallow-copied from the access token contained in the authorization grant.
 * @param grant The authorization grant holding the access token.
 * @param includeAuthorizationGrantId True if the additional information needs to include authorization_grant_id
 * @return An OAuth2AccessToken populated with information from the given authorization grant.
 *//*from www .j  a  va  2 s  .  c  o  m*/
protected OAuth2AccessToken buildAccessTokenFromAuthorizationGrant(AuthorizationGrant grant,
        boolean includeAuthorizationGrantId) {
    log.debug("buildAccessTokenFromAuthorizationGrant");
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(grant.getAccessToken());

    // access token and grant have the same expiry date
    accessToken.setExpiration(grant.getAccessTokenExpires());

    if (supportRefreshToken) {
        accessToken.setRefreshToken(
                new DefaultExpiringOAuth2RefreshToken(grant.getRefreshToken(), grant.getGrantExpires()));
    }
    accessToken.setScope(buildScopeFromAuthorizationGrant(grant));
    accessToken.setTokenType(OAuth2AccessToken.BEARER_TYPE);
    Map<String, Object> additionalInformation = new HashMap<String, Object>();
    additionalInformation.put("issue_date", grant.getIssueDate());
    if (includeAuthorizationGrantId) {
        additionalInformation.put("authorization_grant_id", grant.getId());
    }

    accessToken.setAdditionalInformation(additionalInformation);
    log.debug("Returning from buildAccessTokenFromAuthorizationGrant");
    return accessToken;
}

From source file:org.osiam.auth.token.OsiamCompositeTokenGranter.java

public OAuth2AccessToken grant(String grantType, AuthorizationRequest authorizationRequest) {
    OAuth2AccessToken grant = super.grant(grantType, authorizationRequest);
    if (grant != null) {
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) grant;
        Map<String, Object> additionalInformation = new HashMap<String, Object>();
        additionalInformation.put("access_token", token.getValue());
        additionalInformation.put("expires_at", token.getExpiration());

        StringBuilder scopes = new StringBuilder();
        for (String scopeString : token.getScope()) {
            scopes.append(scopeString).append(" ");
        }//from   ww w  .j a v  a2 s.c  o  m
        additionalInformation.put("scopes", scopes);

        if (token.getRefreshToken() != null) {
            DefaultExpiringOAuth2RefreshToken refreshToken = (DefaultExpiringOAuth2RefreshToken) token
                    .getRefreshToken();
            additionalInformation.put("refresh_token", refreshToken.getValue());
            additionalInformation.put("refresh_token_expires_at", refreshToken.getExpiration());
        }

        additionalInformation.put("token_type", token.getTokenType());
        additionalInformation.put("client_id", authorizationRequest.getClientId());

        OAuth2Authentication auth = tokenServices.loadAuthentication(token.getValue());

        if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
            User user = (User) auth.getPrincipal();
            additionalInformation.put("user_name", user.getUserName());
            additionalInformation.put("user_id", user.getId());
        }

        token.setAdditionalInformation(additionalInformation);
    }
    return grant;
}

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.opentestsystem.shared.security.oauth.resource.SbacTokenConverter.java

@Override
@SuppressWarnings("unchecked")
public OAuth2AccessToken extractAccessToken(final String value, final Map<String, ?> map) {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(value);
    Map<String, Object> info = new HashMap<String, Object>(map);
    info.remove(EXPIRES);/* w  w  w .  j  av a  2  s .co  m*/
    info.remove(AUD);
    info.remove(CLIENT_ID);
    info.remove(SCOPE);
    if (map.containsKey(EXPIRES)) {
        long expires = new Date().getTime() + ((Integer) map.get(EXPIRES) * 1000L);
        token.setExpiration(new Date(expires));
    }
    final Collection<String> scope = (Collection<String>) map.get(SCOPE);
    if (scope != null) {
        token.setScope(Sets.newHashSet(scope));
    }
    token.setAdditionalInformation(info);
    return token;
}

From source file:org.orcid.core.oauth.service.OrcidTokenStoreServiceImpl.java

private OAuth2AccessToken getOauth2AccessTokenFromDetails(OrcidOauth2TokenDetail detail) {
    DefaultOAuth2AccessToken token = null;
    if (detail != null && StringUtils.isNotBlank(detail.getTokenValue())) {
        token = new DefaultOAuth2AccessToken(detail.getTokenValue());
        token.setExpiration(detail.getTokenExpiration());
        token.setScope(OAuth2Utils.parseParameterList(detail.getScope()));
        token.setTokenType(detail.getTokenType());
        String refreshToken = detail.getRefreshTokenValue();
        OAuth2RefreshToken rt;/*from   ww w .  j  a  v  a 2s. co m*/
        if (StringUtils.isNotBlank(refreshToken)) {
            if (detail.getRefreshTokenExpiration() != null) {
                rt = new DefaultExpiringOAuth2RefreshToken(detail.getRefreshTokenValue(),
                        detail.getRefreshTokenExpiration());
            } else {
                rt = new DefaultOAuth2RefreshToken(detail.getRefreshTokenValue());
            }
            token.setRefreshToken(rt);
        }
        ProfileEntity profile = detail.getProfile();
        if (profile != null) {
            Map<String, Object> additionalInfo = new HashMap<String, Object>();
            additionalInfo.put(OrcidOauth2Constants.ORCID, profile.getId());
            additionalInfo.put(OrcidOauth2Constants.PERSISTENT, detail.isPersistent());
            additionalInfo.put(OrcidOauth2Constants.DATE_CREATED, detail.getDateCreated());
            additionalInfo.put(OrcidOauth2Constants.TOKEN_VERSION, detail.getVersion());
            token.setAdditionalInformation(additionalInfo);
        }

        String clientId = detail.getClientDetailsId();
        if (!PojoUtil.isEmpty(clientId)) {
            Map<String, Object> additionalInfo = new HashMap<String, Object>();
            Map<String, Object> additionalInfoInToken = token.getAdditionalInformation();
            if (additionalInfoInToken != null && !additionalInfoInToken.isEmpty()) {
                additionalInfo.putAll(additionalInfoInToken);
            }
            // Copy to a new one to avoid unmodifiable  
            additionalInfo.put(OrcidOauth2Constants.CLIENT_ID, clientId);
            token.setAdditionalInformation(additionalInfo);
        }
    }

    return token;
}

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  ww w  .j  av a 2  s  .  co  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  v a 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;
}