Search in sources :

Example 1 with DefaultOAuth2ClientContext

use of org.springframework.security.oauth2.client.DefaultOAuth2ClientContext in project spring-boot by spring-projects.

the class UserInfoTokenServicesRefreshTokenTests method withRestTemplateChangesState.

@Test
public void withRestTemplateChangesState() {
    OAuth2ProtectedResourceDetails resource = new AuthorizationCodeResourceDetails();
    OAuth2ClientContext context = new DefaultOAuth2ClientContext();
    context.setAccessToken(new DefaultOAuth2AccessToken("FOO"));
    this.services.setRestTemplate(new OAuth2RestTemplate(resource, context));
    assertThat(this.services.loadAuthentication("BAR").getName()).isEqualTo("me");
    assertThat(context.getAccessToken().getValue()).isEqualTo("BAR");
}
Also used : DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) OAuth2ClientContext(org.springframework.security.oauth2.client.OAuth2ClientContext) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with DefaultOAuth2ClientContext

use of org.springframework.security.oauth2.client.DefaultOAuth2ClientContext 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 DefaultOAuth2ClientContext

use of org.springframework.security.oauth2.client.DefaultOAuth2ClientContext in project spring-security-oauth by spring-projects.

the class RefreshTokenGrantTests method testConnectDirectlyToResourceServer.

@Test
public void testConnectDirectlyToResourceServer() throws Exception {
    assertNotNull(existingToken.getRefreshToken());
    // It won't be expired on the server, but we can force the client to refresh it
    assertTrue(existingToken.isExpired());
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setExistingToken(existingToken);
    OAuth2RestTemplate template = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext(request));
    String result = template.getForObject(serverRunning.getUrl("/sparklr2/photos/user/message"), String.class);
    assertEquals("Hello, Trusted User marissa", result);
    assertFalse("Tokens match so there was no refresh", existingToken.equals(template.getAccessToken()));
}
Also used : DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 4 with DefaultOAuth2ClientContext

use of org.springframework.security.oauth2.client.DefaultOAuth2ClientContext in project paascloud-master by paascloud.

the class OAuth2FeignAutoConfiguration method oAuth2RestTemplate.

/**
 * O auth 2 rest template o auth 2 rest template.
 *
 * @return the o auth 2 rest template
 */
@Bean("paascloudOAuth2RestTemplate")
public OAuth2RestTemplate oAuth2RestTemplate() {
    final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails(), new DefaultOAuth2ClientContext());
    oAuth2RestTemplate.setRequestFactory(new Netty4ClientHttpRequestFactory());
    return oAuth2RestTemplate;
}
Also used : DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) Netty4ClientHttpRequestFactory(org.springframework.http.client.Netty4ClientHttpRequestFactory) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) Bean(org.springframework.context.annotation.Bean)

Example 5 with DefaultOAuth2ClientContext

use of org.springframework.security.oauth2.client.DefaultOAuth2ClientContext in project spring-cloud-security by spring-cloud.

the class OAuth2FeignRequestInterceptorTests method tryToAcquireToken.

@Test(expected = OAuth2AccessDeniedException.class)
public void tryToAcquireToken() {
    oAuth2FeignRequestInterceptor = new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), new BaseOAuth2ProtectedResourceDetails());
    OAuth2AccessToken oAuth2AccessToken = oAuth2FeignRequestInterceptor.getToken();
    Assert.assertTrue(oAuth2AccessToken.getValue() + " Must be null", oAuth2AccessToken.getValue() == null);
}
Also used : DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) Test(org.junit.Test)

Aggregations

DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)9 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)8 Test (org.junit.Test)6 OAuth2ClientContext (org.springframework.security.oauth2.client.OAuth2ClientContext)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Bean (org.springframework.context.annotation.Bean)2 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)2 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)2 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)2 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)2 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)2 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)2 IOException (java.io.IOException)1 Date (java.util.Date)1 Scope (org.springframework.context.annotation.Scope)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 Netty4ClientHttpRequestFactory (org.springframework.http.client.Netty4ClientHttpRequestFactory)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 AccessTokenProviderChain (org.springframework.security.oauth2.client.token.AccessTokenProviderChain)1 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)1