Search in sources :

Example 11 with DefaultOAuth2AccessToken

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

the class OAuth2RestTemplateTests method testNewTokenAcquiredIfExpired.

@Test
public void testNewTokenAcquiredIfExpired() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("TEST");
    token.setExpiration(new Date(System.currentTimeMillis() - 1000));
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    restTemplate.setAccessTokenProvider(new StubAccessTokenProvider());
    OAuth2AccessToken newToken = restTemplate.getAccessToken();
    assertNotNull(newToken);
    assertTrue(!token.equals(newToken));
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Date(java.util.Date) Test(org.junit.Test)

Example 12 with DefaultOAuth2AccessToken

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

the class OAuth2ClientAuthenticationProcessingFilterTests method testAuthenticationWithTokenType.

@Test
public void testAuthenticationWithTokenType() throws Exception {
    filter.setRestTemplate(restTemplate);
    filter.setTokenServices(tokenServices);
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setTokenType("foo");
    Mockito.when(restTemplate.getAccessToken()).thenReturn(token);
    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("foo", ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenType());
}
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 13 with DefaultOAuth2AccessToken

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

the class TokenApprovalStoreTests method addApprovals.

@Override
protected boolean addApprovals(Collection<Approval> approvals) {
    Map<String, Map<String, Set<String>>> clientIds = new HashMap<String, Map<String, Set<String>>>();
    for (Approval approval : approvals) {
        String clientId = approval.getClientId();
        if (!clientIds.containsKey(clientId)) {
            clientIds.put(clientId, new HashMap<String, Set<String>>());
        }
        String userId = approval.getUserId();
        Map<String, Set<String>> users = clientIds.get(clientId);
        if (!users.containsKey(userId)) {
            users.put(userId, new HashSet<String>());
        }
        Set<String> scopes = users.get(userId);
        scopes.add(approval.getScope());
    }
    for (String clientId : clientIds.keySet()) {
        Map<String, Set<String>> users = clientIds.get(clientId);
        for (String userId : users.keySet()) {
            Authentication user = new UsernamePasswordAuthenticationToken(userId, "N/A", AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
            AuthorizationRequest authorizationRequest = new AuthorizationRequest();
            authorizationRequest.setClientId(clientId);
            Set<String> scopes = users.get(userId);
            authorizationRequest.setScope(scopes);
            OAuth2Request request = authorizationRequest.createOAuth2Request();
            OAuth2Authentication authentication = new OAuth2Authentication(request, user);
            DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(UUID.randomUUID().toString());
            token.setScope(scopes);
            tokenStore.storeAccessToken(token, authentication);
        }
    }
    return super.addApprovals(approvals);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) HashMap(java.util.HashMap) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with DefaultOAuth2AccessToken

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

the class AccessTokenProviderChainTests method testRefreshAccessTokenTwicePreserveRefreshToken.

// gh-712
@Test
public void testRefreshAccessTokenTwicePreserveRefreshToken() throws Exception {
    DefaultOAuth2AccessToken accessToken = getExpiredToken();
    DefaultOAuth2AccessToken expectedRefreshedAccessToken = new DefaultOAuth2AccessToken("refreshed-access-token");
    expectedRefreshedAccessToken.setExpiration(accessToken.getExpiration());
    AccessTokenProviderChain chain = getTokenProvider(accessToken, expectedRefreshedAccessToken);
    SecurityContextHolder.getContext().setAuthentication(user);
    // Obtain a new Access Token
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    OAuth2AccessToken tokenResult = chain.obtainAccessToken(resource, request);
    assertEquals(accessToken, tokenResult);
    // Obtain the 1st Refreshed Access Token
    Calendar tokenExpiry = Calendar.getInstance();
    tokenExpiry.setTime(tokenResult.getExpiration());
    tokenExpiry.add(Calendar.MINUTE, -1);
    // Expire
    DefaultOAuth2AccessToken.class.cast(tokenResult).setExpiration(tokenExpiry.getTime());
    request = new DefaultAccessTokenRequest();
    request.setExistingToken(tokenResult);
    tokenResult = chain.obtainAccessToken(resource, request);
    assertEquals(expectedRefreshedAccessToken, tokenResult);
    // Obtain the 2nd Refreshed Access Token
    tokenExpiry.setTime(tokenResult.getExpiration());
    tokenExpiry.add(Calendar.MINUTE, -1);
    // Expire
    DefaultOAuth2AccessToken.class.cast(tokenResult).setExpiration(tokenExpiry.getTime());
    request = new DefaultAccessTokenRequest();
    request.setExistingToken(tokenResult);
    tokenResult = chain.obtainAccessToken(resource, request);
    assertEquals(expectedRefreshedAccessToken, tokenResult);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Calendar(java.util.Calendar) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 15 with DefaultOAuth2AccessToken

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

the class AccessTokenProviderChainTests method testRefreshAccessTokenReplacingNullValue.

@Test
public void testRefreshAccessTokenReplacingNullValue() throws Exception {
    DefaultOAuth2AccessToken accessToken = getExpiredToken();
    DefaultOAuth2AccessToken refreshedAccessToken = new DefaultOAuth2AccessToken("refreshed-access-token");
    AccessTokenProviderChain chain = getTokenProvider(accessToken, refreshedAccessToken);
    SecurityContextHolder.getContext().setAuthentication(user);
    // Obtain a new Access Token
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    OAuth2AccessToken newAccessToken = chain.refreshAccessToken(resource, accessToken.getRefreshToken(), request);
    // gh-712
    assertEquals(newAccessToken.getRefreshToken(), accessToken.getRefreshToken());
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

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