Example usage for org.springframework.security.oauth.provider.token OAuthProviderTokenImpl getUserAuthentication

List of usage examples for org.springframework.security.oauth.provider.token OAuthProviderTokenImpl getUserAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.oauth.provider.token OAuthProviderTokenImpl getUserAuthentication.

Prototype

public Authentication getUserAuthentication() 

Source Link

Document

The authentication of the user who granted the access token.

Usage

From source file:nl.surfnet.coin.api.oauth.OpenConextOauth1TokenServices.java

@Override
protected void storeToken(String value, OAuthProviderTokenImpl token) {
    Assert.notNull(token, "Token cannot be null");
    Assert.notNull(value, "token value cannot be null");
    Authentication userAuthentication = token.getUserAuthentication();
    String userId = null;/*  ww  w. jav a 2s .  c om*/
    if (token.isAccessToken()) {
        String consumerKey = token.getConsumerKey();
        /*
         * get the client detail from Janus as we are unable to store them
         * somewhere along the 'road' and we cache this call anyway
         */
        ConsumerDetails consumerDetails = consumerDetailsService.loadConsumerByConsumerKey(consumerKey);
        if (consumerDetails instanceof OpenConextConsumerDetails) {
            OpenConextConsumerDetails extendedBaseConsumerDetails = (OpenConextConsumerDetails) consumerDetails;
            if (userAuthentication instanceof PreAuthenticatedAuthenticationToken) {
                PreAuthenticatedAuthenticationToken pre = (PreAuthenticatedAuthenticationToken) userAuthentication;
                Object principal = pre.getPrincipal();
                if (principal instanceof ClientMetaDataUser) {
                    ((ClientMetaDataUser) principal)
                            .setClientMetaData(extendedBaseConsumerDetails.getClientMetaData());
                    userId = ((ClientMetaDataUser) principal).getUsername();
                } else if (principal instanceof SAMLAuthenticationToken) {
                    ((SAMLAuthenticationToken) principal)
                            .setClientMetaData(extendedBaseConsumerDetails.getClientMetaData());
                    userId = ((SAMLAuthenticationToken) principal).getName();
                } else {
                    throw new RuntimeException(
                            "The principal on the PreAuthenticatedAuthenticationToken is of the type '"
                                    + (principal != null ? principal.getClass() : "null")
                                    + "'. Required is a (sub)class of ClientMetaDataUser or a (sub)class of SAMLAuthenticationToken");
                }
            } else if (userAuthentication instanceof SAMLAuthenticationToken) {
                SAMLAuthenticationToken samlToken = (SAMLAuthenticationToken) userAuthentication;
                samlToken.setClientMetaData(extendedBaseConsumerDetails.getClientMetaData());
                userId = samlToken.getName();
            } else {
                throw new RuntimeException("The userAuthentication is of the type '"
                        + (userAuthentication != null ? userAuthentication.getClass() : "null")
                        + "'. Required is a (sub)class of PreAuthenticatedAuthenticationToken or SAMLAuthenticationToken");
            }
        } else {
            throw new RuntimeException("The consumerDetails is of the type '"
                    + (consumerDetails != null ? consumerDetails.getClass() : "null")
                    + "'. Required is a (sub)class of ExtendedBaseConsumerDetails");
        }
    }
    jdbcTemplate.update(deleteTokenSql, value);
    jdbcTemplate.update(insertTokenSql, value, token.getCallbackUrl(), token.getVerifier(), token.getSecret(),
            token.getConsumerKey(), userId, token.isAccessToken(), token.getTimestamp(),
            SerializationUtils.serialize(userAuthentication));
}

From source file:org.springframework.security.oauth.provider.token.RandomValueProviderTokenServices.java

public OAuthAccessProviderToken createAccessToken(String requestToken) throws AuthenticationException {
    OAuthProviderTokenImpl tokenImpl = readToken(requestToken);

    if (tokenImpl == null) {
        throw new InvalidOAuthTokenException("Invalid token: " + requestToken);
    } else if (isExpired(tokenImpl)) {
        removeToken(requestToken);/* w w  w.  j a  va 2 s  . c o  m*/
        onTokenRemoved(tokenImpl);
        throw new ExpiredOAuthTokenException("Expired token.");
    } else if (tokenImpl.isAccessToken()) {
        throw new InvalidOAuthTokenException("Not a request token.");
    } else if (tokenImpl.getUserAuthentication() == null) {
        throw new InvalidOAuthTokenException("Request token has not been authorized.");
    }

    OAuthProviderTokenImpl requestTokenImpl = removeToken(requestToken);
    if (requestTokenImpl != null) {
        onTokenRemoved(requestTokenImpl);
    }

    String tokenValue = UUID.randomUUID().toString();
    byte[] secretBytes = new byte[getTokenSecretLengthBytes()];
    getRandom().nextBytes(secretBytes);
    String secret = new String(Base64.encodeBase64(secretBytes));
    OAuthProviderTokenImpl token = new OAuthProviderTokenImpl();
    token.setAccessToken(true);
    token.setConsumerKey(tokenImpl.getConsumerKey());
    token.setUserAuthentication(tokenImpl.getUserAuthentication());
    token.setSecret(secret);
    token.setValue(tokenValue);
    token.setTimestamp(System.currentTimeMillis());
    onTokenCreated(token);
    storeToken(tokenValue, token);
    return token;
}