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

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

Introduction

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

Prototype

public ApprovalStatus getStatus() 

Source Link

Usage

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 w  w  .  j a v  a  2 s.  co  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
@Transactional//  w  w w  . j  ava2  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 w w  w  .j  a v a2 s  .  com
        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: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 {/* w w w. j  ava  2s  . com*/
            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");
    }// w ww  . j  a v  a  2s.  com
    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;
}

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

private boolean updateApproval(final String sql, final Approval approval) {
    logger.debug(String.format("refreshing approval: [%s]", approval));
    int refreshed = jdbcTemplate.update(sql, new PreparedStatementSetter() {
        @Override//w ww  .  ja  va  2 s  .co m
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setTimestamp(1, new Timestamp(approval.getExpiresAt().getTime()));
            ps.setString(2, (approval.getStatus() == null ? APPROVED : approval.getStatus()).toString());
            ps.setTimestamp(3, new Timestamp(approval.getLastUpdatedAt().getTime()));
            ps.setString(4, approval.getUserId());
            ps.setString(5, approval.getClientId());
            ps.setString(6, approval.getScope());
        }
    });
    if (refreshed != 1) {
        return false;
    }
    return true;
}