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

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

Introduction

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

Prototype

public String getTokenType() 

Source Link

Document

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

Usage

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