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

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

Introduction

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

Prototype

public void setTokenType(String tokenType) 

Source Link

Document

The token type, as introduced in draft 11 of the OAuth 2 spec.

Usage

From source file:org.springframework.security.oauth2.common.OAuth2AccessTokenJackson2Deserializer.java

@Override
public OAuth2AccessToken deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    String tokenValue = null;// w  ww. j av  a 2  s. co  m
    String tokenType = null;
    String refreshToken = null;
    Long expiresIn = null;
    Set<String> scope = null;
    Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();

    // TODO What should occur if a parameter exists twice
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String name = jp.getCurrentName();
        jp.nextToken();
        if (OAuth2AccessToken.ACCESS_TOKEN.equals(name)) {
            tokenValue = jp.getText();
        } else if (OAuth2AccessToken.TOKEN_TYPE.equals(name)) {
            tokenType = jp.getText();
        } else if (OAuth2AccessToken.REFRESH_TOKEN.equals(name)) {
            refreshToken = jp.getText();
        } else if (OAuth2AccessToken.EXPIRES_IN.equals(name)) {
            try {
                expiresIn = jp.getLongValue();
            } catch (JsonParseException e) {
                expiresIn = Long.valueOf(jp.getText());
            }
        } else if (OAuth2AccessToken.SCOPE.equals(name)) {
            String text = jp.getText();
            scope = OAuth2Utils.parseParameterList(text);
        } else {
            additionalInformation.put(name, jp.readValueAs(Object.class));
        }
    }

    // TODO What should occur if a required parameter (tokenValue or tokenType) is missing?

    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(tokenValue);
    accessToken.setTokenType(tokenType);
    if (expiresIn != null) {
        accessToken.setExpiration(new Date(System.currentTimeMillis() + (expiresIn * 1000)));
    }
    if (refreshToken != null) {
        accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(refreshToken));
    }
    accessToken.setScope(scope);
    accessToken.setAdditionalInformation(additionalInformation);

    return accessToken;
}

From source file:org.osiam.resource_server.security.authorization.AccessTokenValidationService.java

@Override
public OAuth2AccessToken readAccessToken(String token) {
    AccessToken accessToken = validateAccessToken(token);

    Set<String> scopes = new HashSet<String>();
    for (Scope scope : accessToken.getScopes()) {
        scopes.add(scope.toString());/*from ww w .ja  va  2 s .c o  m*/
    }

    DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(token);
    oAuth2AccessToken.setScope(scopes);
    oAuth2AccessToken.setExpiration(accessToken.getExpiresAt());
    oAuth2AccessToken.setTokenType("BEARER");

    return oAuth2AccessToken;
}

From source file:com.onedrive.api.internal.InternalTokenServices.java

public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource,
        Authentication authentication) {
    if (reference.getAccessTokenListener() != null) {
        AccessToken internalAccessToken = reference.getAccessTokenListener().onAccessTokenRequired(reference);
        if (internalAccessToken != null) {
            DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(
                    internalAccessToken.getAccessToken());
            accessToken.setExpiration(internalAccessToken.getExpiration());
            accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(internalAccessToken.getRefreshToken()));
            accessToken.setScope(internalAccessToken.getScope());
            accessToken.setTokenType(internalAccessToken.getTokenType());
            return accessToken;
        }/*from w w w.  j  ava2  s.  com*/
    }
    return null;
}

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 v a 2  s  . co  m*/
        accessToken.setExpiration(existingToken.getExpiration());
        accessToken.setScope(existingToken.getScope());
        accessToken.setTokenType(existingToken.getTokenType());
        return accessToken;
    }
    return null;
}

From source file:com.climate.oada.security.oauth.TokenServicesImpl.java

@Override
public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
    LOG.debug("Generating OAuth Token for principal: " + authentication.getName());
    DefaultOAuth2AccessToken result = null;
    String randomUUID = UUID.randomUUID().toString();
    MessageDigest md = null;/*from  w w  w . j av a2 s  . co  m*/
    try {
        byte[] emailBytes = authentication.getName().getBytes("UTF-8");
        md = MessageDigest.getInstance("MD5");
        String emailDigest = new String(Base64.encodeBase64(md.digest(emailBytes)));
        String token = randomUUID + emailDigest;
        result = new DefaultOAuth2AccessToken(token);
        int validitySeconds = getAccessTokenValiditySeconds(authentication.getAuthorizationRequest());
        if (validitySeconds > 0) {
            result.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * SEC_TO_MSEC)));
        }
        result.setTokenType("Bearer");
        tokenStore.storeAccessToken(result, authentication);
    } catch (Exception e) {
        LOG.error("Unable to generate OAuth token: " + e.getMessage());
    }
    return result;
}

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 ww  w  . j av  a  2s  .  c om*/
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.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 w w.  j  av  a2s .  com
        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 . jav  a 2s .co  m*/
        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: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 .  ja v  a  2s . 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");
    }
}