Example usage for org.springframework.security.oauth.provider OAuthAuthenticationDetails getConsumerDetails

List of usage examples for org.springframework.security.oauth.provider OAuthAuthenticationDetails getConsumerDetails

Introduction

In this page you can find the example usage for org.springframework.security.oauth.provider OAuthAuthenticationDetails getConsumerDetails.

Prototype

public ConsumerDetails getConsumerDetails() 

Source Link

Usage

From source file:nl.surfnet.coin.api.AbstractApiController.java

protected ClientMetaData getClientMetaData() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    ClientMetaData metaData = null;/*  w w w .j  a v  a2  s  .  c  o m*/
    // oauth2
    if (authentication instanceof OAuth2Authentication) {
        OAuth2Authentication oauth2 = (OAuth2Authentication) authentication;
        String clientId = oauth2.getAuthorizationRequest().getClientId();
        ClientDetails clientDetails = janusClientDetailsService.loadClientByClientId(clientId);
        metaData = ((OpenConextClientDetails) clientDetails).getClientMetaData();
        registerApiVersion("oauth2");
    }
    // oauth1 3-legged
    else if (authentication instanceof PreAuthenticatedAuthenticationToken) {
        PreAuthenticatedAuthenticationToken preAuth = (PreAuthenticatedAuthenticationToken) authentication;
        Object principal = preAuth.getPrincipal();
        if (principal instanceof ClientMetaDataUser) {
            ClientMetaDataUser user = (ClientMetaDataUser) principal;
            metaData = user.getClientMetaData();
            if (metaData == null) {
                Object details = preAuth.getDetails();
                if (details instanceof OAuthAuthenticationDetails) {
                    OAuthAuthenticationDetails authDetails = (OAuthAuthenticationDetails) details;
                    ConsumerDetails consumerDetails = authDetails.getConsumerDetails();
                    if (consumerDetails instanceof OpenConextConsumerDetails) {
                        OpenConextConsumerDetails base = (OpenConextConsumerDetails) consumerDetails;
                        metaData = base.getClientMetaData();
                    }
                }
            }
            registerApiVersion("oauth1-3legged");
        }
    } // oauth1 2-legged
    else if (authentication instanceof ConsumerAuthentication) {
        ConsumerAuthentication conAuth = (ConsumerAuthentication) authentication;
        ConsumerDetails consumerDetails = conAuth.getConsumerDetails();
        if (consumerDetails instanceof OpenConextConsumerDetails) {
            OpenConextConsumerDetails details = (OpenConextConsumerDetails) consumerDetails;
            metaData = details.getClientMetaData();
            registerApiVersion("oauth1-2legged");
        }
    } else if (authentication instanceof SAMLAuthenticationToken) {
        SAMLAuthenticationToken samlToken = (SAMLAuthenticationToken) authentication;
        metaData = samlToken.getClientMetaData();
        registerApiVersion("oauth2");
    } else {
        throw new IllegalArgumentException("Authentication is of unknown class ('"
                + (authentication != null ? authentication.getClass() : "null") + "')");
    }
    Assert.notNull(metaData, "ClientMetaData may not be null for checking ACL's. Authentication is of class ('"
            + (authentication != null ? authentication.getClass() : "null") + "')");
    return metaData;
}