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

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

Introduction

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

Prototype

public boolean isAccessToken() 

Source Link

Document

Whether this is an OAuth 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;//from w w w . j  a v a  2  s  .  co  m
    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

/**
 * Whether the auth token is expired./*ww w  .j  a  va  2  s .  c  o m*/
 *
 * @param authToken The auth token to check for expiration.
 * @return Whether the auth token is expired. 
 */
protected boolean isExpired(OAuthProviderTokenImpl authToken) {
    if (authToken.isAccessToken()) {
        if ((authToken.getTimestamp() + (getAccessTokenValiditySeconds() * 1000L)) < System
                .currentTimeMillis()) {
            return true;
        }
    } else {
        if ((authToken.getTimestamp() + (getRequestTokenValiditySeconds() * 1000L)) < System
                .currentTimeMillis()) {
            return true;
        }
    }

    return false;
}

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

public void authorizeRequestToken(String requestToken, String verifier, Authentication authentication)
        throws AuthenticationException {
    OAuthProviderTokenImpl tokenImpl = readToken(requestToken);

    if (tokenImpl == null) {
        throw new InvalidOAuthTokenException("Invalid token: " + requestToken);
    } else if (isExpired(tokenImpl)) {
        removeToken(requestToken);//from   w w w  .  j  a  v  a  2  s  .c om
        onTokenRemoved(tokenImpl);
        throw new ExpiredOAuthTokenException("Expired token.");
    } else if (tokenImpl.isAccessToken()) {
        throw new InvalidOAuthTokenException("Request to authorize an access token.");
    }

    tokenImpl.setUserAuthentication(authentication);
    tokenImpl.setTimestamp(System.currentTimeMillis());//reset the expiration.
    tokenImpl.setVerifier(verifier);
    storeToken(requestToken, tokenImpl);
}

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 v  a 2s . 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;
}