Search in sources :

Example 1 with ClientCredentialsResourceDetails

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails in project spring-security-oauth by spring-projects.

the class AccessTokenProviderChainTests method testSunnyDayClientCredentialsWithTokenServicesSave.

@Test
public void testSunnyDayClientCredentialsWithTokenServicesSave() throws Exception {
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider()));
    chain.setClientTokenServices(clientTokenServices);
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    resource = new ClientCredentialsResourceDetails();
    resource.setId("resource");
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
    Mockito.verify(clientTokenServices).saveAccessToken(resource, null, token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) Test(org.junit.Test)

Example 2 with ClientCredentialsResourceDetails

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails in project spring-security-oauth by spring-projects.

the class ClientCredentialsGrantTests method testConnectDirectlyToResourceServer.

@Test
public void testConnectDirectlyToResourceServer() throws Exception {
    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
    resource.setAccessTokenUri(serverRunning.getUrl("/sparklr2/oauth/token"));
    resource.setClientId("my-client-with-registered-redirect");
    resource.setId("sparklr");
    resource.setScope(Arrays.asList("trust"));
    ClientCredentialsAccessTokenProvider provider = new ClientCredentialsAccessTokenProvider();
    OAuth2AccessToken accessToken = provider.obtainAccessToken(resource, new DefaultAccessTokenRequest());
    OAuth2RestTemplate template = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext(accessToken));
    String result = template.getForObject(serverRunning.getUrl("/sparklr2/photos/trusted/message"), String.class);
    assertEquals("Hello, Trusted Client", result);
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) ClientCredentialsAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 3 with ClientCredentialsResourceDetails

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails in project faf-java-server by FAForever.

the class ApiConfig method restTemplate.

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder, JsonApiMessageConverter jsonApiMessageConverter, ServerProperties properties) throws IOException {
    Api api = properties.getApi();
    ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
    resourceDetails.setClientId(api.getClientId());
    resourceDetails.setClientSecret(api.getClientSecret());
    resourceDetails.setAccessTokenUri(api.getAccessTokenUri());
    return restTemplateBuilder.additionalMessageConverters(jsonApiMessageConverter).rootUri(api.getBaseUrl()).configure(new OAuth2RestTemplate(resourceDetails));
}
Also used : Api(com.faforever.server.config.ServerProperties.Api) ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) Bean(org.springframework.context.annotation.Bean)

Example 4 with ClientCredentialsResourceDetails

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails in project tutorials by eugenp.

the class CustomConfigAuthorizationServerIntegrationTest method givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown.

@Test(expected = OAuth2AccessDeniedException.class)
public void givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("write"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
    restTemplate.getAccessToken();
}
Also used : ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with ClientCredentialsResourceDetails

use of org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails in project tutorials by eugenp.

the class DefaultConfigAuthorizationServerIntegrationTest method givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull.

@Test
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("client", asList("read", "write"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);
    OAuth2AccessToken accessToken = restTemplate.getAccessToken();
    assertNotNull(accessToken);
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) ClientCredentialsResourceDetails(org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

ClientCredentialsResourceDetails (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails)12 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)9 Test (org.junit.Test)7 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Bean (org.springframework.context.annotation.Bean)3 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)2 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)2 Api (com.faforever.server.config.ServerProperties.Api)1 Date (java.util.Date)1 PostConstruct (javax.annotation.PostConstruct)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 DefaultOAuth2RequestAuthenticator (org.springframework.security.oauth2.client.DefaultOAuth2RequestAuthenticator)1 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)1 ClientCredentialsAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider)1 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)1 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)1