Example usage for org.springframework.security.oauth2.client OAuth2RestOperations getOAuth2ClientContext

List of usage examples for org.springframework.security.oauth2.client OAuth2RestOperations getOAuth2ClientContext

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.client OAuth2RestOperations getOAuth2ClientContext.

Prototype

OAuth2ClientContext getOAuth2ClientContext();

Source Link

Usage

From source file:org.openmhealth.shim.OAuth2ShimBase.java

@Override
public AuthorizationRequestParameters getAuthorizationRequestParameters(String username,
        Map<String, String> addlParameters) throws ShimException {
    OAuth2RestOperations restTemplate = restTemplate();
    try {//from w w w . ja  v  a  2s. co  m
        trigger(restTemplate, getTriggerDataRequest());
        return AuthorizationRequestParameters.authorized();
    } catch (UserRedirectRequiredException e) {
        /**
         * If an exception was thrown it means a redirect is required
         * for user's external authorization with toolmaker.
         */
        AccessTokenRequest accessTokenRequest = restTemplate.getOAuth2ClientContext().getAccessTokenRequest();
        String stateKey = accessTokenRequest.getStateKey();

        /**
         * Build an authorization request from the exception
         * parameters. We also serialize spring's accessTokenRequest.
         */
        AuthorizationRequestParameters authRequestParams = new AuthorizationRequestParameters();
        authRequestParams.setRedirectUri(e.getRedirectUri());
        authRequestParams.setStateKey(e.getStateKey());
        authRequestParams.setAuthorizationUrl(getAuthorizationUrl(e));
        authRequestParams.setSerializedRequest(SerializationUtils.serialize(accessTokenRequest));
        authRequestParams.setStateKey(stateKey);

        authorizationRequestParametersRepo.save(authRequestParams);
        return authRequestParams;
    }
}

From source file:org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices.java

@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
    this.logger.info("Getting user info from: " + path);
    try {/*www  .j a v a 2 s . c  o  m*/
        OAuth2RestOperations restTemplate = this.restTemplate;
        if (restTemplate == null) {
            BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
            resource.setClientId(this.clientId);
            restTemplate = new OAuth2RestTemplate(resource);
        }
        DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
        token.setTokenType(this.tokenType);
        restTemplate.getOAuth2ClientContext().setAccessToken(token);
        return restTemplate.getForEntity(path, Map.class).getBody();
    } catch (Exception ex) {
        this.logger.info("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
        return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
    }
}

From source file:org.springframework.cloud.security.oauth2.resource.UserInfoTokenServices.java

private Map<String, Object> getMap(String path, String accessToken) {
    logger.info("Getting user info from: " + path);
    OAuth2RestOperations restTemplate = this.restTemplate;
    if (restTemplate == null) {
        BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
        resource.setClientId(clientId);//from www .  j av  a  2 s  .  c om
        restTemplate = new OAuth2RestTemplate(resource);
    }
    restTemplate.getOAuth2ClientContext().setAccessToken(new DefaultOAuth2AccessToken(accessToken));
    @SuppressWarnings("rawtypes")
    Map map = restTemplate.getForEntity(path, Map.class).getBody();
    @SuppressWarnings("unchecked")
    Map<String, Object> result = map;
    return result;
}

From source file:ua.com.skywell.oauth.custom.UserInfoTokenServices.java

@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
    this.logger.info("Getting user info from: " + path);
    try {//from  www .  j a v a  2 s .  c  o  m
        OAuth2RestOperations restTemplate = this.restTemplate;
        if (restTemplate == null) {
            BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
            resource.setClientId(this.clientId);
            restTemplate = new OAuth2RestTemplate(resource);
        }
        OAuth2AccessToken existingToken = restTemplate.getOAuth2ClientContext().getAccessToken();
        if (existingToken == null || !accessToken.equals(existingToken.getValue())) {
            DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
            token.setTokenType(this.tokenType);
            restTemplate.getOAuth2ClientContext().setAccessToken(token);
        }
        return restTemplate.getForEntity(path, Map.class).getBody();
    } catch (Exception ex) {
        this.logger.info("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
        return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
    }
}