Example usage for org.springframework.security.oauth2.client OAuth2RestTemplate getAccessToken

List of usage examples for org.springframework.security.oauth2.client OAuth2RestTemplate getAccessToken

Introduction

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

Prototype

public OAuth2AccessToken getAccessToken() throws UserRedirectRequiredException 

Source Link

Document

Acquire or renew an access token for the current context if necessary.

Usage

From source file:com.companyname.plat.commons.client.Oauth2Operation.java

/**
* Get the current access token. Should be available inside a test method as long as a resource has been setup with
* {@link OAuth2ContextConfiguration @OAuth2ContextConfiguration}.
* 
* @return the current access token initializing it if necessary
*//*  w w w.  j  a v  a 2s .c  o m*/
public static OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource,
        OAuth2RestTemplate client) {
    if (resource == null || client == null) {
        return null;
    }

    try {
        return client.getAccessToken();
    } catch (OAuth2AccessDeniedException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        if (cause instanceof Error) {
            throw (Error) cause;
        }
        throw e;
    }
}

From source file:org.trustedanalytics.user.common.OAuth2PriviligedInterceptor.java

@Override
public void apply(RequestTemplate requestTemplate) {
    OAuth2RestTemplate rt = new OAuth2RestTemplate(clientCredentials);
    OAuth2AccessToken accessToken = rt.getAccessToken();
    requestTemplate.header("Authorization", "bearer " + accessToken.getValue());
}

From source file:com.create.controller.AuthenticationIT.java

@Test(expected = OAuth2AccessDeniedException.class)
public void shouldThrowAnExceptionForInvalidCredentials() {
    final OAuth2RestTemplate restTemplate = getRestTemplateWithInvalidUserPassword();
    restTemplate.getAccessToken();
}

From source file:com.create.controller.AuthenticationIT.java

@Test(expected = OAuth2AccessDeniedException.class)
public void shouldThrowAnExceptionForInvalidClientCredentials() {
    final OAuth2RestTemplate restTemplate = getRestTemplateWithInvalidClientPassword();
    restTemplate.getAccessToken();
}

From source file:org.trustedanalytics.servicecatalog.cf.CcConfig.java

@Bean
protected CcOperations ccPrivilegedClient(@Qualifier("clientRestTemplate") OAuth2RestTemplate restTemplate) {
    return new FeignClient(apiBaseUrl, builder -> builder.requestInterceptor(
            template -> template.header("Authorization", "bearer " + restTemplate.getAccessToken())));
}

From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java

/**
 * Test using a bad username/*from w  w  w .  j  a va  2s.com*/
 */
@Test
public void testBadUsername() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials("badUsername", PASSWORD);
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof InvalidGrantException) {
            InvalidGrantException clientException = (InvalidGrantException) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected InvalidGrantException. Got %s", ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java

/**
 * Test using a bad username/*from  w  ww  .j a v a 2  s  .  c om*/
 */
@Test
public void testBadPassword() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, "badPassword");
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof InvalidGrantException) {
            InvalidGrantException clientException = (InvalidGrantException) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected InvalidGrantException. Got %s", ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad scope.
 *///from  ww w  .j  av  a 2 s . c om
@Test
public void testBadScope() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadScope();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof InvalidScopeException) {
            InvalidScopeException clientException = (InvalidScopeException) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad secret.
 *///from  w w  w  .j  av  a 2s.com
@Test
public void testBadClientSecret() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadSecret();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof OAuth2Exception) {
            OAuth2Exception clientException = (OAuth2Exception) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad client Id.
 *//*w  w  w .j a v  a2s  .com*/
@Test
public void testBadClientId() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadClientId();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof HttpClientErrorException) {
            HttpClientErrorException clientException = (HttpClientErrorException) ex.getCause();
            assertEquals(HttpStatus.UNAUTHORIZED, clientException.getStatusCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}