Example usage for org.springframework.security.oauth2.provider.approval Approval getScope

List of usage examples for org.springframework.security.oauth2.provider.approval Approval getScope

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider.approval Approval getScope.

Prototype

public String getScope() 

Source Link

Usage

From source file:oauth2.authentication.approvals.ApprovalServiceImpl.java

static ApprovalPK toPrimaryKey(Approval approval) {
    return new ApprovalPK(approval.getUserId(), approval.getClientId(), approval.getScope());
}

From source file:oauth2.authentication.approvals.ApprovalServiceImpl.java

static Approval fromEntity(oauth2.entities.Approval entity) {
    return new Approval(entity.getUserId(), entity.getClientId(), entity.getScope(),
            toDate(entity.getExpiresAt()), entity.getStatus(), toDate(entity.getLastUpdateAt()));
}

From source file:oauth2.authentication.approvals.ApprovalServiceImpl.java

static oauth2.entities.Approval toEntity(Approval approval) {
    oauth2.entities.Approval entity = new oauth2.entities.Approval();
    entity.setUserId(approval.getUserId());
    entity.setClientId(approval.getClientId());
    entity.setScope(approval.getScope());
    entity.setStatus(approval.getStatus());
    entity.setExpiresAt(toInstant(approval.getExpiresAt()));
    entity.setLastUpdateAt(toInstant(approval.getLastUpdatedAt()));
    return entity;
}

From source file:org.energyos.espi.datacustodian.oauth.AccessConfirmationController.java

@RequestMapping("/oauth/confirm_access")
public ModelAndView getAccessConfirmation(Map<String, Object> model, Principal principal) throws Exception {
    AuthorizationRequest clientAuth = (AuthorizationRequest) model.remove("authorizationRequest");
    ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
    model.put("auth_request", clientAuth);
    model.put("client", client);
    Map<String, String> scopes = new LinkedHashMap<String, String>();
    for (String scope : clientAuth.getScope()) {
        scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, "false"); //Spring Security OAuth2 2.0.0.M2 change
    }//from  w ww  . j av  a2 s. c  o  m
    for (Approval approval : approvalStore.getApprovals(principal.getName(), client.getClientId())) {
        if (clientAuth.getScope().contains(approval.getScope())) {
            scopes.put(OAuth2Utils.SCOPE_PREFIX + approval.getScope(),
                    approval.getStatus() == ApprovalStatus.APPROVED ? "true" : "false");
        }
    }
    model.put("scopes", scopes);
    return new ModelAndView("access_confirmation", model);
}

From source file:org.meruvian.yama.web.security.oauth.DefaultOauthApplicationApprovalService.java

@Override
public boolean revokeApprovals(Collection<Approval> approvals) {
    for (Approval approval : approvals) {
        OauthApplicationApproval a = getApprovalByUserAndClientAndScope(approval.getUserId(),
                approval.getClientId(), approval.getScope());
        if (a != null) {
            approvalRepository.delete(a);
        }/*  w ww .  ja va 2s  .  co  m*/
    }

    return true;
}

From source file:org.meruvian.yama.web.security.oauth.DefaultOauthApplicationApprovalService.java

@Override
@Transactional//from ww w .j  ava 2 s .  c o  m
public boolean addApprovals(Collection<Approval> approvals) {
    for (Approval approval : approvals) {
        OauthApplicationApproval a = getApprovalByUserAndClientAndScope(approval.getUserId(),
                approval.getClientId(), approval.getScope());
        if (a != null) {
            a.setStatus(approval.getStatus() == null ? ApprovalStatus.APPROVED
                    : ApprovalStatus.valueOf(approval.getStatus().name()));
            a.setExpiresAt(approval.getExpiresAt());
        } else {
            a = new OauthApplicationApproval();
            {
                Application application = new Application();
                application.setId(approval.getClientId());
                a.setApplication(application);
            }
            a.setExpiresAt(approval.getExpiresAt());
            a.setScope(approval.getScope());
            a.setStatus(approval.getStatus() == null ? ApprovalStatus.APPROVED
                    : ApprovalStatus.valueOf(approval.getStatus().name()));
        }

        approvalRepository.save(a);
    }

    return true;
}

From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java

@Override
public boolean addApprovals(final Collection<Approval> approvals) {
    LOG.debug(APPROVAL, "Adding approvals: {}", approvals);

    boolean success = true;
    for (Approval approval : approvals) {
        DBObject query = new BasicDBObject(userIdFieldName, approval.getUserId())
                .append(clientIdFieldName, approval.getClientId()).append(scopeFieldName, approval.getScope());
        DBObject obj = getApprovalsCollection().findOne(query);
        if (obj == null) {
            obj = new BasicDBObject(userIdFieldName, approval.getUserId())
                    .append(clientIdFieldName, approval.getClientId())
                    .append(scopeFieldName, approval.getScope());
        }/*from ww  w . j a  v a 2s  .co  m*/
        obj.put(statusFieldName, approval.getStatus().name());
        obj.put(expiresAtFieldName, approval.getExpiresAt());
        obj.put(lastModifiedAtFieldName, approval.getLastUpdatedAt());

        LOG.trace(APPROVAL, "Saving approval {}", obj);

        WriteResult result = getApprovalsCollection().save(obj, writeConcern);

        LOG.trace(APPROVAL, "Approval save result is {}", result);

        success = success && result.getN() == 1;
    }
    return success;
}

