Search in sources :

Example 1 with BaseOAuth2ProtectedResourceDetails

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

the class DefaultOAuth2RequestAuthenticatorTests method addsAccessToken.

@Test
public void addsAccessToken() {
    context.setAccessToken(new DefaultOAuth2AccessToken("FOO"));
    BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
    authenticator.authenticate(resource, context, request);
    String header = request.getHeaders().getFirst("Authorization");
    assertEquals("bearer FOO", header);
}
Also used : BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 2 with BaseOAuth2ProtectedResourceDetails

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

the class DefaultOAuth2RequestAuthenticatorTests method missingAccessToken.

@Test(expected = AccessTokenRequiredException.class)
public void missingAccessToken() {
    BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
    authenticator.authenticate(resource, new DefaultOAuth2ClientContext(), request);
}
Also used : BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) Test(org.junit.Test)

Example 3 with BaseOAuth2ProtectedResourceDetails

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

the class OAuth2RestTemplateTests method open.

@Before
public void open() throws Exception {
    resource = new BaseOAuth2ProtectedResourceDetails();
    // Facebook and older specs:
    resource.setTokenName("bearer_token");
    restTemplate = new OAuth2RestTemplate(resource);
    restTemplate.setAccessTokenProvider(accessTokenProvider);
    request = Mockito.mock(ClientHttpRequest.class);
    headers = new HttpHeaders();
    Mockito.when(request.getHeaders()).thenReturn(headers);
    ClientHttpResponse response = Mockito.mock(ClientHttpResponse.class);
    HttpStatus statusCode = HttpStatus.OK;
    Mockito.when(response.getStatusCode()).thenReturn(statusCode);
    Mockito.when(request.execute()).thenReturn(response);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Before(org.junit.Before)

Example 4 with BaseOAuth2ProtectedResourceDetails

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

the class UserInfoTokenServices method getMap.

@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Getting user info from: " + path);
    }
    try {
        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.warn("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
        return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
    }
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) OAuth2RestOperations(org.springframework.security.oauth2.client.OAuth2RestOperations) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) Map(java.util.Map) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) AuthenticationException(org.springframework.security.core.AuthenticationException) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException)

Example 5 with BaseOAuth2ProtectedResourceDetails

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

the class AbstractIntegrationTests method fixPaths.

@BeforeOAuth2Context
public void fixPaths() {
    String prefix = server.getServletPrefix();
    http.setPort(port);
    http.setPrefix(prefix);
    BaseOAuth2ProtectedResourceDetails resource = (BaseOAuth2ProtectedResourceDetails) context.getResource();
    resource.setAccessTokenUri(http.getUrl(tokenPath()));
    if (resource instanceof AbstractRedirectResourceDetails) {
        ((AbstractRedirectResourceDetails) resource).setUserAuthorizationUri(http.getUrl(authorizePath()));
    }
    if (resource instanceof ImplicitResourceDetails) {
        resource.setAccessTokenUri(http.getUrl(authorizePath()));
    }
    if (resource instanceof ResourceOwnerPasswordResourceDetails) {
        ((ResourceOwnerPasswordResourceDetails) resource).setUsername(security.getUser().getName());
        ((ResourceOwnerPasswordResourceDetails) resource).setPassword(security.getUser().getPassword());
    }
}
Also used : AbstractRedirectResourceDetails(org.springframework.security.oauth2.client.token.grant.redirect.AbstractRedirectResourceDetails) ResourceOwnerPasswordResourceDetails(org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails) ImplicitResourceDetails(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) BeforeOAuth2Context(org.springframework.security.oauth2.client.test.BeforeOAuth2Context)

Aggregations

BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)5 Test (org.junit.Test)2 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)2 Map (java.util.Map)1 Before (org.junit.Before)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 OAuth2RestOperations (org.springframework.security.oauth2.client.OAuth2RestOperations)1 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)1 BeforeOAuth2Context (org.springframework.security.oauth2.client.test.BeforeOAuth2Context)1 ImplicitResourceDetails (org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails)1 ResourceOwnerPasswordResourceDetails (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails)1 AbstractRedirectResourceDetails (org.springframework.security.oauth2.client.token.grant.redirect.AbstractRedirectResourceDetails)1 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)1 InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)1