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

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

Introduction

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

Prototype

public Set<String> getScope() 

Source Link

Document

The scope of the token.

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