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.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 w  ww. java 2s . c o 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.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices.java

@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
    this.logger.info("Getting user info from: " + path);
    try {/*w w  w  . j  a va  2s .  com*/
        OAuth2RestOperations restTemplate = this.restTemplate;
        if (restTemplate == null) {
            BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
            resource.setClientId(this.clientId);
            restTemplate = new OAuth2RestTemplate(resource);
        }
        DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
        token.setTokenType(this.tokenType);
        restTemplate.getOAuth2ClientContext().setAccessToken(token);
        return restTemplate.getForEntity(path, Map.class).getBody();
    } catch (Exception ex) {
        this.logger.info("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
        return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
    }
}

From source file:org.springframework.cloud.security.oauth2.resource.UserInfoTokenServices.java

private Map<String, Object> getMap(String path, String accessToken) {
    logger.info("Getting user info from: " + path);
    OAuth2RestOperations restTemplate = this.restTemplate;
    if (restTemplate == null) {
        BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
        resource.setClientId(clientId);/*from w  w  w  .j a v a 2 s  .c  o m*/
        restTemplate = new OAuth2RestTemplate(resource);
    }
    restTemplate.getOAuth2ClientContext().setAccessToken(new DefaultOAuth2AccessToken(accessToken));
    @SuppressWarnings("rawtypes")
    Map map = restTemplate.getForEntity(path, Map.class).getBody();
    @SuppressWarnings("unchecked")
    Map<String, Object> result = map;
    return result;
}

From source file:org.springframework.cloud.security.oauth2.UserInfoTokenServices.java

private Map<String, Object> getMap(String path, String accessToken) {
    logger.info("Getting user info from: " + path);
    BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
    resource.setClientId(clientId);/*from  ww w. j a v  a  2s.  c om*/
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource);
    restTemplate.getOAuth2ClientContext().setAccessToken(new DefaultOAuth2AccessToken(accessToken));
    @SuppressWarnings("rawtypes")
    Map map = restTemplate.getForEntity(path, Map.class).getBody();
    @SuppressWarnings("unchecked")
    Map<String, Object> result = map;
    return result;
}

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  .ja v 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 ww  .  ja  va  2s .c o  m
    }
    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;
}

From source file:ua.com.skywell.oauth.custom.UserInfoTokenServices.java

@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
    this.logger.info("Getting user info from: " + path);
    try {/*from  w ww .j  a  v a  2 s.  c o  m*/
        OAuth2RestOperations restTemplate = this.restTemplate;
        if (restTemplate == null) {
            BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
            resource.setClientId(this.clientId);
            restTemplate = new OAuth2RestTemplate(resource);
        }
        OAuth2AccessToken existingToken = restTemplate.getOAuth2ClientContext().getAccessToken();
        if (existingToken == null || !accessToken.equals(existingToken.getValue())) {
            DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
            token.setTokenType(this.tokenType);
            restTemplate.getOAuth2ClientContext().setAccessToken(token);
        }
        return restTemplate.getForEntity(path, Map.class).getBody();
    } catch (Exception ex) {
        this.logger.info("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
        return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
    }
}