Search in sources :

Example 16 with IdSearchControl

use of com.sun.identity.idm.IdSearchControl in project OpenAM by OpenRock.

the class OpenAMClientDAO method read.

/**
     * {@inheritDoc}
     */
public Client read(String clientId, OAuth2Request request) throws UnauthorizedClientException {
    Map<String, Set<String>> clientAttributes = new HashMap<String, Set<String>>();
    try {
        AMIdentity theID = null;
        final SSOToken token = AccessController.doPrivileged(AdminTokenAction.getInstance());
        final String realm = request.getParameter(OAuth2Constants.Custom.REALM);
        AMIdentityRepository repo = idRepoFactory.create(realm, token);
        IdSearchControl idsc = new IdSearchControl();
        idsc.setRecursive(true);
        idsc.setAllReturnAttributes(true);
        // search for the identity
        Set<AMIdentity> results;
        idsc.setMaxResults(0);
        IdSearchResults searchResults = repo.searchIdentities(IdType.AGENTONLY, clientId, idsc);
        results = searchResults.getSearchResults();
        if (results == null || results.size() != 1) {
            logger.error("OpenAMClientDAO.read(): No client profile or more than one profile found.");
            throw new UnauthorizedClientException("Not able to get client from OpenAM");
        }
        theID = results.iterator().next();
        //if the client is deactivated return null
        if (!theID.isActive()) {
            theID = null;
        } else {
            clientAttributes = theID.getAttributes();
        }
    } catch (UnauthorizedClientException e) {
        logger.error("OpenAMClientDAO.read(): Unable to get client AMIdentity: ", e);
        throw new UnauthorizedClientException("Not able to get client from OpenAM");
    } catch (SSOException e) {
        logger.error("OpenAMClientDAO.read(): Unable to get client AMIdentity: ", e);
        throw new UnauthorizedClientException("Not able to get client from OpenAM");
    } catch (IdRepoException e) {
        logger.error("OpenAMClientDAO.read(): Unable to get client AMIdentity: ", e);
        throw new UnauthorizedClientException("Not able to get client from OpenAM");
    }
    Client client = createClient(clientAttributes);
    client.setClientID(clientId);
    return client;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SSOToken(com.iplanet.sso.SSOToken) HashMap(java.util.HashMap) IdSearchResults(com.sun.identity.idm.IdSearchResults) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) AMIdentity(com.sun.identity.idm.AMIdentity) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdSearchControl(com.sun.identity.idm.IdSearchControl) OAuth2Client(org.forgerock.oauth2.core.OAuth2Constants.OAuth2Client) Client(org.forgerock.openidconnect.Client)

Example 17 with IdSearchControl

use of com.sun.identity.idm.IdSearchControl in project OpenAM by OpenRock.

the class IdentityServicesImpl method fetchAMIdentities.

private List<AMIdentity> fetchAMIdentities(IdType type, CrestQuery crestQuery, boolean fetchAllAttrs, AMIdentityRepository repo, Map searchModifiers) throws IdRepoException, ObjectNotFound, SSOException {
    IdSearchControl searchControl = new IdSearchControl();
    IdSearchResults searchResults;
    List<AMIdentity> identities;
    if (isOperationSupported(repo, type, IdOperation.READ)) {
        Set<AMIdentity> resultSet;
        if (fetchAllAttrs) {
            searchControl.setAllReturnAttributes(true);
        } else {
            searchControl.setAllReturnAttributes(false);
        }
        if (searchModifiers != null) {
            searchControl.setSearchModifiers(IdSearchOpModifier.AND, searchModifiers);
        }
        searchResults = repo.searchIdentities(type, crestQuery, searchControl);
        resultSet = searchResults.getSearchResults();
        identities = new ArrayList<>(resultSet);
    } else {
        // A list is expected back
        /*
             * TODO: throw an exception instead of returning an empty list
             */
        identities = new ArrayList<>();
    }
    return identities;
}
Also used : IdSearchResults(com.sun.identity.idm.IdSearchResults) AMIdentity(com.sun.identity.idm.AMIdentity) IdSearchControl(com.sun.identity.idm.IdSearchControl)

Example 18 with IdSearchControl

use of com.sun.identity.idm.IdSearchControl in project OpenAM by OpenRock.

the class LdapSPValidator method searchAgents.

