Search in sources :

Example 51 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 52 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 53 with Approval

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

the class AbstractImplicitProviderTests method testPostForNonAutomaticApprovalToken.

@Test
@OAuth2ContextConfiguration(resource = NonAutoApproveImplicit.class, initialize = false)
public void testPostForNonAutomaticApprovalToken() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic " + new String(Base64.encode("user:password".getBytes())));
    context.getAccessTokenRequest().setHeaders(headers);
    try {
        assertNotNull(context.getAccessToken());
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
    // ignore
    }
    // add user approval parameter for the second request
    context.getAccessTokenRequest().add(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
    context.getAccessTokenRequest().add("scope.read", "true");
    assertNotNull(context.getAccessToken());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 54 with Approval

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

the class OAuth2FeignRequestInterceptor method acquireAccessToken.

/**
 * Try to acquire the token using a access token provider.
 * @return valid access token
 * @throws UserRedirectRequiredException in case the user needs to be redirected to an
 * approval page or login page
 */
protected OAuth2AccessToken acquireAccessToken() throws UserRedirectRequiredException {
    AccessTokenRequest tokenRequest = oAuth2ClientContext.getAccessTokenRequest();
    if (tokenRequest == null) {
        throw new AccessTokenRequiredException("Cannot find valid context on request for resource '" + resource.getId() + "'.", resource);
    }
    String stateKey = tokenRequest.getStateKey();
    if (stateKey != null) {
        tokenRequest.setPreservedState(oAuth2ClientContext.removePreservedState(stateKey));
    }
    OAuth2AccessToken existingToken = oAuth2ClientContext.getAccessToken();
    if (existingToken != null) {
        oAuth2ClientContext.setAccessToken(existingToken);
    }
    OAuth2AccessToken obtainableAccessToken;
    obtainableAccessToken = accessTokenProvider.obtainAccessToken(resource, tokenRequest);
    if (obtainableAccessToken == null || obtainableAccessToken.getValue() == null) {
        throw new IllegalStateException(" Access token provider returned a null token, which is illegal according to the contract.");
    }
    oAuth2ClientContext.setAccessToken(obtainableAccessToken);
    return obtainableAccessToken;
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AccessTokenRequiredException(org.springframework.security.oauth2.client.http.AccessTokenRequiredException) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest)

Example 55 with Approval

use of org.springframework.security.oauth2.provider.approval.Approval in project wumei-smart by kerwincui.

the class SpeakerApprovalHandler method updateAfterApproval.

@Override
public AuthorizationRequest updateAfterApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
    // 获取授权过的范围
    Set<String> requestedScopes = authorizationRequest.getScope();
    Set<String> approvedScopes = new HashSet<String>();
    Set<Approval> approvals = new HashSet<Approval>();
    Date expiry = computeExpiry();
    // 存储授权或拒绝的范围
    Map<String, String> approvalParameters = authorizationRequest.getApprovalParameters();
    for (String requestedScope : requestedScopes) {
        String approvalParameter = OAuth2Utils.SCOPE_PREFIX + requestedScope;
        String value = approvalParameters.get(approvalParameter);
        value = value == null ? "" : value.toLowerCase();
        if ("true".equals(value) || value.startsWith("approve") || value.equals("on")) {
            approvedScopes.add(requestedScope);
            approvals.add(new Approval(userAuthentication.getName(), authorizationRequest.getClientId(), requestedScope, expiry, Approval.ApprovalStatus.APPROVED));
        } else {
            approvals.add(new Approval(userAuthentication.getName(), authorizationRequest.getClientId(), requestedScope, expiry, Approval.ApprovalStatus.DENIED));
        }
    }
    approvalStore.addApprovals(approvals);
    boolean approved;
    authorizationRequest.setScope(approvedScopes);
    if (approvedScopes.isEmpty() && !requestedScopes.isEmpty()) {
        approved = false;
    } else {
        approved = true;
    }
    authorizationRequest.setApproved(approved);
    return authorizationRequest;
}
Also used : Approval(org.springframework.security.oauth2.provider.approval.Approval)

Aggregations

AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)46 Approval (org.cloudfoundry.identity.uaa.approval.Approval)43 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)34 Authentication (org.springframework.security.core.Authentication)27 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)25 Test (org.junit.Test)24 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)22 Date (java.util.Date)21 Test (org.junit.jupiter.api.Test)19 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)16 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)15 Approval (org.springframework.security.oauth2.provider.approval.Approval)15 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)7 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)7 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)7