Search in sources :

Example 41 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-security-oauth by spring-projects.

the class ScopeVoterTests method testExceptionThrownIfWrongScopesPresent.

@Test(expected = AccessDeniedException.class)
public void testExceptionThrownIfWrongScopesPresent() throws Exception {
    OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request("foo", false, Collections.singleton("read"));
    Authentication userAuthentication = null;
    OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
    assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(oAuth2Authentication, null, Collections.<ConfigAttribute>singleton(new SecurityConfig("SCOPE_WRITE"))));
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 42 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-security-oauth by spring-projects.

the class ScopeVoterTests method testAccessDeniedIfWrongScopesPresent.

@Test
public void testAccessDeniedIfWrongScopesPresent() throws Exception {
    OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request("foo", false, Collections.singleton("read"));
    Authentication userAuthentication = null;
    OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
    voter.setThrowException(false);
    assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(oAuth2Authentication, null, Collections.<ConfigAttribute>singleton(new SecurityConfig("SCOPE_WRITE"))));
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 43 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-security-oauth by spring-projects.

the class AbstractPersistentDefaultTokenServicesTests method testTokenEnhancerUpdatesStoredTokens.

@Test
public void testTokenEnhancerUpdatesStoredTokens() throws Exception {
    final ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken("testToken", new Date(System.currentTimeMillis() + 100000));
    getTokenServices().setTokenEnhancer(new TokenEnhancer() {

        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
            result.setRefreshToken(refreshToken);
            return result;
        }
    });
    OAuth2Authentication authentication = createAuthentication();
    OAuth2AccessToken original = getTokenServices().createAccessToken(authentication);
    assertTrue(original.getRefreshToken().equals(refreshToken));
    OAuth2AccessToken result = getTokenStore().getAccessToken(authentication);
    assertEquals(original, result);
    assertEquals(refreshToken, result.getRefreshToken());
    assertEquals(refreshToken, getTokenStore().readRefreshToken(refreshToken.getValue()));
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) Date(java.util.Date) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) Test(org.junit.Test)

Example 44 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-security-oauth by spring-projects.

the class DefaultAccessTokenConverterTests method extractAuthenticationSingleScopeString.

// gh-745
@Test
public void extractAuthenticationSingleScopeString() {
    String scope = "read";
    Map<String, Object> tokenAttrs = new HashMap<String, Object>();
    tokenAttrs.put(AccessTokenConverter.SCOPE, scope);
    OAuth2Authentication authentication = converter.extractAuthentication(tokenAttrs);
    assertEquals(Collections.singleton(scope), authentication.getOAuth2Request().getScope());
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 45 with OAuth2Authentication

use of org.springframework.security.oauth2.provider.OAuth2Authentication in project spring-security-oauth by spring-projects.

the class DefaultAccessTokenConverterTests method extractAuthenticationFromClientTokenSingleValuedAudience.

@Test
public void extractAuthenticationFromClientTokenSingleValuedAudience() {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    OAuth2Authentication authentication = new OAuth2Authentication(request, null);
    token.setScope(authentication.getOAuth2Request().getScope());
    Map<String, Object> map = new LinkedHashMap<String, Object>(converter.convertAccessToken(token, authentication));
    @SuppressWarnings("unchecked") Object aud = ((Collection<Object>) map.get(AccessTokenConverter.AUD)).iterator().next();
    map.put(AccessTokenConverter.AUD, aud);
    assertTrue(map.containsKey(AccessTokenConverter.AUD));
    OAuth2Authentication extracted = converter.extractAuthentication(map);
    assertEquals("[" + aud + "]", extracted.getOAuth2Request().getResourceIds().toString());
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Aggregations

OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)166 Test (org.junit.Test)116 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)84 Authentication (org.springframework.security.core.Authentication)68 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)57 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)49 Date (java.util.Date)34 HashMap (java.util.HashMap)22 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)21 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)20 DBUnitTest (org.orcid.test.DBUnitTest)17 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)15 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)15 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)15 HashSet (java.util.HashSet)13 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)13 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)13 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)13 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)13 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)12