Example usage for org.springframework.security.oauth2.common DefaultExpiringOAuth2RefreshToken getExpiration

List of usage examples for org.springframework.security.oauth2.common DefaultExpiringOAuth2RefreshToken getExpiration

Introduction

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

Prototype

public Date getExpiration() 

Source Link

Document

The instant the token expires.

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();/*  ww  w.j a  v a 2s  .  com*/
        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: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(" ");
        }/*w  w  w .j  ava2  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;
}