Search in sources :

Example 66 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class OAuth2RestTemplateTests method testNonBearerToken.

@Test
public void testNonBearerToken() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
    token.setTokenType("MINE");
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    ClientHttpRequest http = restTemplate.createRequest(URI.create("https://nowhere.com/api/crap"), HttpMethod.GET);
    String auth = http.getHeaders().getFirst("Authorization");
    assertTrue(auth.startsWith("MINE "));
}
Also used : ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 67 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class OAuth2RestTemplateTests method testDoubleEncodingOfAccessTokenValue.

/**
	 * tests encoding of access token value passed in protected requests ref: SECOAUTH-90
	 */
@Test
public void testDoubleEncodingOfAccessTokenValue() throws Exception {
    // try with fictitious token value with many characters to encode
    OAuth2AccessToken token = new DefaultOAuth2AccessToken("1 qI+x:y=z");
    // System.err.println(UriUtils.encodeQueryParam(token.getValue(), "UTF-8"));
    URI appended = restTemplate.appendQueryParameter(URI.create("https://graph.facebook.com/search"), token);
    assertEquals("https://graph.facebook.com/search?bearer_token=1+qI%2Bx%3Ay%3Dz", appended.toString());
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) URI(java.net.URI) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 68 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class OAuth2ClientAuthenticationProcessingFilterTests method testAuthentication.

@Test
public void testAuthentication() throws Exception {
    filter.setRestTemplate(restTemplate);
    filter.setTokenServices(tokenServices);
    Mockito.when(restTemplate.getAccessToken()).thenReturn(new DefaultOAuth2AccessToken("FOO"));
    Set<String> scopes = new HashSet<String>();
    scopes.addAll(Arrays.asList("read", "write"));
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("client", false, scopes);
    this.authentication = new OAuth2Authentication(storedOAuth2Request, null);
    Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
    Authentication authentication = filter.attemptAuthentication(new MockHttpServletRequest(), null);
    assertEquals(this.authentication, authentication);
    Mockito.verify(restTemplate, Mockito.times(1)).getAccessToken();
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 69 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class ResourceServerConfigurationTests method init.

@Before
public void init() {
    token = new DefaultOAuth2AccessToken("FOO");
    ClientDetails client = new BaseClientDetails("client", null, "read", "client_credentials", "ROLE_CLIENT");
    authentication = new OAuth2Authentication(new TokenRequest(null, "client", null, "client_credentials").createOAuth2Request(client), null);
    tokenStore.clear();
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Before(org.junit.Before)

Example 70 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class JaxbOAuth2AccessTokenMessageConverterTests method before.

@Before
public void before() throws Exception {
    converter = new JaxbOAuth2AccessTokenMessageConverter();
    accessToken = new DefaultOAuth2AccessToken("SlAV32hkKG");
    accessToken.setExpiration(expiration);
    accessToken.setRefreshToken(new DefaultOAuth2RefreshToken("8xLOxBtZp8"));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Before(org.junit.Before)

Aggregations

DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)95 Test (org.junit.Test)78 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)52 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)48 Date (java.util.Date)27 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)13 HashMap (java.util.HashMap)12 Authentication (org.springframework.security.core.Authentication)12 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)12 URI (java.net.URI)9 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)9 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)8 DBUnitTest (org.orcid.test.DBUnitTest)7 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)6 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)6 Before (org.junit.Before)5 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)5 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)5 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)5