Search in sources :

Example 41 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project perun by CESNET.

the class PerunUserImpl method addAsFacilityAdmin.

@Override
public void addAsFacilityAdmin(User user, Facility facility) {
    DirContextOperations entry = findByDN(buildDN(user));
    Name facilityDN = addBaseDN(perunFacility.getEntryDN(String.valueOf(facility.getId())));
    entry.addAttributeValue(PerunAttribute.PerunAttributeNames.ldapAttrAdminOfFacility, facilityDN.toString());
    ldapTemplate.modifyAttributes(entry);
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) Name(javax.naming.Name)

Example 42 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project perun by CESNET.

the class PerunUserImpl method addAsVoAdmin.

@Override
public void addAsVoAdmin(User user, Vo vo) {
    DirContextOperations entry = findByDN(buildDN(user));
    Name voDN = addBaseDN(perunVO.getEntryDN(String.valueOf(vo.getId())));
    entry.addAttributeValue(PerunAttribute.PerunAttributeNames.ldapAttrAdminOfVo, voDN.toString());
    ldapTemplate.modifyAttributes(entry);
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) Name(javax.naming.Name)

Example 43 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project hub-alert by blackducksoftware.

the class MappingLdapAuthoritiesPopulatorTest method testEmptyAdditionalRoles.

@Test
public void testEmptyAdditionalRoles() {
    UserManagementAuthoritiesPopulator authoritiesPopulator = Mockito.mock(UserManagementAuthoritiesPopulator.class);
    Mockito.doReturn(Set.of(new SimpleGrantedAuthority(DefaultUserRole.ALERT_USER.name()))).when(authoritiesPopulator).addAdditionalRoles(Mockito.anyString(), Mockito.anySet());
    ContextSource contextSource = Mockito.mock(ContextSource.class);
    DirContextOperations user = Mockito.mock(DirContextOperations.class);
    MappingLdapAuthoritiesPopulator ldapAuthoritiesPopulator = new MappingLdapAuthoritiesPopulator(contextSource, null, authoritiesPopulator);
    Set<GrantedAuthority> actualRoles = ldapAuthoritiesPopulator.getAdditionalRoles(user, "");
    boolean hasAlertUserRole = actualRoles.stream().map(GrantedAuthority::getAuthority).allMatch(roleName -> DefaultUserRole.ALERT_USER.name().equals(roleName));
    Mockito.verify(authoritiesPopulator).addAdditionalRoles(Mockito.anyString(), Mockito.anySet());
    assertFalse(actualRoles.isEmpty());
    assertTrue(hasAlertUserRole);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DirContextOperations(org.springframework.ldap.core.DirContextOperations) UserManagementAuthoritiesPopulator(com.synopsys.integration.alert.component.authentication.security.UserManagementAuthoritiesPopulator) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ContextSource(org.springframework.ldap.core.ContextSource) Test(org.junit.jupiter.api.Test)

Example 44 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project kylo by Teradata.

the class ActiveDirectoryAuthenticationProvider method authenticate.

/* (non-Javadoc)
     * @see org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider#authenticate(org.springframework.security.core.Authentication)
     */
@Override
public Authentication authenticate(Authentication authentication) throws org.springframework.security.core.AuthenticationException {
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, this.messages.getMessage("LdapAuthenticationProvider.onlySupports", "Only UsernamePasswordAuthenticationToken is supported"));
    final UsernamePasswordAuthenticationToken userToken = (UsernamePasswordAuthenticationToken) authentication;
    final UsernamePasswordAuthenticationToken authToken = this.serviceToken != null ? this.serviceToken : userToken;
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Processing authentication request for user: " + userToken.getName());
    }
    if (!StringUtils.hasLength(userToken.getName())) {
        throw new BadCredentialsException(this.messages.getMessage("LdapAuthenticationProvider.emptyUsername", "Empty Username"));
    }
    String credentials = String.valueOf((char[]) authToken.getCredentials());
    if (!StringUtils.hasLength(credentials)) {
        throw new BadCredentialsException(this.messages.getMessage("AbstractLdapAuthenticationProvider.emptyPassword", "Empty Password"));
    }
    DirContextOperations userData = doAuthentication(userToken);
    Collection<? extends GrantedAuthority> authorities = loadUserAuthorities(userData, authToken.getName(), credentials);
    UserDetails user = this.userDetailsContextMapper.mapUserFromContext(userData, userToken.getName(), authorities);
    return createSuccessfulAuthentication(userToken, user);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) DirContextOperations(org.springframework.ldap.core.DirContextOperations) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 45 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project cxf by apache.

the class LdapUtils method getDnOfEntry.

public static Name getDnOfEntry(LdapTemplate ldapTemplate, String baseDN, String objectClass, String filterAttributeName, String filterAttributeValue) {
    ContextMapper<Name> mapper = new AbstractContextMapper<Name>() {

        public Name doMapFromContext(DirContextOperations ctx) {
            return ctx.getDn();
        }
    };
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", objectClass)).and(new EqualsFilter(filterAttributeName, filterAttributeValue));
    List<Name> result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(), SearchControls.SUBTREE_SCOPE, mapper);
    if (result != null && !result.isEmpty()) {
        // not only the first one....
        return result.get(0);
    }
    return null;
}
Also used : AbstractContextMapper(org.springframework.ldap.core.support.AbstractContextMapper) AndFilter(org.springframework.ldap.filter.AndFilter) DirContextOperations(org.springframework.ldap.core.DirContextOperations) EqualsFilter(org.springframework.ldap.filter.EqualsFilter) Name(javax.naming.Name)

Aggregations

DirContextOperations (org.springframework.ldap.core.DirContextOperations)89 Name (javax.naming.Name)20 Test (org.junit.jupiter.api.Test)13 Test (org.junit.Test)9 UserDetails (org.springframework.security.core.userdetails.UserDetails)9 HashSet (java.util.HashSet)8 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)8 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)8 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)7 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)7 FilterBasedLdapUserSearch (org.springframework.security.ldap.search.FilterBasedLdapUserSearch)7 LdapConfig (com.thoughtworks.go.config.LdapConfig)6 BaseConfig (com.thoughtworks.go.config.server.security.ldap.BaseConfig)6 BasesConfig (com.thoughtworks.go.config.server.security.ldap.BasesConfig)6 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 NameNotFoundException (org.springframework.ldap.NameNotFoundException)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)5 PerunAttribute (cz.metacentrum.perun.ldapc.model.PerunAttribute)4 List (java.util.List)4