private Map searchAgents(StringBuffer rootPrefix, String realm) throws Exception {
    /*
         * Search for attribute "sunIdentityServerDeviceKeyValue:
         * sunIdentityServerAgentRootURL=<rootURL>"
         */
    Map searchParams = new HashMap();
    Set attrValues = new HashSet(2);
    attrValues.add(PROVIDER_ID_ATTR_NAME + "=" + rootPrefix.toString());
    searchParams.put(LDAP_ATTR_NAME, attrValues);
    IdSearchControl idsc = new IdSearchControl();
    idsc.setTimeOut(0);
    idsc.setMaxResults(0);
    idsc.setSearchModifiers(IdSearchOpModifier.AND, searchParams);
    Set returnAttrs = new HashSet(4);
    returnAttrs.add(LDAP_ATTR_NAME);
    returnAttrs.add(LDAP_STATUS_ATTR_NAME);
    idsc.setReturnAttributes(returnAttrs);
    try {
        SSOToken adminToken = (SSOToken) AccessController.doPrivileged(AdminTokenAction.getInstance());
        IdSearchResults sr = null;
        if ((realm != null) && (realm.trim().length() > 0)) {
            AMIdentityRepository idRepo = new AMIdentityRepository(adminToken, realm);
            sr = idRepo.searchIdentities(IdType.AGENT, "*", idsc);
        } else {
            sr = amIdRepo.searchIdentities(IdType.AGENT, "*", idsc);
        }
        return sr.getResultAttributes();
    } catch (IdRepoException ire) {
        CDCServlet.debug.error("LdapSPValidator.searchAgents", ire);
        throw new Exception(ire);
    } catch (SSOException ssoe) {
        CDCServlet.debug.error("LdapSPValidator.searchAgents", ssoe);
        throw new Exception(ssoe);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SSOToken(com.iplanet.sso.SSOToken) HashMap(java.util.HashMap) IdSearchResults(com.sun.identity.idm.IdSearchResults) IdSearchControl(com.sun.identity.idm.IdSearchControl) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) HashMap(java.util.HashMap) Map(java.util.Map) IdRepoException(com.sun.identity.idm.IdRepoException) MalformedURLException(java.net.MalformedURLException) SSOException(com.iplanet.sso.SSOException) HashSet(java.util.HashSet)

Example 19 with IdSearchControl

use of com.sun.identity.idm.IdSearchControl in project OpenAM by OpenRock.

the class DelegationPolicyImpl method getPermissions.

/**
     * Returns a set of permissions that a user has.
     * 
     * @param token sso token of the user requesting permissions
     * @param orgName The name of the realm from which the delegation 
     *        permissions are fetched.
     * 
     * @return a <code>Set</code> of permissions that a user has
     * 
     * @throws SSOException if single-sign-on token invalid or expired
     * @throws DelegationException for any other abnormal condition
     */
public Set getPermissions(SSOToken token, String orgName) throws SSOException, DelegationException {
    DelegationPrivilege dp;
    Set perms = new HashSet();
    Set subjects;
    AMIdentity userIdentity = null;
    AMIdentity subjectIdentity = null;
    IdSearchResults results = null;
    if (token == null) {
        if (DelegationManager.debug.warningEnabled()) {
            DelegationManager.debug.warning("DelegationPolicyImpl.getPermissions():" + "user sso token is null");
        }
        return perms;
    }
    try {
        userIdentity = IdUtils.getIdentity(token);
        if (userIdentity == null) {
            if (DelegationManager.debug.warningEnabled()) {
                DelegationManager.debug.warning("DelegationPolicyImpl.getPermissions():" + "could not get user's identity from token");
            }
            return perms;
        }
        Set privileges = getPrivileges(appToken, orgName);
        if ((privileges != null) && (!privileges.isEmpty())) {
            AMIdentityRepository idRepo = new AMIdentityRepository(appToken, orgName);
            IdSearchControl ctrl = new IdSearchControl();
            ctrl.setRecursive(true);
            ctrl.setMaxResults(-1);
            ctrl.setTimeOut(-1);
            Iterator it = privileges.iterator();
            while (it.hasNext()) {
                dp = (DelegationPrivilege) it.next();
                subjects = dp.getSubjects();
                if ((subjects != null) && (!subjects.isEmpty())) {
                    Iterator sit = subjects.iterator();
                    while (sit.hasNext()) {
                        String subject = (String) sit.next();
                        String subjectId = LDAPUtils.rdnValueFromDn(subject);
                        if (subjectId != null) {
                            results = idRepo.searchIdentities(IdType.ROLE, subjectId, ctrl);
                            if (results != null) {
                                Set idSet = results.getSearchResults();
                                if ((idSet != null) && !idSet.isEmpty()) {
                                    subjectIdentity = (AMIdentity) (idSet.iterator().next());
                                    if (userIdentity.isMember(subjectIdentity)) {
                                        perms.addAll(dp.getPermissions());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new DelegationException(e);
    }
    return perms;
}
Also used : DelegationPrivilege(com.sun.identity.delegation.DelegationPrivilege) Set(java.util.Set) HashSet(java.util.HashSet) IdSearchResults(com.sun.identity.idm.IdSearchResults) AMIdentity(com.sun.identity.idm.AMIdentity) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdSearchControl(com.sun.identity.idm.IdSearchControl) Iterator(java.util.Iterator) DelegationException(com.sun.identity.delegation.DelegationException) DelegationException(com.sun.identity.delegation.DelegationException) PolicyException(com.sun.identity.policy.PolicyException) SSOException(com.iplanet.sso.SSOException) IdRepoException(com.sun.identity.idm.IdRepoException) HashSet(java.util.HashSet)

Example 20 with IdSearchControl

use of com.sun.identity.idm.IdSearchControl in project OpenAM by OpenRock.

the class DelegationPolicyImpl method getSubjects.

/**
     * Returns a set of selected subjects of specified types matching the
     * pattern in the given realm. The pattern accepts "*" as the wild card for
     * searching subjects. For example, "a*c" matches with any subject starting
     * with a and ending with c.
     * 
     * @param token The <code>SSOToken</code> of the requesting user
     * @param orgName The name of the realm from which the subjects are fetched.
     * @param types a set of subject types. e.g. ROLE, GROUP.
     * @param pattern a filter used to select the subjects.
     * 
     * @return a set of subjects associated with the realm.
     * 
     * @throws SSOException invalid or expired single-sign-on token
     * @throws DelegationException for any abnormal condition
     *
     * @return <code>Set</code> of universal Ids of the subjects associated 
     *         with the realm.
     *
     * @throws SSOException invalid or expired single-sign-on token
     * @throws DelegationException for any abnormal condition
     */
public Set getSubjects(SSOToken token, String orgName, Set types, String pattern) throws SSOException, DelegationException {
    Set results = new HashSet();
    // All Authenticated Users would be returned only if pattern is *
    if ((pattern != null) && pattern.equals("*")) {
        results.add(AUTHN_USERS_ID);
    }
    if (DelegationManager.debug.messageEnabled()) {
        DelegationManager.debug.message("DelegationPolicyImpl.getSubjects(): types=" + types);
    }
    try {
        AMIdentityRepository idRepo = new AMIdentityRepository(appToken, orgName);
        Set supportedTypes = idRepo.getSupportedIdTypes();
        if (DelegationManager.debug.messageEnabled()) {
            DelegationManager.debug.message("DelegationPolicyImpl.getSubjects(): " + "supported subject types=" + supportedTypes);
        }
        if ((supportedTypes != null) && (!supportedTypes.isEmpty()) && (types != null) && (!types.isEmpty())) {
            Iterator it = types.iterator();
            while (it.hasNext()) {
                IdType idType = IdUtils.getType((String) it.next());
                if (supportedTypes.contains(idType)) {
                    IdSearchControl ctrl = new IdSearchControl();
                    ctrl.setRecursive(true);
                    ctrl.setMaxResults(-1);
                    ctrl.setTimeOut(-1);
                    IdSearchResults idsr = idRepo.searchIdentities(idType, pattern, ctrl);
                    if (idsr != null) {
                        Set searchRes = idsr.getSearchResults();
                        if ((searchRes != null) && (!searchRes.isEmpty())) {
                            Iterator iter = searchRes.iterator();
                            while (iter.hasNext()) {
                                AMIdentity id = (AMIdentity) iter.next();
                                results.add(IdUtils.getUniversalId(id));
                            }
                        }
                    }
                }
            }
        }
        return results;
    } catch (IdRepoException ide) {
        throw new DelegationException(ide);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IdSearchResults(com.sun.identity.idm.IdSearchResults) AMIdentity(com.sun.identity.idm.AMIdentity) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) Iterator(java.util.Iterator) IdSearchControl(com.sun.identity.idm.IdSearchControl) IdRepoException(com.sun.identity.idm.IdRepoException) DelegationException(com.sun.identity.delegation.DelegationException) HashSet(java.util.HashSet) IdType(com.sun.identity.idm.IdType)

Aggregations

IdSearchControl (com.sun.identity.idm.IdSearchControl)48 IdSearchResults (com.sun.identity.idm.IdSearchResults)43 IdRepoException (com.sun.identity.idm.IdRepoException)41 SSOException (com.iplanet.sso.SSOException)36 AMIdentityRepository (com.sun.identity.idm.AMIdentityRepository)36 AMIdentity (com.sun.identity.idm.AMIdentity)35 Set (java.util.Set)25 HashSet (java.util.HashSet)20 SSOToken (com.iplanet.sso.SSOToken)15 Iterator (java.util.Iterator)14 IdType (com.sun.identity.idm.IdType)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)4 UnauthorizedClientException (org.forgerock.oauth2.core.exceptions.UnauthorizedClientException)4 CLIException (com.sun.identity.cli.CLIException)3 IOutput (com.sun.identity.cli.IOutput)3 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)2 DelegationException (com.sun.identity.delegation.DelegationException)2 TreeSet (java.util.TreeSet)2