Search in sources :

Example 1 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method deleteVo.

public void deleteVo(Vo vo) throws InternalErrorException {
    try {
        ldapTemplate.unbind(getVoDNByVoId(String.valueOf(vo.getId())));
        log.debug("Entry deleted from LDAP: Vo {}.", vo);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : NameNotFoundException(org.springframework.ldap.NameNotFoundException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method createResource.

//------------------RESOURCE MODIFICATION METHODS----------------------------
public void createResource(Resource resource, String entityID) throws InternalErrorException {
    // Create a set of attributes
    Attributes attributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute objClasses = new BasicAttribute("objectClass");
    objClasses.add("top");
    objClasses.add("perunResource");
    // Add attributes
    attributes.put(objClasses);
    attributes.put("cn", resource.getName());
    attributes.put("perunResourceId", String.valueOf(resource.getId()));
    attributes.put("perunFacilityId", String.valueOf(resource.getFacilityId()));
    attributes.put("perunVoId", String.valueOf(resource.getVoId()));
    if (resource.getDescription() != null && !resource.getDescription().isEmpty())
        attributes.put("description", resource.getDescription());
    // get info about entityID attribute if exists
    if (entityID != null)
        attributes.put("entityID", entityID);
    // Create the entry
    try {
        ldapTemplate.bind(getResourceDN(String.valueOf(resource.getVoId()), String.valueOf(resource.getId())), null, attributes);
        log.debug("New entry created in LDAP: Resource {} in Vo with Id=" + resource.getVoId() + " and Facility with ID=" + resource.getFacilityId() + ".", resource);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 3 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method addGroup.

//------------------GROUP MODIFICATION METHODS-------------------------------
public void addGroup(Group group) throws InternalErrorException {
    // Create a set of attributes
    Attributes attributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute objClasses = new BasicAttribute("objectClass");
    objClasses.add("top");
    objClasses.add("perunGroup");
    // Add attributes
    attributes.put(objClasses);
    attributes.put("cn", group.getName());
    attributes.put("perunGroupId", String.valueOf(group.getId()));
    attributes.put("perunUniqueGroupName", new String(this.getVoShortName(group.getVoId()) + ":" + group.getName()));
    attributes.put("perunVoId", String.valueOf(group.getVoId()));
    if (group.getDescription() != null && !group.getDescription().isEmpty())
        attributes.put("description", group.getDescription());
    if (group.getParentGroupId() != null) {
        attributes.put("perunParentGroup", "perunGroupId=" + group.getParentGroupId().toString() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase());
        attributes.put("perunParentGroupId", group.getParentGroupId().toString());
    }
    // Create the entry
    try {
        ldapTemplate.bind(getGroupDN(String.valueOf(group.getVoId()), String.valueOf(group.getId())), null, attributes);
        log.debug("New entry created in LDAP: Group {} in Vo with Id=" + group.getVoId() + ".", group);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 4 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project perun by CESNET.

the class LdapConnectorImpl method createVo.

//--------------------------VO MODIFICATION METHODS---------------------------
public void createVo(Vo vo) throws InternalErrorException {
    // Create a set of attributes for vo
    Attributes voAttributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute voObjClasses = new BasicAttribute("objectClass");
    voObjClasses.add("top");
    voObjClasses.add("organization");
    voObjClasses.add("perunVO");
    // Add attributes
    voAttributes.put(voObjClasses);
    voAttributes.put("o", vo.getShortName());
    voAttributes.put("description", vo.getName());
    voAttributes.put("perunVoId", String.valueOf(vo.getId()));
    // Create the entires
    try {
        ldapTemplate.bind(getVoDNByVoId(String.valueOf(vo.getId())), null, voAttributes);
        log.debug("New entry created in LDAP: Vo {}.", vo);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 5 with NameNotFoundException

use of org.springframework.ldap.NameNotFoundException in project spring-security by spring-projects.

the class PasswordComparisonAuthenticator method authenticate.

@Override
public DirContextOperations authenticate(final Authentication authentication) {
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, "Can only process UsernamePasswordAuthenticationToken objects");
    // locate the user and check the password
    DirContextOperations user = null;
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();
    SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(getContextSource());
    for (String userDn : getUserDns(username)) {
        try {
            user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
        } catch (NameNotFoundException ignore) {
            logger.trace(LogMessage.format("Failed to retrieve user with %s", userDn), ignore);
        }
        if (user != null) {
            break;
        }
    }
    if (user == null) {
        logger.debug(LogMessage.of(() -> "Failed to retrieve user with any user DNs " + getUserDns(username)));
    }
    if (user == null && getUserSearch() != null) {
        logger.trace("Searching for user using " + getUserSearch());
        user = getUserSearch().searchForUser(username);
        if (user == null) {
            logger.debug("Failed to find user using " + getUserSearch());
        }
    }
    if (user == null) {
        throw new UsernameNotFoundException("User not found: " + username);
    }
    if (logger.isTraceEnabled()) {
        logger.trace(LogMessage.format("Comparing password attribute '%s' for user '%s'", this.passwordAttributeName, user.getDn()));
    }
    if (this.usePasswordAttrCompare && isPasswordAttrCompare(user, password)) {
        logger.debug(LogMessage.format("Locally matched password attribute '%s' for user '%s'", this.passwordAttributeName, user.getDn()));
        return user;
    }
    if (isLdapPasswordCompare(user, ldapTemplate, password)) {
        logger.debug(LogMessage.format("LDAP-matched password attribute '%s' for user '%s'", this.passwordAttributeName, user.getDn()));
        return user;
    }
    throw new BadCredentialsException(this.messages.getMessage("PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) DirContextOperations(org.springframework.ldap.core.DirContextOperations) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Aggregations

NameNotFoundException (org.springframework.ldap.NameNotFoundException)14 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)10 Attribute (javax.naming.directory.Attribute)5 BasicAttribute (javax.naming.directory.BasicAttribute)5 Attributes (javax.naming.directory.Attributes)4 BasicAttributes (javax.naming.directory.BasicAttributes)4 DirContextOperations (org.springframework.ldap.core.DirContextOperations)4 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 SpringSecurityLdapTemplate (org.springframework.security.ldap.SpringSecurityLdapTemplate)2 Attribute (cz.metacentrum.perun.core.api.Attribute)1 PerunAttribute (cz.metacentrum.perun.ldapc.model.PerunAttribute)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 ModificationItem (javax.naming.directory.ModificationItem)1