Search in sources :

Example 1 with Approval

use of org.springframework.security.oauth2.provider.approval.Approval 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 2 with Approval

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

the class ApprovalStoreUserApprovalHandlerTests method testApprovalsAddedForAutoapprovedScopes.

@Test
public void testApprovalsAddedForAutoapprovedScopes() {
    handler.setClientDetailsService(clientDetailsService);
    BaseClientDetails client = new BaseClientDetails("client", null, "read", "authorization_code", null);
    client.setAutoApproveScopes(new HashSet<String>(Arrays.asList("read")));
    clientDetailsService.setClientDetailsStore(Collections.singletonMap("client", client));
    AuthorizationRequest authorizationRequest = new AuthorizationRequest("client", Arrays.asList("read"));
    AuthorizationRequest result = handler.checkForPreApproval(authorizationRequest, userAuthentication);
    Collection<Approval> approvals = store.getApprovals(userAuthentication.getName(), "client");
    assertEquals(1, approvals.size());
    Approval approval = approvals.iterator().next();
    assertEquals("read", approval.getScope());
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) Test(org.junit.Test)

Example 3 with Approval

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

the class ApprovalStoreUserApprovalHandlerTests method testExpiredPreapprovedScopes.

@Test
public void testExpiredPreapprovedScopes() {
    store.addApprovals(Arrays.asList(new Approval("user", "client", "read", new Date(System.currentTimeMillis() - 10000), Approval.ApprovalStatus.APPROVED)));
    AuthorizationRequest authorizationRequest = new AuthorizationRequest("client", Arrays.asList("read"));
    AuthorizationRequest result = handler.checkForPreApproval(authorizationRequest, userAuthentication);
    assertFalse(result.isApproved());
}
Also used : AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Approval

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

the class ApprovalStoreUserApprovalHandlerTests method testExplicitlyPreapprovedScopes.

@Test
public void testExplicitlyPreapprovedScopes() {
    store.addApprovals(Arrays.asList(new Approval("user", "client", "read", new Date(System.currentTimeMillis() + 10000), Approval.ApprovalStatus.APPROVED)));
    AuthorizationRequest authorizationRequest = new AuthorizationRequest("client", Arrays.asList("read"));
    AuthorizationRequest result = handler.checkForPreApproval(authorizationRequest, userAuthentication);
    assertTrue(result.isApproved());
}
Also used : AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) Date(java.util.Date) Test(org.junit.Test)

Example 5 with Approval

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

the class ApprovalStoreUserApprovalHandlerTests method testImplicitlyDeniedScope.

@Test
public void testImplicitlyDeniedScope() {
    AuthorizationRequest authorizationRequest = new AuthorizationRequest("client", Arrays.asList("read", "write"));
    authorizationRequest.setApprovalParameters(Collections.singletonMap("scope.read", "approved"));
    AuthorizationRequest result = handler.updateAfterApproval(authorizationRequest, userAuthentication);
    assertTrue(handler.isApproved(result, userAuthentication));
    Collection<Approval> approvals = store.getApprovals("user", "client");
    assertEquals(2, approvals.size());
    approvals.contains(new Approval("user", "client", "read", new Date(), Approval.ApprovalStatus.APPROVED));
    approvals.contains(new Approval("user", "client", "write", new Date(), Approval.ApprovalStatus.DENIED));
    assertEquals(1, result.getScope().size());
}
Also used : AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 Date (java.util.Date)13 Approval (org.springframework.security.oauth2.provider.approval.Approval)11 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)9 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)6 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)6 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)5 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)5 HashSet (java.util.HashSet)3 OAuth2ClientEntity (org.codenergic.theskeleton.client.OAuth2ClientEntity)3 Authentication (org.springframework.security.core.Authentication)3 UserApprovalRequiredException (org.springframework.security.oauth2.client.resource.UserApprovalRequiredException)3 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)3 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)3 ClientRegistrationException (org.springframework.security.oauth2.provider.ClientRegistrationException)3 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2