Search in sources :

Example 1 with GroupNamePrincipal

use of org.ow2.proactive.authentication.principals.GroupNamePrincipal in project scheduling by ow2-proactive.

the class Client method getGroups.

public Set<String> getGroups() {
    Set<String> answer = new HashSet<>();
    Set<GroupNamePrincipal> groupPrincipals = subject.getPrincipals(GroupNamePrincipal.class);
    for (GroupNamePrincipal principal : groupPrincipals) {
        answer.add(principal.getName());
    }
    return answer;
}
Also used : GroupNamePrincipal(org.ow2.proactive.authentication.principals.GroupNamePrincipal) HashSet(java.util.HashSet)

Example 2 with GroupNamePrincipal

use of org.ow2.proactive.authentication.principals.GroupNamePrincipal in project scheduling by ow2-proactive.

the class UserIdentificationImpl method getGroups.

@Override
public Set<String> getGroups() {
    Set<String> answer = new HashSet<>();
    Set<GroupNamePrincipal> groupPrincipals = subject.getPrincipals(GroupNamePrincipal.class);
    for (GroupNamePrincipal principal : groupPrincipals) {
        answer.add(principal.getName());
    }
    return answer;
}
Also used : GroupNamePrincipal(org.ow2.proactive.authentication.principals.GroupNamePrincipal) HashSet(java.util.HashSet)

Example 3 with GroupNamePrincipal

use of org.ow2.proactive.authentication.principals.GroupNamePrincipal in project scheduling by ow2-proactive.

the class FileLoginModule method groupMembershipFromFile.

/**
 * Return corresponding group for an user from the group file.
 * @param username user's login
 * @throws LoginException if group file is not found or unreadable.
 */
protected void groupMembershipFromFile(String username) throws LoginException {
    try (FileInputStream stream = new FileInputStream(groupFile)) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] u2g = line.split(":");
            if (u2g[0].trim().equals(username)) {
                subject.getPrincipals().add(new GroupNamePrincipal(u2g[1]));
                logger.debug("adding group principal '" + u2g[1] + "' for user '" + username + "'");
            }
        }
    } catch (FileNotFoundException e) {
        throw new LoginException(e.toString());
    } catch (IOException e) {
        throw new LoginException(e.toString());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) GroupNamePrincipal(org.ow2.proactive.authentication.principals.GroupNamePrincipal) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 4 with GroupNamePrincipal

use of org.ow2.proactive.authentication.principals.GroupNamePrincipal in project scheduling by ow2-proactive.

the class LDAPLoginModule method getLDAPUserDN.

/**
 * Connects anonymously to the LDAP server <code>url</code> and retrieve
 * DN of the user <code>username</code>
 *
 * <p>
 * @exception NamingException
 *                if a naming exception is encountered.
 * <p>
 *
 * @return the String containing the UID of the user or null if the user is
 *         not found.
 */
private String getLDAPUserDN(String username) throws NamingException {
    String userDN = null;
    DirContext ctx = null;
    try {
        // Create the initial directory context
        ctx = this.connectAndGetContext();
        SearchControls sControl = new SearchControls();
        sControl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String filter = String.format(ldapProperties.getProperty(LDAPProperties.LDAP_USER_FILTER), username);
        // looking for the user dn (distinguish name)
        NamingEnumeration<SearchResult> answer = ctx.search(USERS_DN, filter, sControl);
        if (answer.hasMoreElements()) {
            SearchResult result = (SearchResult) answer.next();
            userDN = result.getNameInNamespace();
            if (logger.isDebugEnabled()) {
                logger.debug("User " + username + " has LDAP entry " + userDN);
            }
            subject.getPrincipals().add(new UserNamePrincipal(username));
            // looking for the user groups
            String groupFilter = String.format(ldapProperties.getProperty(LDAPProperties.LDAP_GROUP_FILTER), userDN);
            NamingEnumeration<SearchResult> groupResults = ctx.search(GROUPS_DN, groupFilter, sControl);
            while (groupResults.hasMoreElements()) {
                SearchResult res = (SearchResult) groupResults.next();
                Attribute attr = res.getAttributes().get(ldapProperties.getProperty(LDAPProperties.LDAP_GROUPNAME_ATTR));
                if (attr != null) {
                    String groupName = attr.get().toString();
                    subject.getPrincipals().add(new GroupNamePrincipal(groupName));
                    if (logger.isDebugEnabled()) {
                        logger.debug("User " + username + " is a member of group " + groupName);
                    }
                }
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("User DN not found");
            }
        }
    } catch (NamingException e) {
        logger.error("Problem with the search in mode: " + AUTHENTICATION_METHOD + e);
        throw e;
    } finally {
        try {
            if (ctx != null) {
                ctx.close();
            }
        } catch (NamingException e) {
            logger.error("", e);
            logger.error("Problem closing LDAP connection: " + e.getMessage());
        }
    }
    return userDN;
}
Also used : UserNamePrincipal(org.ow2.proactive.authentication.principals.UserNamePrincipal) Attribute(javax.naming.directory.Attribute) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) GroupNamePrincipal(org.ow2.proactive.authentication.principals.GroupNamePrincipal)

Aggregations

GroupNamePrincipal (org.ow2.proactive.authentication.principals.GroupNamePrincipal)4 HashSet (java.util.HashSet)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 DirContext (javax.naming.directory.DirContext)1 InitialDirContext (javax.naming.directory.InitialDirContext)1 SearchControls (javax.naming.directory.SearchControls)1 SearchResult (javax.naming.directory.SearchResult)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 LoginException (javax.security.auth.login.LoginException)1 UserNamePrincipal (org.ow2.proactive.authentication.principals.UserNamePrincipal)1