Search in sources :

Example 1 with Pair

use of org.xdi.util.Pair in project oxAuth by GluuFederation.

the class PermissionService method hasEnoughPermissionsWithTicketRegistration.

public Pair<Boolean, Response> hasEnoughPermissionsWithTicketRegistration(UmaRPT p_rpt, List<ResourceSetPermission> p_rptPermissions, RsResourceType p_resourceType, List<RsScopeType> p_scopes) {
    final Pair<Boolean, Response> result = new Pair<Boolean, Response>(false, null);
    final ResourceSet resource = umaRsResourceService.getResource(p_resourceType);
    if (resource == null || StringUtils.isBlank(resource.getId())) {
        result.setFirst(false);
        result.setSecond(Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
        return result;
    }
    if (hasEnoughPermissions(p_rpt, p_rptPermissions, resource, p_scopes)) {
        result.setFirst(true);
        return result;
    } else {
        // If the RPT is valid but has insufficient authorization data for the type of access sought,
        // the resource server SHOULD register a requested permission with the authorization server
        // that would suffice for that scope of access (see Section 3.2),
        // and then respond with the HTTP 403 (Forbidden) status code,
        // along with providing the authorization server's URI in an "as_uri" property in the header,
        // and the permission ticket it just received from the AM in the body in a JSON-encoded "ticket" property.
        result.setFirst(false);
        final String ticket = registerPermission(p_rpt, resource, p_scopes);
        //                    log.debug("Register permissions on AM, permission ticket: " + ticket);
        final String entity = ServerUtil.asJsonSilently(new PermissionTicket(ticket));
        log.debug("Construct response: HTTP 403 (Forbidden), entity: " + entity);
        final Response response = Response.status(Response.Status.FORBIDDEN).header("host_id", appConfiguration.getIssuer()).header("as_uri", appConfiguration.getUmaConfigurationEndpoint()).header("error", "insufficient_scope").entity(entity).build();
        result.setSecond(response);
        return result;
    }
}
Also used : Response(javax.ws.rs.core.Response) PermissionTicket(org.xdi.oxauth.model.uma.PermissionTicket) ResourceSet(org.xdi.oxauth.model.uma.persistence.ResourceSet) Pair(org.xdi.util.Pair)

Example 2 with Pair

use of org.xdi.util.Pair in project oxTrust by GluuFederation.

the class UmaPermissionService method validateRptToken.

public Pair<Boolean, Response> validateRptToken(Token patToken, String authorization, String resourceSetId, String scopeId) {
    if ((patToken == null) || (authorization == null) || !authorization.startsWith("Bearer ")) {
        return authenticationFailure;
    }
    String rptToken = authorization.substring(7);
    boolean isGat = rptToken.startsWith("gat_");
    RptIntrospectionResponse rptStatusResponse = getStatusResponse(patToken, rptToken);
    if ((rptStatusResponse == null) || !rptStatusResponse.getActive()) {
        log.error("Status response for RPT token: '{}' is invalid", rptToken);
        return authenticationFailure;
    }
    boolean rptHasPermissions = isRptHasPermissions(rptStatusResponse);
    if (rptHasPermissions) {
        for (UmaPermission umaPermission : rptStatusResponse.getPermissions()) {
            if ((umaPermission.getScopes() != null) && umaPermission.getScopes().contains(scopeId) && (isGat || StringHelper.equals(resourceSetId, umaPermission.getResourceSetId()))) {
                return authenticationSuccess;
            }
        }
        log.error("Status response for RPT token: '{}' not contains right permission", rptToken);
        return authenticationFailure;
    }
    // If the RPT is valid but has insufficient authorization data for the type of access sought,
    // the resource server SHOULD register a requested permission with the authorization server
    // that would suffice for that scope of access (see Section 3.2),
    // and then respond with the HTTP 403 (Forbidden) status code,
    // along with providing the authorization server's URI in an "as_uri" property in the header,
    // and the permission ticket it just received from the AM in the body in a JSON-encoded "ticket" property.
    final String ticket = registerUmaPermissions(patToken, resourceSetId, scopeId);
    if (StringHelper.isEmpty(ticket)) {
        return authenticationFailure;
    }
    Response registerUmaPermissionsResponse = prepareRegisterUmaPermissionsResponse(patToken, resourceSetId, scopeId);
    if (registerUmaPermissionsResponse == null) {
        return authenticationFailure;
    }
    return new Pair<Boolean, Response>(true, registerUmaPermissionsResponse);
}
Also used : Response(javax.ws.rs.core.Response) RptIntrospectionResponse(org.xdi.oxauth.model.uma.RptIntrospectionResponse) HttpResponse(org.apache.http.HttpResponse) RptIntrospectionResponse(org.xdi.oxauth.model.uma.RptIntrospectionResponse) UmaPermission(org.xdi.oxauth.model.uma.UmaPermission) Pair(org.xdi.util.Pair)

Example 3 with Pair

use of org.xdi.util.Pair in project oxTrust by GluuFederation.

the class UmaPermissionService method validateRptToken.

public Pair<Boolean, Response> validateRptToken(Token patToken, String authorization, String resourceId, List<String> scopeIds) {
    if (StringHelper.isNotEmpty(authorization) && authorization.startsWith("Bearer ")) {
        String rptToken = authorization.substring(7);
        RptIntrospectionResponse rptStatusResponse = getStatusResponse(patToken, rptToken);
        if ((rptStatusResponse == null) || !rptStatusResponse.getActive()) {
            log.error("Status response for RPT token: '{}' is invalid", rptToken);
        // return authenticationFailure;
        } else {
            boolean rptHasPermissions = isRptHasPermissions(rptStatusResponse);
            if (rptHasPermissions) {
                // Collect all scopes
                List<String> returnScopeIds = new LinkedList<String>();
                for (UmaPermission umaPermission : rptStatusResponse.getPermissions()) {
                    if (umaPermission.getScopes() != null) {
                        returnScopeIds.addAll(umaPermission.getScopes());
                    }
                }
                if (returnScopeIds.containsAll(scopeIds)) {
                    return authenticationSuccess;
                }
                log.error("Status response for RPT token: '{}' not contains right permissions", rptToken);
            }
        }
    }
    Response registerPermissionsResponse = prepareRegisterPermissionsResponse(patToken, resourceId, scopeIds);
    if (registerPermissionsResponse == null) {
        return authenticationFailure;
    }
    return new Pair<Boolean, Response>(true, registerPermissionsResponse);
}
Also used : Response(javax.ws.rs.core.Response) RptIntrospectionResponse(org.xdi.oxauth.model.uma.RptIntrospectionResponse) HttpResponse(org.apache.http.HttpResponse) RptIntrospectionResponse(org.xdi.oxauth.model.uma.RptIntrospectionResponse) UmaPermission(org.xdi.oxauth.model.uma.UmaPermission) LinkedList(java.util.LinkedList) Pair(org.xdi.util.Pair)

Example 4 with Pair

use of org.xdi.util.Pair in project oxTrust by GluuFederation.

the class SearchResourcesWebService method computeResults.

/**
 * Here we reuse every single POST search found in other web services, but handle serialization differently to a more
 * manual approach for performance reasons. In the end, this saves us 3 deserializations and 3 serializations of
 * multiple results packs.
 * Result set as a whole will not be sorted by sortBy param but every group of resources (by resource type) will be
 * sorted as such
 * @param searchRequest
 * @param resources
 * @return
 */
private Pair<Integer, Integer> computeResults(SearchRequest searchRequest, List<JsonNode> resources) throws Exception {
    int i;
    int totalInPage = 0, totalResults = 0, skip = 0;
    boolean resultsAvailable = false;
    Integer startIndex_ = searchRequest.getStartIndex();
    JsonNode tree = null;
    // Move forward to skip the searches that might have no results and find the first one starting at index = searchRequest.getStartIndex()
    for (i = 0; i < NUM_RESOURCE_TYPES && !resultsAvailable; i++) {
        tree = getListResponseTree(i, searchRequest);
        if (tree != null) {
            totalResults += tree.get("totalResults").asInt();
            if (totalResults > 0) {
                if (totalResults >= startIndex_) {
                    // when null, it means searchRequest.getCount() was zero or empty page
                    resultsAvailable = tree.get("itemsPerPage") != null;
                    if (searchRequest.getStartIndex() == 1)
                        skip = startIndex_ - (totalResults - tree.get("totalResults").asInt()) - 1;
                }
                // Adjust startindex of subsequent searches to 1
                searchRequest.setStartIndex(1);
            }
        }
    }
    if (resultsAvailable) {
        // Accumulate till we have searchRequest.getCount() results or exhaust data
        Iterator<JsonNode> iterator = tree.get("Resources").getElements();
        while (iterator.hasNext() && totalInPage < searchRequest.getCount()) {
            if (skip == 0) {
                totalInPage++;
                resources.add(iterator.next());
            } else {
                skip--;
                iterator.next();
            }
        }
        while (i < NUM_RESOURCE_TYPES && totalInPage < searchRequest.getCount()) {
            resultsAvailable = false;
            tree = getListResponseTree(i, searchRequest);
            if (tree != null) {
                totalResults += tree.get("totalResults").asInt();
                if (tree.get("totalResults").asInt() > 0)
                    resultsAvailable = tree.get("itemsPerPage") != null;
            }
            if (resultsAvailable) {
                for (iterator = tree.get("Resources").getElements(); iterator.hasNext() && totalInPage < searchRequest.getCount(); totalInPage++) resources.add(iterator.next());
            }
            i++;
        }
        // Continue the remainder of searches to just compute final value for totalResults
        while (i < NUM_RESOURCE_TYPES) {
            tree = getListResponseTree(i, searchRequest);
            if (tree != null)
                totalResults += tree.get("totalResults").asInt();
            i++;
        }
    }
    // Revert startIndex to original
    searchRequest.setStartIndex(startIndex_);
    return new Pair<Integer, Integer>(totalInPage, totalResults);
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) Pair(org.xdi.util.Pair)

Example 5 with Pair

use of org.xdi.util.Pair in project oxTrust by GluuFederation.

the class CacheRefreshTimer method removeTargetEntries.

private Pair<List<String>, List<String>> removeTargetEntries(LdapServerConnection inumDbServerConnection, LdapEntryManager targetLdapEntryManager, List<GluuSimplePerson> removedPersons, HashMap<String, GluuInumMap> inumInumMap) {
    String runDate = ldapEntryManager.encodeGeneralizedTime(new Date(this.lastFinishedTime));
    LdapEntryManager inumDbLdapEntryManager = inumDbServerConnection.getLdapEntryManager();
    List<String> result1 = new ArrayList<String>();
    List<String> result2 = new ArrayList<String>();
    for (GluuSimplePerson removedPerson : removedPersons) {
        String inum = removedPerson.getAttribute(OxTrustConstants.inum);
        // Update GluuInumMap if it exist
        GluuInumMap currentInumMap = inumInumMap.get(inum);
        if (currentInumMap == null) {
            log.warn("Can't find inum entry of person with DN: {}", removedPerson.getDn());
        } else {
            GluuInumMap removedInumMap = getMarkInumMapEntryAsRemoved(currentInumMap, runDate);
            try {
                inumDbLdapEntryManager.merge(removedInumMap);
                result2.add(removedInumMap.getInum());
            } catch (BaseMappingException ex) {
                log.error("Failed to update entry with inum '{}' and DN: {}", currentInumMap.getInum(), currentInumMap.getDn(), ex);
                continue;
            }
        }
        // Remove person from target server
        try {
            targetLdapEntryManager.removeRecursively(removedPerson.getDn());
            result1.add(inum);
        } catch (BaseMappingException ex) {
            log.error("Failed to remove person entry with inum '{}' and DN: {}", inum, removedPerson.getDn(), ex);
            continue;
        }
        log.debug("Person with DN: '{}' removed from target server", removedPerson.getDn());
    }
    return new Pair<List<String>, List<String>>(result1, result2);
}
Also used : GluuSimplePerson(org.gluu.oxtrust.ldap.cache.model.GluuSimplePerson) BaseMappingException(org.gluu.persist.exception.mapping.BaseMappingException) GluuInumMap(org.gluu.oxtrust.ldap.cache.model.GluuInumMap) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) ArrayList(java.util.ArrayList) Date(java.util.Date) Pair(org.xdi.util.Pair)

Aggregations

Pair (org.xdi.util.Pair)8 Response (javax.ws.rs.core.Response)4 HttpResponse (org.apache.http.HttpResponse)2 RptIntrospectionResponse (org.xdi.oxauth.model.uma.RptIntrospectionResponse)2 UmaPermission (org.xdi.oxauth.model.uma.UmaPermission)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 Attribute (com.unboundid.ldap.sdk.Attribute)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)1 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)1 AttributeTypeDefinition (com.unboundid.ldap.sdk.schema.AttributeTypeDefinition)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 JsonNode (org.codehaus.jackson.JsonNode)1 GluuInumMap (org.gluu.oxtrust.ldap.cache.model.GluuInumMap)1 GluuSimplePerson (org.gluu.oxtrust.ldap.cache.model.GluuSimplePerson)1 BulkResponse (org.gluu.oxtrust.model.scim2.bulk.BulkResponse)1 FidoDeviceResource (org.gluu.oxtrust.model.scim2.fido.FidoDeviceResource)1