From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java

@Override
public boolean revokeApprovals(Collection<Approval> approvals) {
    LOG.debug("Revoking approvals: {}", approvals);

    boolean success = true;
    for (Approval approval : approvals) {
        DBObject query = new BasicDBObject(userIdFieldName, approval.getUserId())
                .append(clientIdFieldName, approval.getClientId()).append(scopeFieldName, approval.getScope());
        DBObject result = getApprovalsCollection().findOne(query);
        if (result != null) {
            WriteResult writeResult;/*from w  w w  . j  a  va 2s  . c o m*/
            if (handleRevocationsAsExpiry) {
                LOG.trace(APPROVAL, "Handling revocation as expiry: updating approval {} field",
                        expiresAtFieldName);

                result.put(expiresAtFieldName, new Date());
                writeResult = getApprovalsCollection().save(result, writeConcern);
            } else {
                LOG.trace(APPROVAL, "Handling revocation as delete: removing approval {}", result);

                writeResult = getApprovalsCollection().remove(result, writeConcern);
            }
            success = success && writeResult.getN() == 1;
        } else {
            LOG.debug(APPROVAL, "No approval found for sample {}", query);
            success = false;
        }
    }
    return success;
}

From source file:org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler.java

public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest,
        Authentication userAuthentication) {

    String clientId = authorizationRequest.getClientId();
    Collection<String> requestedScopes = authorizationRequest.getScope();
    Set<String> approvedScopes = new HashSet<String>();
    Set<String> validUserApprovedScopes = new HashSet<String>();

    if (clientDetailsService != null) {
        try {//from   w  ww .ja va2  s.co m
            ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
            for (String scope : requestedScopes) {
                if (client.isAutoApprove(scope) || client.isAutoApprove("all")) {
                    approvedScopes.add(scope);
                }
            }
            if (approvedScopes.containsAll(requestedScopes)) {
                authorizationRequest.setApproved(true);
                return authorizationRequest;
            }
        } catch (ClientRegistrationException e) {
            logger.warn("Client registration problem prevent autoapproval check for client=" + clientId);
        }
    }

    if (logger.isDebugEnabled()) {
        StringBuilder builder = new StringBuilder("Looking up user approved authorizations for ");
        builder.append("client_id=" + clientId);
        builder.append(" and username=" + userAuthentication.getName());
        logger.debug(builder.toString());
    }

    // Find the stored approvals for that user and client
    Collection<Approval> userApprovals = approvalStore.getApprovals(userAuthentication.getName(), clientId);

    // Look at the scopes and see if they have expired
    Date today = new Date();
    for (Approval approval : userApprovals) {
        if (approval.getExpiresAt().after(today)) {
            validUserApprovedScopes.add(approval.getScope());
            if (approval.getStatus() == ApprovalStatus.APPROVED) {
                approvedScopes.add(approval.getScope());
            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Valid user approved/denied scopes are " + validUserApprovedScopes);
    }

    // If the requested scopes have already been acted upon by the user,
    // this request is approved
    if (validUserApprovedScopes.containsAll(requestedScopes)) {
        approvedScopes.retainAll(requestedScopes);
        // Set only the scopes that have been approved by the user
        authorizationRequest.setScope(approvedScopes);
        authorizationRequest.setApproved(true);
    }

    return authorizationRequest;

}

From source file:org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler.java

@Override
public Map<String, Object> getUserApprovalRequest(AuthorizationRequest authorizationRequest,
        Authentication userAuthentication) {
    Map<String, Object> model = new HashMap<String, Object>();
    model.putAll(authorizationRequest.getRequestParameters());
    Map<String, String> scopes = new LinkedHashMap<String, String>();
    for (String scope : authorizationRequest.getScope()) {
        scopes.put(OAuth2Utils.SCOPE_PREFIX + scope, "false");
    }//from  w w  w.j a v a 2 s .co  m
    for (Approval approval : approvalStore.getApprovals(userAuthentication.getName(),
            authorizationRequest.getClientId())) {
        if (authorizationRequest.getScope().contains(approval.getScope())) {
            scopes.put(OAuth2Utils.SCOPE_PREFIX + approval.getScope(),
                    approval.getStatus() == ApprovalStatus.APPROVED ? "true" : "false");
        }
    }
    model.put("scopes", scopes);
    return model;
}