* under the License.
*/
+import org.apache.archiva.redback.common.ldap.UserMapper;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.archiva.redback.authentication.AuthenticationDataSource;
import org.apache.archiva.redback.authentication.AuthenticationException;
import org.apache.archiva.redback.authentication.AuthenticationResult;
import org.apache.archiva.redback.authentication.Authenticator;
import org.apache.archiva.redback.authentication.PasswordBasedAuthenticationDataSource;
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapException;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
+import org.apache.archiva.redback.common.ldap.connection.LdapException;
import org.codehaus.plexus.redback.configuration.UserConfiguration;
import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
import org.slf4j.Logger;
<property name="password" value="secret"/>
</bean>
- <bean name="ldapConnectionFactory#configurable" class="org.codehaus.plexus.redback.common.ldap.connection.ConfigurableLdapConnectionFactory">
+ <bean name="ldapConnectionFactory#configurable" class="org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory">
<property name="hostname" value="localhost"/>
<property name="port" value="${ldapPort}"/>
<property name="baseDn" value="dc=redback,dc=plexus,dc=codehaus,dc=org"/>
<property name="userConf" ref="userConfiguration"/>
</bean>
- <bean name="userMapper#ldap" class="org.codehaus.plexus.redback.common.ldap.LdapUserMapper">
+ <bean name="userMapper#ldap" class="org.apache.archiva.redback.common.ldap.LdapUserMapper">
<property name="emailAttribute" value="mail"/>
<property name="fullNameAttribute" value="givenName"/>
<property name="passwordAttribute" value="userPassword"/>
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.users.User;
+
+import javax.naming.directory.Attributes;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+// TODO this class should be able to be replaced with a model
+public class LdapUser
+ implements User, Serializable
+{
+
+ private String key;
+
+ private String username;
+
+ private String fullName;
+
+ private String email;
+
+ private String encodedPassword;
+
+ private List<String> previousEncodedPasswords;
+
+ private boolean locked = false;
+
+ private boolean requiresPasswordChange = false;
+
+ private boolean permanent = true;
+
+ private boolean valid = true;
+
+ private Date creationDate = null;
+
+ private int failedLoginAttempts;
+
+ private Date lastLoginDate = null;
+
+ private Date lastPasswordChange = null;
+
+ // DO NOT STORE AS SUCH!!!
+ private String newPassword;
+
+ private Attributes originalAttributes;
+
+ public LdapUser( String username )
+ {
+ key = username;
+ this.username = username;
+ previousEncodedPasswords = new ArrayList<String>( 0 );
+ failedLoginAttempts = 0;
+ }
+
+ public LdapUser( String username, String fullName, String email )
+ {
+ this( username );
+ this.fullName = fullName;
+ this.email = email;
+ }
+
+ public LdapUser()
+ {
+ previousEncodedPasswords = new ArrayList<String>( 0 );
+ failedLoginAttempts = Integer.MIN_VALUE;
+ }
+
+ public void addPreviousEncodedPassword( String encodedPassword )
+ {
+ previousEncodedPasswords.add( encodedPassword );
+ }
+
+ public Date getAccountCreationDate()
+ {
+ return creationDate;
+ }
+
+ public int getCountFailedLoginAttempts()
+ {
+ return failedLoginAttempts;
+ }
+
+ public String getEmail()
+ {
+ return email;
+ }
+
+ public String getEncodedPassword()
+ {
+ return encodedPassword;
+ }
+
+ public String getFullName()
+ {
+ return fullName;
+ }
+
+ public Date getLastLoginDate()
+ {
+ return lastLoginDate;
+ }
+
+ public Date getLastPasswordChange()
+ {
+ return lastPasswordChange;
+ }
+
+ public String getPassword()
+ {
+ return newPassword;
+ }
+
+ public List<String> getPreviousEncodedPasswords()
+ {
+ return previousEncodedPasswords;
+ }
+
+ public Object getPrincipal()
+ {
+ return key;
+ }
+
+ public String getUsername()
+ {
+ return username;
+ }
+
+ public boolean isLocked()
+ {
+ return locked;
+ }
+
+ public boolean isPasswordChangeRequired()
+ {
+ return requiresPasswordChange;
+ }
+
+ public boolean isPermanent()
+ {
+ return permanent;
+ }
+
+ public boolean isValidated()
+ {
+ return valid;
+ }
+
+ public void setCountFailedLoginAttempts( int count )
+ {
+ failedLoginAttempts = count;
+ }
+
+ public void setEmail( String address )
+ {
+ email = address;
+ }
+
+ public void setEncodedPassword( String encodedPassword )
+ {
+ this.encodedPassword = encodedPassword;
+ }
+
+ public void setFullName( String name )
+ {
+ fullName = name;
+ }
+
+ public void setAccountCreationDate( Date date )
+ {
+ creationDate = date;
+ }
+
+ public void setLastLoginDate( Date date )
+ {
+ lastLoginDate = date;
+ }
+
+ public void setLastPasswordChange( Date passwordChangeDate )
+ {
+ lastPasswordChange = passwordChangeDate;
+ }
+
+ public void setLocked( boolean locked )
+ {
+ this.locked = locked;
+ }
+
+ public void setPassword( String rawPassword )
+ {
+ newPassword = rawPassword;
+ }
+
+ public void setPasswordChangeRequired( boolean changeRequired )
+ {
+ requiresPasswordChange = changeRequired;
+ }
+
+ public void setPermanent( boolean permanent )
+ {
+ this.permanent = permanent;
+ }
+
+ public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
+ {
+ previousEncodedPasswords = new ArrayList<String>( encodedPasswordList );
+ }
+
+ public void setUsername( String name )
+ {
+ username = name;
+ }
+
+ public void setValidated( boolean valid )
+ {
+ this.valid = valid;
+ }
+
+ public Attributes getOriginalAttributes()
+ {
+ return originalAttributes;
+ }
+
+ public void setOriginalAttributes( Attributes originalAttributes )
+ {
+ this.originalAttributes = originalAttributes;
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.users.User;
+import org.codehaus.plexus.redback.configuration.UserConfiguration;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttributes;
+import java.util.Date;
+
+/**
+ * @author <a href="jesse@codehaus.org"> jesse
+ * @version $Id$
+ */
+@Service( "userMapper#ldap" )
+public class LdapUserMapper
+ implements UserMapper
+{
+ /**
+ *
+ */
+ String emailAttribute = "mail";
+
+ /**
+ *
+ */
+ String fullNameAttribute = "givenName";
+
+ /**
+ *
+ */
+ String passwordAttribute = "userPassword";
+
+ /**
+ *
+ */
+ String userIdAttribute = "cn";
+
+ /**
+ *
+ */
+ String userBaseDn;
+
+ /**
+ *
+ */
+ String userObjectClass = "inetOrgPerson";
+
+ /**
+ *
+ */
+ String userFilter;
+
+ /**
+ *
+ */
+ int maxResultCount = 0;
+
+ @Inject
+ @Named( value = "userConfiguration" )
+ private UserConfiguration userConf;
+
+ @PostConstruct
+ public void initialize()
+ {
+ emailAttribute = userConf.getString( "ldap.config.mapper.attribute.email", emailAttribute );
+ fullNameAttribute = userConf.getString( "ldap.config.mapper.attribute.fullname", fullNameAttribute );
+ passwordAttribute = userConf.getString( "ldap.config.mapper.attribute.password", passwordAttribute );
+ userIdAttribute = userConf.getString( "ldap.config.mapper.attribute.user.id", userIdAttribute );
+ userBaseDn = userConf.getConcatenatedList( "ldap.config.mapper.attribute.user.base.dn",
+ userConf.getConcatenatedList( "ldap.config.base.dn", userBaseDn ) );
+ userObjectClass = userConf.getString( "ldap.config.mapper.attribute.user.object.class", userObjectClass );
+ userFilter = userConf.getString( "ldap.config.mapper.attribute.user.filter", userFilter );
+ maxResultCount = userConf.getInt( "ldap.config.max.result.count", maxResultCount );
+ }
+
+ public Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
+ throws MappingException
+ {
+ Attributes userAttrs = new BasicAttributes();
+
+ boolean passwordSet = false;
+
+ if ( !passwordSet && ( user.getEncodedPassword() != null ) )
+ {
+ userAttrs.put( getPasswordAttribute(), user.getEncodedPassword() );
+ }
+
+ if ( !StringUtils.isEmpty( user.getFullName() ) )
+ {
+ userAttrs.put( getUserFullNameAttribute(), user.getFullName() );
+ }
+
+ if ( !StringUtils.isEmpty( user.getEmail() ) )
+ {
+ userAttrs.put( getEmailAddressAttribute(), user.getEmail() );
+ }
+
+ return userAttrs;
+ }
+
+ public String getEmailAddressAttribute()
+ {
+ return emailAttribute;
+ }
+
+ public String getUserFullNameAttribute()
+ {
+ return fullNameAttribute;
+ }
+
+ public String getPasswordAttribute()
+ {
+ return passwordAttribute;
+ }
+
+ public String[] getUserAttributeNames()
+ {
+ return new String[]{ emailAttribute, fullNameAttribute, passwordAttribute, userIdAttribute };
+ }
+
+ public int getMaxResultCount()
+ {
+ return maxResultCount;
+ }
+
+ public UserUpdate getUpdate( LdapUser user )
+ throws MappingException
+ {
+
+ Attributes addAttrs = new BasicAttributes();
+
+ Attributes modAttrs = new BasicAttributes();
+
+ if ( !StringUtils.isEmpty( user.getFullName() ) )
+ {
+ if ( user.getFullName() == null )
+ {
+ addAttrs.put( getUserFullNameAttribute(), user.getFullName() );
+ }
+ else if ( !user.getFullName().equals( user.getFullName() ) )
+ {
+ modAttrs.put( getUserFullNameAttribute(), user.getFullName() );
+ }
+ }
+
+ if ( !StringUtils.isEmpty( user.getEmail() ) )
+ {
+ if ( user.getEmail() == null )
+ {
+ addAttrs.put( getEmailAddressAttribute(), user.getEmail() );
+ }
+ else if ( !user.getEmail().equals( user.getEmail() ) )
+ {
+ modAttrs.put( getEmailAddressAttribute(), user.getEmail() );
+ }
+ }
+
+ return null;
+ }
+
+ public LdapUser getUser( Attributes attributes )
+ throws MappingException
+ {
+ String userIdAttribute = getUserIdAttribute();
+ String emailAddressAttribute = getEmailAddressAttribute();
+ String nameAttribute = getUserFullNameAttribute();
+ String passwordAttribute = getPasswordAttribute();
+
+ String userId = ( LdapUtils.getAttributeValue( attributes, userIdAttribute, "username" ) );
+
+ LdapUser user = new LdapUser( userId );
+ user.setOriginalAttributes( attributes );
+
+ user.setEmail( LdapUtils.getAttributeValue( attributes, emailAddressAttribute, "email address" ) );
+ user.setFullName( LdapUtils.getAttributeValue( attributes, nameAttribute, "name" ) );
+
+ String encodedPassword = LdapUtils.getAttributeValueFromByteArray( attributes, passwordAttribute, "password" );
+
+ // it seems to be a common convention for the password to come back prepended with the encoding type..
+ // however we deal with that via configuration right now so just smoke it.
+ if ( encodedPassword != null && encodedPassword.startsWith( "{" ) )
+ {
+ encodedPassword = encodedPassword.substring( encodedPassword.indexOf( "}" ) + 1 );
+ }
+
+ user.setEncodedPassword( encodedPassword );
+
+ // REDBACK-215: skip NPE
+ user.setLastPasswordChange( new Date() );
+
+ return user;
+ }
+
+ public String getUserIdAttribute()
+ {
+ return userIdAttribute;
+ }
+
+ public String getEmailAttribute()
+ {
+ return emailAttribute;
+ }
+
+ public void setEmailAttribute( String emailAttribute )
+ {
+ this.emailAttribute = emailAttribute;
+ }
+
+ public String getFullNameAttribute()
+ {
+ return fullNameAttribute;
+ }
+
+ public void setFullNameAttribute( String fullNameAttribute )
+ {
+ this.fullNameAttribute = fullNameAttribute;
+ }
+
+ public void setMaxResultCount( int maxResultCount )
+ {
+ this.maxResultCount = maxResultCount;
+ }
+
+ public String getUserBaseDn()
+ {
+ return userBaseDn;
+ }
+
+ public void setUserBaseDn( String userBaseDn )
+ {
+ this.userBaseDn = userBaseDn;
+ }
+
+ public String getUserObjectClass()
+ {
+ return userObjectClass;
+ }
+
+ public String getUserFilter()
+ {
+ return userFilter;
+ }
+
+ public void setUserFilter( String userFilter )
+ {
+ this.userFilter = userFilter;
+ }
+
+ public void setUserObjectClass( String userObjectClass )
+ {
+ this.userObjectClass = userObjectClass;
+ }
+
+ public void setPasswordAttribute( String passwordAttribute )
+ {
+ this.passwordAttribute = passwordAttribute;
+ }
+
+ public void setUserIdAttribute( String userIdAttribute )
+ {
+ this.userIdAttribute = userIdAttribute;
+ }
+
+ public LdapUser newUserInstance( String username, String fullName, String email )
+ {
+ return new LdapUser( username, fullName, email );
+ }
+
+ public LdapUser newTemplateUserInstance()
+ {
+ return new LdapUser();
+ }
+
+ public String[] getReturningAttributes()
+ {
+ return new String[]{ getUserIdAttribute(), getEmailAttribute(), getFullNameAttribute(),
+ getPasswordAttribute() };
+ }
+
+ public UserConfiguration getUserConf()
+ {
+ return userConf;
+ }
+
+ public void setUserConf( UserConfiguration userConf )
+ {
+ this.userConf = userConf;
+ }
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+/**
+ *
+ * @version $Id$
+ */
+public final class LdapUtils
+{
+ private LdapUtils()
+ {
+ // no op
+ }
+
+ @SuppressWarnings("unchecked")
+ public static String getLabeledUriValue( Attributes attributes, String attrName, String label,
+ String attributeDescription )
+ throws MappingException
+ {
+ if ( attrName == null )
+ {
+ return null;
+ }
+
+ Attribute attribute = attributes.get( attrName );
+ if ( attribute != null )
+ {
+ NamingEnumeration attrs;
+ try
+ {
+ attrs = attribute.getAll();
+ }
+ catch ( NamingException e )
+ {
+ throw new MappingException(
+ "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
+ }
+
+ while ( attrs.hasMoreElements() )
+ {
+ Object value = attrs.nextElement();
+
+ String val = String.valueOf( value );
+
+ if ( val.endsWith( " " + label ) )
+ {
+ return val.substring( 0, val.length() - ( label.length() + 1 ) );
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public static String getAttributeValue( Attributes attributes, String attrName, String attributeDescription )
+ throws MappingException
+ {
+ if ( attrName == null )
+ {
+ return null;
+ }
+
+ Attribute attribute = attributes.get( attrName );
+ if ( attribute != null )
+ {
+ try
+ {
+ Object value = attribute.get();
+
+ return String.valueOf( value );
+ }
+ catch ( NamingException e )
+ {
+ throw new MappingException(
+ "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
+ }
+ }
+
+ return null;
+ }
+
+ public static String getAttributeValueFromByteArray( Attributes attributes, String attrName,
+ String attributeDescription )
+ throws MappingException
+ {
+ if ( attrName == null )
+ {
+ return null;
+ }
+
+ Attribute attribute = attributes.get( attrName );
+ if ( attribute != null )
+ {
+ try
+ {
+ byte[] value = (byte[]) attribute.get();
+
+ return new String( value );
+ }
+ catch ( NamingException e )
+ {
+ throw new MappingException(
+ "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
+ }
+ }
+
+ return null;
+ }
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @version $Id$
+ */
+public class MappingException
+ extends Exception
+{
+
+ public MappingException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+ public MappingException( String message )
+ {
+ super( message );
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.users.User;
+
+import javax.naming.directory.Attributes;
+
+/**
+ * @version $Id$
+ */
+public interface UserMapper
+{
+ LdapUser getUser( Attributes attributes )
+ throws MappingException;
+
+ Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
+ throws MappingException;
+
+ UserUpdate getUpdate( LdapUser user )
+ throws MappingException;
+
+ String[] getUserAttributeNames();
+
+ String getEmailAddressAttribute();
+
+ String getUserFullNameAttribute();
+
+ String getPasswordAttribute();
+
+ String getUserIdAttribute();
+
+ String getEmailAttribute();
+
+ String getUserBaseDn();
+
+ String getUserObjectClass();
+
+ String getUserFilter();
+
+ LdapUser newUserInstance( String username, String fullName, String email );
+
+ LdapUser newTemplateUserInstance();
+
+ String[] getReturningAttributes();
+
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.naming.directory.Attributes;
+
+/**
+ * @version $Id$
+ */
+public class UserUpdate
+{
+
+ private final Attributes created;
+
+ private final Attributes modified;
+
+ private final Attributes removed;
+
+ public UserUpdate( Attributes created, Attributes modified, Attributes removed )
+ {
+ this.created = created;
+ this.modified = modified;
+ this.removed = removed;
+ }
+
+ public Attributes getAddedAttributes()
+ {
+ return created;
+ }
+
+ public Attributes getModifiedAttributes()
+ {
+ return modified;
+ }
+
+ public Attributes getRemovedAttributes()
+ {
+ return removed;
+ }
+
+ public boolean hasAdditions()
+ {
+ return ( created != null ) && ( created.size() > 0 );
+ }
+
+ public boolean hasModifications()
+ {
+ return ( modified != null ) && ( modified.size() > 0 );
+ }
+
+
+
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap.connection;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.redback.configuration.UserConfiguration;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.InvalidNameException;
+import javax.naming.ldap.LdapName;
+import javax.naming.ldap.Rdn;
+import javax.naming.spi.ObjectFactory;
+import javax.naming.spi.StateFactory;
+import java.util.Properties;
+
+/**
+ * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
+ * @version $Id$
+ */
+@Service( "ldapConnectionFactory#configurable" )
+public class ConfigurableLdapConnectionFactory
+ implements LdapConnectionFactory
+{
+ /**
+ *
+ */
+ private String hostname;
+
+ /**
+ *
+ */
+ private int port;
+
+ /**
+ *
+ */
+ private boolean ssl;
+
+ /**
+ *
+ */
+ private String baseDn;
+
+ /**
+ *
+ */
+ private String contextFactory;
+
+ /**
+ *
+ */
+ private String bindDn;
+
+ /**
+ *
+ */
+ private String password;
+
+ /**
+ *
+ */
+ private String authenticationMethod;
+
+ /**
+ *
+ */
+ private Properties extraProperties;
+
+ private LdapConnectionConfiguration configuration;
+
+
+ @Inject
+ @Named( value = "userConfiguration" )
+ private UserConfiguration userConf;
+
+ // ----------------------------------------------------------------------
+ // Component Lifecycle
+ // ----------------------------------------------------------------------
+ @PostConstruct
+ public void initialize()
+ {
+ try
+ {
+ configuration = new LdapConnectionConfiguration();
+ configuration.setHostname( userConf.getString( "ldap.config.hostname", hostname ) );
+ configuration.setPort( userConf.getInt( "ldap.config.port", port ) );
+ configuration.setSsl( userConf.getBoolean( "ldap.config.ssl", ssl ) );
+ configuration.setBaseDn( userConf.getConcatenatedList( "ldap.config.base.dn", baseDn ) );
+ configuration.setContextFactory( userConf.getString( "ldap.config.context.factory", contextFactory ) );
+ configuration.setBindDn( userConf.getConcatenatedList( "ldap.config.bind.dn", bindDn ) );
+ configuration.setPassword( userConf.getString( "ldap.config.password", password ) );
+ configuration.setAuthenticationMethod(
+ userConf.getString( "ldap.config.authentication.method", authenticationMethod ) );
+ configuration.setExtraProperties( extraProperties );
+ }
+ catch ( InvalidNameException e )
+ {
+ throw new RuntimeException( "Error while initializing connection factory.", e );
+ }
+ }
+
+ // ----------------------------------------------------------------------
+ // LdapConnectionFactory Implementation
+ // ----------------------------------------------------------------------
+
+ public LdapConnection getConnection()
+ throws LdapException
+ {
+ return new LdapConnection( configuration, null );
+ }
+
+ public LdapConnection getConnection( Rdn subRdn )
+ throws LdapException
+ {
+ return new LdapConnection( configuration, subRdn );
+ }
+
+ public LdapConnection getConnection( String bindDn, String password )
+ throws LdapException
+ {
+ return new LdapConnection( configuration, bindDn, password );
+ }
+
+ public LdapName getBaseDnLdapName()
+ throws LdapException
+ {
+ try
+ {
+ return new LdapName( baseDn );
+ }
+ catch ( InvalidNameException e )
+ {
+ throw new LdapException( "The base DN is not a valid name.", e );
+ }
+ }
+
+ public void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass )
+ {
+ configuration.getObjectFactories().add( objectFactoryClass );
+ }
+
+ public void addStateFactory( Class<? extends StateFactory> stateFactoryClass )
+ {
+ configuration.getStateFactories().add( stateFactoryClass );
+ }
+
+ // ----------------------------------------------------------------------
+ //
+ // ----------------------------------------------------------------------
+
+ public String toString()
+ {
+ return "{ConfigurableLdapConnectionFactory: configuration: " + configuration + "}";
+ }
+
+ public LdapConnectionConfiguration getConfiguration()
+ {
+ return configuration;
+ }
+
+ public String getHostname()
+ {
+ return hostname;
+ }
+
+ public void setHostname( String hostname )
+ {
+ this.hostname = hostname;
+ }
+
+ public int getPort()
+ {
+ return port;
+ }
+
+ public void setPort( int port )
+ {
+ this.port = port;
+ }
+
+ public boolean isSsl()
+ {
+ return ssl;
+ }
+
+ public void setSsl( boolean ssl )
+ {
+ this.ssl = ssl;
+ }
+
+ public String getBaseDn()
+ {
+ return baseDn;
+ }
+
+ public void setBaseDn( String baseDn )
+ {
+ this.baseDn = baseDn;
+ }
+
+ public String getContextFactory()
+ {
+ return contextFactory;
+ }
+
+ public void setContextFactory( String contextFactory )
+ {
+ this.contextFactory = contextFactory;
+ }
+
+ public String getBindDn()
+ {
+ return bindDn;
+ }
+
+ public void setBindDn( String bindDn )
+ {
+ this.bindDn = bindDn;
+ }
+
+ public String getPassword()
+ {
+ return password;
+ }
+
+ public void setPassword( String password )
+ {
+ this.password = password;
+ }
+
+ public String getAuthenticationMethod()
+ {
+ return authenticationMethod;
+ }
+
+ public void setAuthenticationMethod( String authenticationMethod )
+ {
+ this.authenticationMethod = authenticationMethod;
+ }
+
+ public Properties getExtraProperties()
+ {
+ return extraProperties;
+ }
+
+ public void setExtraProperties( Properties extraProperties )
+ {
+ this.extraProperties = extraProperties;
+ }
+
+ public UserConfiguration getUserConf()
+ {
+ return userConf;
+ }
+
+ public void setUserConf( UserConfiguration userConf )
+ {
+ this.userConf = userConf;
+ }
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap.connection;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import com.sun.jndi.ldap.LdapCtxFactory;
+import org.jvnet.animal_sniffer.IgnoreJRERequirement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
+import javax.naming.ldap.LdapName;
+import javax.naming.ldap.Rdn;
+import java.util.Collections;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * The configuration for a connection will not change.
+ *
+ * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
+ * @version $Id$
+ */
+public class LdapConnection
+{
+
+ private static LdapCtxFactory ctxFactory;// = new LdapCtxFactory();
+
+
+ static
+ {
+ initCtxFactory();
+ }
+
+
+ private Logger log = LoggerFactory.getLogger( getClass() );
+
+ private LdapConnectionConfiguration config;
+
+ private DirContext context;
+
+ private List<Rdn> baseDnRdns;
+
+ @IgnoreJRERequirement
+ private static void initCtxFactory()
+ {
+ ctxFactory = new LdapCtxFactory();
+ }
+
+ @IgnoreJRERequirement
+ public LdapConnection( LdapConnectionConfiguration config, Rdn subRdn )
+ throws LdapException
+ {
+ this.config = config;
+
+ LdapName baseDn = new LdapName( config.getBaseDn().getRdns() );
+
+ if ( subRdn != null )
+ {
+ baseDn.add( subRdn );
+ }
+
+ baseDnRdns = Collections.unmodifiableList( baseDn.getRdns() );
+
+ if ( context != null )
+ {
+ throw new LdapException( "Already connected." );
+ }
+
+ Hashtable<Object, Object> e = getEnvironment();
+
+ try
+ {
+ context = (DirContext) ctxFactory.getInitialContext( e );
+ }
+ catch ( NamingException ex )
+ {
+ throw new LdapException( "Could not connect to the server.", ex );
+ }
+ }
+
+ /**
+ * This ldap connection will attempt to establish a connection using the configuration,
+ * replacing the principal and the password
+ *
+ * @param config
+ * @param bindDn
+ * @param password
+ * @throws LdapException
+ */
+ @IgnoreJRERequirement
+ public LdapConnection( LdapConnectionConfiguration config, String bindDn, String password )
+ throws LdapException
+ {
+ this.config = config;
+
+ Hashtable<Object, Object> e = getEnvironment();
+
+ e.put( Context.SECURITY_PRINCIPAL, bindDn );
+ e.put( Context.SECURITY_CREDENTIALS, password );
+
+ try
+ {
+ context = (DirContext) ctxFactory.getInitialContext( e );
+ }
+ catch ( NamingException ex )
+ {
+ throw new LdapException( "Could not connect to the server.", ex );
+ }
+ }
+
+ // ----------------------------------------------------------------------
+ // Connection Managment
+ // ----------------------------------------------------------------------
+
+ public Hashtable<Object, Object> getEnvironment()
+ throws LdapException
+ {
+ Properties env = new Properties();
+
+ env.putAll( config.getExtraProperties() );
+
+ config.check();
+
+ env.put( Context.INITIAL_CONTEXT_FACTORY, config.getContextFactory() );
+
+ // REDBACK-289/MRM-1488
+ // enable connection pooling when using Sun's LDAP context factory
+ if( config.getContextFactory().equals( "com.sun.jndi.ldap.LdapCtxFactory" ) )
+ {
+ env.put( "com.sun.jndi.ldap.connect.pool", "true");
+
+ env.put( "com.sun.jndi.ldap.connect.pool.timeout", "3600" );
+ }
+
+ if ( config.getHostname() != null )
+ {
+ String protocol = config.isSsl() ? "ldaps" : "ldap";
+ if ( config.getPort() != 0 )
+ {
+ env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + ":" + config.getPort() + "/" );
+ }
+ else
+ {
+ env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + "/" );
+ }
+ }
+
+ if ( config.getAuthenticationMethod() != null )
+ {
+ env.put( Context.SECURITY_AUTHENTICATION, config.getAuthenticationMethod() );
+ }
+
+ if ( config.getBindDn() != null )
+ {
+ env.put( Context.SECURITY_PRINCIPAL, config.getBindDn().toString() );
+ }
+
+ if ( config.getPassword() != null )
+ {
+ env.put( Context.SECURITY_CREDENTIALS, config.getPassword() );
+ }
+
+ // ----------------------------------------------------------------------
+ // Object Factories
+ // ----------------------------------------------------------------------
+
+ String objectFactories = null;
+
+ for ( Class<?> objectFactoryClass : config.getObjectFactories() )
+ {
+ if ( objectFactories == null )
+ {
+ objectFactories = objectFactoryClass.getName();
+ }
+ else
+ {
+ objectFactories += ":" + objectFactoryClass.getName();
+ }
+ }
+
+ if ( objectFactories != null )
+ {
+ env.setProperty( Context.OBJECT_FACTORIES, objectFactories );
+ }
+
+ // ----------------------------------------------------------------------
+ // State Factories
+ // ----------------------------------------------------------------------
+
+ String stateFactories = null;
+
+ for ( Class<?> stateFactoryClass : config.getStateFactories() )
+ {
+ if ( stateFactories == null )
+ {
+ stateFactories = stateFactoryClass.getName();
+ }
+ else
+ {
+ stateFactories += ":" + stateFactoryClass.getName();
+ }
+ }
+
+ if ( stateFactories != null )
+ {
+ env.setProperty( Context.STATE_FACTORIES, stateFactories );
+ }
+
+ return env;
+ }
+
+ public void close()
+ {
+ try
+ {
+ if ( context != null )
+ {
+ context.close();
+ }
+ }
+ catch ( NamingException ex )
+ {
+ log.info( "skip error closing ldap connection {}", ex.getMessage() );
+ }
+ finally
+ {
+ context = null;
+ }
+ }
+
+ // ----------------------------------------------------------------------
+ // Utils
+ // ----------------------------------------------------------------------
+
+ public LdapConnectionConfiguration getConfiguration()
+ {
+ return config;
+ }
+
+ public List<Rdn> getBaseDnRdns()
+ {
+ return baseDnRdns;
+ }
+
+ public DirContext getDirContext()
+ {
+ return context;
+ }
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap.connection;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.lang.StringUtils;
+
+import javax.naming.InvalidNameException;
+import javax.naming.ldap.LdapName;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.TreeMap;
+
+/**
+ * This class contains the configuration for a ldap connection.
+ * <p/>
+ * Properties of a ldap connection:
+ * <ul>
+ * <li>Hostname - String, required.
+ * <li>Port - int, not required. If 0 then the default value is used by the ldap driver.
+ * <li>Ssl - boolean, not required. If true then the ldaps will be used.
+ * <li>Base DN - String, required.
+ * <li>Context factory - String, required.
+ * <li>Bind DN - String, not required.
+ * <li>Password - String, not required.
+ * </ul>
+ * Note that both the bind dn and password must be set if any are set.
+ *
+ * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
+ * @version $Id$
+ */
+public class LdapConnectionConfiguration
+{
+ private String hostname;
+
+ private int port;
+
+ private boolean ssl;
+
+ private LdapName baseDn;
+
+ private String contextFactory;
+
+ private LdapName bindDn;
+
+ private String password;
+
+ private String authenticationMethod;
+
+ private List<Class<?>> objectFactories;
+
+ private List<Class<?>> stateFactories;
+
+ private Properties extraProperties;
+
+ public LdapConnectionConfiguration()
+ {
+ }
+
+ public LdapConnectionConfiguration( String hostname, int port, LdapName baseDn, String contextFactory,
+ LdapName bindDn, String password, String authenticationMethod,
+ Properties extraProperties )
+ throws LdapException
+ {
+ this.hostname = hostname;
+
+ this.port = port;
+
+ if ( baseDn != null )
+ {
+ this.baseDn = new LdapName( baseDn.getRdns() );
+ }
+
+ this.contextFactory = contextFactory;
+
+ if ( bindDn != null )
+ {
+ this.bindDn = new LdapName( bindDn.getRdns() );
+ }
+
+ this.password = password;
+
+ this.authenticationMethod = authenticationMethod;
+
+ this.extraProperties = extraProperties;
+
+ check();
+ }
+
+ public LdapConnectionConfiguration( String hostname, int port, String baseDn, String contextFactory, String bindDn,
+ String password, String authenticationMethod, Properties extraProperties )
+ throws InvalidNameException, LdapException
+ {
+ this.hostname = hostname;
+ this.port = port;
+
+ if ( baseDn != null )
+ {
+ this.baseDn = new LdapName( baseDn );
+ }
+
+ if ( bindDn != null )
+ {
+ this.bindDn = new LdapName( bindDn );
+ }
+
+ this.contextFactory = contextFactory;
+
+ this.password = password;
+
+ this.authenticationMethod = authenticationMethod;
+
+ this.extraProperties = extraProperties;
+
+ check();
+ }
+
+ public LdapConnectionConfiguration( String hostname, int port, LdapName baseDn, String contextFactory )
+ throws LdapException
+ {
+ this.hostname = hostname;
+
+ this.port = port;
+
+ this.baseDn = baseDn;
+
+ this.contextFactory = contextFactory;
+
+ check();
+ }
+
+ // ----------------------------------------------------------------------
+ // Accessors
+ // ----------------------------------------------------------------------
+
+ public String getHostname()
+ {
+ return hostname;
+ }
+
+ public void setHostname( String hostname )
+ {
+ this.hostname = hostname;
+ }
+
+ public int getPort()
+ {
+ return port;
+ }
+
+ public void setPort( int port )
+ {
+ this.port = port;
+ }
+
+ public boolean isSsl()
+ {
+ return ssl;
+ }
+
+ public void setSsl( boolean ssl )
+ {
+ this.ssl = ssl;
+ }
+
+ public LdapName getBaseDn()
+ {
+ return baseDn;
+ }
+
+ public void setBaseDn( LdapName baseDn )
+ {
+ this.baseDn = baseDn;
+ }
+
+ public void setBaseDn( String baseDn )
+ throws InvalidNameException
+ {
+ if ( baseDn != null )
+ {
+ this.baseDn = new LdapName( baseDn );
+ }
+ }
+
+ public String getContextFactory()
+ {
+ return contextFactory;
+ }
+
+ public void setContextFactory( String contextFactory )
+ {
+ this.contextFactory = contextFactory;
+ }
+
+ public LdapName getBindDn()
+ {
+ return bindDn;
+ }
+
+ public void setBindDn( LdapName bindDn )
+ {
+ this.bindDn = bindDn;
+ }
+
+ public void setBindDn( String bindDn )
+ throws InvalidNameException
+ {
+ if ( bindDn != null )
+ {
+ this.bindDn = new LdapName( bindDn );
+ }
+ }
+
+ public String getPassword()
+ {
+ return password;
+ }
+
+ public void setPassword( String password )
+ {
+ this.password = password;
+ }
+
+ public String getAuthenticationMethod()
+ {
+ return authenticationMethod;
+ }
+
+ public void setAuthenticationMethod( String authenticationMethod )
+ {
+ this.authenticationMethod = authenticationMethod;
+ }
+
+ public List<Class<?>> getObjectFactories()
+ {
+ if ( objectFactories == null )
+ {
+ objectFactories = new ArrayList<Class<?>>( 0 );
+ }
+
+ return objectFactories;
+ }
+
+ public void setObjectFactories( List<Class<?>> objectFactories )
+ {
+ this.objectFactories = objectFactories;
+ }
+
+ public List<Class<?>> getStateFactories()
+ {
+ if ( stateFactories == null )
+ {
+ stateFactories = new ArrayList<Class<?>>( 0 );
+ }
+
+ return stateFactories;
+ }
+
+ public void setStateFactories( List<Class<?>> stateFactories )
+ {
+ this.stateFactories = stateFactories;
+ }
+
+ public Properties getExtraProperties()
+ {
+ if ( extraProperties == null )
+ {
+ extraProperties = new Properties();
+ }
+
+ return extraProperties;
+ }
+
+ public void setExtraProperties( Properties extraProperties )
+ {
+ this.extraProperties = extraProperties;
+ }
+
+ // ----------------------------------------------------------------------
+ //
+ // ----------------------------------------------------------------------
+
+ public void check()
+ throws LdapException
+ {
+ if ( port < 0 || port > 65535 )
+ {
+ throw new LdapException( "The port must be between 1 and 65535." );
+ }
+ if ( baseDn == null )
+ {
+ throw new LdapException( "The base DN must be set." );
+ }
+ if ( StringUtils.isEmpty( contextFactory ) )
+ {
+ throw new LdapException( "The context factory must be set." );
+ }
+ if ( password != null && bindDn == null )
+ {
+ throw new LdapException( "The password cant be set unless the bind dn is." );
+ }
+
+ if ( extraProperties == null )
+ {
+ extraProperties = new Properties();
+ }
+ }
+
+ // ----------------------------------------------------------------------
+ //
+ // ----------------------------------------------------------------------
+
+ public String toString()
+ {
+ return "{LdapConnectionConfiguration: " +
+ "hostname: " + getHostname() + ", " +
+ "port: " + getPort() + ", " +
+ "ssl: " + isSsl() + ", " +
+ "baseDn: " + getBaseDn() + ", " +
+ "contextFactory: " + getContextFactory() + ", " +
+ "bindDn: " + getBindDn() + ", " +
+ "password: " + getPassword() + ", " +
+ "authenticationMethod: " + getAuthenticationMethod() + ", " +
+ "objectFactories: " + getObjectFactories() + ", " +
+ "stateFactories: " + getStateFactories() + ", " +
+ "extraProperties: " + new TreeMap<Object, Object>( extraProperties ).toString() + "}";
+ }
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap.connection;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.naming.ldap.LdapName;
+import javax.naming.ldap.Rdn;
+import javax.naming.spi.ObjectFactory;
+import javax.naming.spi.StateFactory;
+
+
+/**
+ * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
+ * @version $Id$
+ */
+public interface LdapConnectionFactory
+{
+ String ROLE = LdapConnectionFactory.class.getName();
+
+ LdapConnection getConnection()
+ throws LdapException;
+
+ LdapConnection getConnection( Rdn subRdn )
+ throws LdapException;
+
+ LdapConnection getConnection( String bindDn, String password )
+ throws LdapException;
+
+ LdapName getBaseDnLdapName()
+ throws LdapException;
+
+ void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass );
+
+ void addStateFactory( Class<? extends StateFactory> objectFactoryClass );
+
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap.connection;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.naming.NamingException;
+
+/**
+ * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
+ * @version $Id$
+ */
+public class LdapException
+ extends NamingException
+{
+ public LdapException( String message )
+ {
+ super( message );
+ }
+
+ public LdapException( String message, Throwable t )
+ {
+ super( message );
+ setRootCause( t );
+ }
+}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.redback.users.User;
-
-import javax.naming.directory.Attributes;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
-// TODO this class should be able to be replaced with a model
-public class LdapUser
- implements User, Serializable
-{
-
- private String key;
-
- private String username;
-
- private String fullName;
-
- private String email;
-
- private String encodedPassword;
-
- private List<String> previousEncodedPasswords;
-
- private boolean locked = false;
-
- private boolean requiresPasswordChange = false;
-
- private boolean permanent = true;
-
- private boolean valid = true;
-
- private Date creationDate = null;
-
- private int failedLoginAttempts;
-
- private Date lastLoginDate = null;
-
- private Date lastPasswordChange = null;
-
- // DO NOT STORE AS SUCH!!!
- private String newPassword;
-
- private Attributes originalAttributes;
-
- public LdapUser( String username )
- {
- key = username;
- this.username = username;
- previousEncodedPasswords = new ArrayList<String>( 0 );
- failedLoginAttempts = 0;
- }
-
- public LdapUser( String username, String fullName, String email )
- {
- this( username );
- this.fullName = fullName;
- this.email = email;
- }
-
- public LdapUser()
- {
- previousEncodedPasswords = new ArrayList<String>( 0 );
- failedLoginAttempts = Integer.MIN_VALUE;
- }
-
- public void addPreviousEncodedPassword( String encodedPassword )
- {
- previousEncodedPasswords.add( encodedPassword );
- }
-
- public Date getAccountCreationDate()
- {
- return creationDate;
- }
-
- public int getCountFailedLoginAttempts()
- {
- return failedLoginAttempts;
- }
-
- public String getEmail()
- {
- return email;
- }
-
- public String getEncodedPassword()
- {
- return encodedPassword;
- }
-
- public String getFullName()
- {
- return fullName;
- }
-
- public Date getLastLoginDate()
- {
- return lastLoginDate;
- }
-
- public Date getLastPasswordChange()
- {
- return lastPasswordChange;
- }
-
- public String getPassword()
- {
- return newPassword;
- }
-
- public List<String> getPreviousEncodedPasswords()
- {
- return previousEncodedPasswords;
- }
-
- public Object getPrincipal()
- {
- return key;
- }
-
- public String getUsername()
- {
- return username;
- }
-
- public boolean isLocked()
- {
- return locked;
- }
-
- public boolean isPasswordChangeRequired()
- {
- return requiresPasswordChange;
- }
-
- public boolean isPermanent()
- {
- return permanent;
- }
-
- public boolean isValidated()
- {
- return valid;
- }
-
- public void setCountFailedLoginAttempts( int count )
- {
- failedLoginAttempts = count;
- }
-
- public void setEmail( String address )
- {
- email = address;
- }
-
- public void setEncodedPassword( String encodedPassword )
- {
- this.encodedPassword = encodedPassword;
- }
-
- public void setFullName( String name )
- {
- fullName = name;
- }
-
- public void setAccountCreationDate( Date date )
- {
- creationDate = date;
- }
-
- public void setLastLoginDate( Date date )
- {
- lastLoginDate = date;
- }
-
- public void setLastPasswordChange( Date passwordChangeDate )
- {
- lastPasswordChange = passwordChangeDate;
- }
-
- public void setLocked( boolean locked )
- {
- this.locked = locked;
- }
-
- public void setPassword( String rawPassword )
- {
- newPassword = rawPassword;
- }
-
- public void setPasswordChangeRequired( boolean changeRequired )
- {
- requiresPasswordChange = changeRequired;
- }
-
- public void setPermanent( boolean permanent )
- {
- this.permanent = permanent;
- }
-
- public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
- {
- previousEncodedPasswords = new ArrayList<String>( encodedPasswordList );
- }
-
- public void setUsername( String name )
- {
- username = name;
- }
-
- public void setValidated( boolean valid )
- {
- this.valid = valid;
- }
-
- public Attributes getOriginalAttributes()
- {
- return originalAttributes;
- }
-
- public void setOriginalAttributes( Attributes originalAttributes )
- {
- this.originalAttributes = originalAttributes;
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.redback.users.User;
-import org.codehaus.plexus.redback.configuration.UserConfiguration;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.stereotype.Service;
-
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.naming.directory.Attributes;
-import javax.naming.directory.BasicAttributes;
-import java.util.Date;
-
-/**
- * @author <a href="jesse@codehaus.org"> jesse
- * @version $Id$
- */
-@Service( "userMapper#ldap" )
-public class LdapUserMapper
- implements UserMapper
-{
- /**
- *
- */
- String emailAttribute = "mail";
-
- /**
- *
- */
- String fullNameAttribute = "givenName";
-
- /**
- *
- */
- String passwordAttribute = "userPassword";
-
- /**
- *
- */
- String userIdAttribute = "cn";
-
- /**
- *
- */
- String userBaseDn;
-
- /**
- *
- */
- String userObjectClass = "inetOrgPerson";
-
- /**
- *
- */
- String userFilter;
-
- /**
- *
- */
- int maxResultCount = 0;
-
- @Inject
- @Named( value = "userConfiguration" )
- private UserConfiguration userConf;
-
- @PostConstruct
- public void initialize()
- {
- emailAttribute = userConf.getString( "ldap.config.mapper.attribute.email", emailAttribute );
- fullNameAttribute = userConf.getString( "ldap.config.mapper.attribute.fullname", fullNameAttribute );
- passwordAttribute = userConf.getString( "ldap.config.mapper.attribute.password", passwordAttribute );
- userIdAttribute = userConf.getString( "ldap.config.mapper.attribute.user.id", userIdAttribute );
- userBaseDn = userConf.getConcatenatedList( "ldap.config.mapper.attribute.user.base.dn",
- userConf.getConcatenatedList( "ldap.config.base.dn", userBaseDn ) );
- userObjectClass = userConf.getString( "ldap.config.mapper.attribute.user.object.class", userObjectClass );
- userFilter = userConf.getString( "ldap.config.mapper.attribute.user.filter", userFilter );
- maxResultCount = userConf.getInt( "ldap.config.max.result.count", maxResultCount );
- }
-
- public Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
- throws MappingException
- {
- Attributes userAttrs = new BasicAttributes();
-
- boolean passwordSet = false;
-
- if ( !passwordSet && ( user.getEncodedPassword() != null ) )
- {
- userAttrs.put( getPasswordAttribute(), user.getEncodedPassword() );
- }
-
- if ( !StringUtils.isEmpty( user.getFullName() ) )
- {
- userAttrs.put( getUserFullNameAttribute(), user.getFullName() );
- }
-
- if ( !StringUtils.isEmpty( user.getEmail() ) )
- {
- userAttrs.put( getEmailAddressAttribute(), user.getEmail() );
- }
-
- return userAttrs;
- }
-
- public String getEmailAddressAttribute()
- {
- return emailAttribute;
- }
-
- public String getUserFullNameAttribute()
- {
- return fullNameAttribute;
- }
-
- public String getPasswordAttribute()
- {
- return passwordAttribute;
- }
-
- public String[] getUserAttributeNames()
- {
- return new String[]{ emailAttribute, fullNameAttribute, passwordAttribute, userIdAttribute };
- }
-
- public int getMaxResultCount()
- {
- return maxResultCount;
- }
-
- public UserUpdate getUpdate( LdapUser user )
- throws MappingException
- {
-
- Attributes addAttrs = new BasicAttributes();
-
- Attributes modAttrs = new BasicAttributes();
-
- if ( !StringUtils.isEmpty( user.getFullName() ) )
- {
- if ( user.getFullName() == null )
- {
- addAttrs.put( getUserFullNameAttribute(), user.getFullName() );
- }
- else if ( !user.getFullName().equals( user.getFullName() ) )
- {
- modAttrs.put( getUserFullNameAttribute(), user.getFullName() );
- }
- }
-
- if ( !StringUtils.isEmpty( user.getEmail() ) )
- {
- if ( user.getEmail() == null )
- {
- addAttrs.put( getEmailAddressAttribute(), user.getEmail() );
- }
- else if ( !user.getEmail().equals( user.getEmail() ) )
- {
- modAttrs.put( getEmailAddressAttribute(), user.getEmail() );
- }
- }
-
- return null;
- }
-
- public LdapUser getUser( Attributes attributes )
- throws MappingException
- {
- String userIdAttribute = getUserIdAttribute();
- String emailAddressAttribute = getEmailAddressAttribute();
- String nameAttribute = getUserFullNameAttribute();
- String passwordAttribute = getPasswordAttribute();
-
- String userId = ( LdapUtils.getAttributeValue( attributes, userIdAttribute, "username" ) );
-
- LdapUser user = new LdapUser( userId );
- user.setOriginalAttributes( attributes );
-
- user.setEmail( LdapUtils.getAttributeValue( attributes, emailAddressAttribute, "email address" ) );
- user.setFullName( LdapUtils.getAttributeValue( attributes, nameAttribute, "name" ) );
-
- String encodedPassword = LdapUtils.getAttributeValueFromByteArray( attributes, passwordAttribute, "password" );
-
- // it seems to be a common convention for the password to come back prepended with the encoding type..
- // however we deal with that via configuration right now so just smoke it.
- if ( encodedPassword != null && encodedPassword.startsWith( "{" ) )
- {
- encodedPassword = encodedPassword.substring( encodedPassword.indexOf( "}" ) + 1 );
- }
-
- user.setEncodedPassword( encodedPassword );
-
- // REDBACK-215: skip NPE
- user.setLastPasswordChange( new Date() );
-
- return user;
- }
-
- public String getUserIdAttribute()
- {
- return userIdAttribute;
- }
-
- public String getEmailAttribute()
- {
- return emailAttribute;
- }
-
- public void setEmailAttribute( String emailAttribute )
- {
- this.emailAttribute = emailAttribute;
- }
-
- public String getFullNameAttribute()
- {
- return fullNameAttribute;
- }
-
- public void setFullNameAttribute( String fullNameAttribute )
- {
- this.fullNameAttribute = fullNameAttribute;
- }
-
- public void setMaxResultCount( int maxResultCount )
- {
- this.maxResultCount = maxResultCount;
- }
-
- public String getUserBaseDn()
- {
- return userBaseDn;
- }
-
- public void setUserBaseDn( String userBaseDn )
- {
- this.userBaseDn = userBaseDn;
- }
-
- public String getUserObjectClass()
- {
- return userObjectClass;
- }
-
- public String getUserFilter()
- {
- return userFilter;
- }
-
- public void setUserFilter( String userFilter )
- {
- this.userFilter = userFilter;
- }
-
- public void setUserObjectClass( String userObjectClass )
- {
- this.userObjectClass = userObjectClass;
- }
-
- public void setPasswordAttribute( String passwordAttribute )
- {
- this.passwordAttribute = passwordAttribute;
- }
-
- public void setUserIdAttribute( String userIdAttribute )
- {
- this.userIdAttribute = userIdAttribute;
- }
-
- public LdapUser newUserInstance( String username, String fullName, String email )
- {
- return new LdapUser( username, fullName, email );
- }
-
- public LdapUser newTemplateUserInstance()
- {
- return new LdapUser();
- }
-
- public String[] getReturningAttributes()
- {
- return new String[]{ getUserIdAttribute(), getEmailAttribute(), getFullNameAttribute(),
- getPasswordAttribute() };
- }
-
- public UserConfiguration getUserConf()
- {
- return userConf;
- }
-
- public void setUserConf( UserConfiguration userConf )
- {
- this.userConf = userConf;
- }
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-import javax.naming.directory.Attribute;
-import javax.naming.directory.Attributes;
-
-/**
- *
- * @version $Id$
- */
-public final class LdapUtils
-{
- private LdapUtils()
- {
- // no op
- }
-
- @SuppressWarnings("unchecked")
- public static String getLabeledUriValue( Attributes attributes, String attrName, String label,
- String attributeDescription )
- throws MappingException
- {
- if ( attrName == null )
- {
- return null;
- }
-
- Attribute attribute = attributes.get( attrName );
- if ( attribute != null )
- {
- NamingEnumeration attrs;
- try
- {
- attrs = attribute.getAll();
- }
- catch ( NamingException e )
- {
- throw new MappingException(
- "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
- }
-
- while ( attrs.hasMoreElements() )
- {
- Object value = attrs.nextElement();
-
- String val = String.valueOf( value );
-
- if ( val.endsWith( " " + label ) )
- {
- return val.substring( 0, val.length() - ( label.length() + 1 ) );
- }
- }
- }
-
- return null;
- }
-
- public static String getAttributeValue( Attributes attributes, String attrName, String attributeDescription )
- throws MappingException
- {
- if ( attrName == null )
- {
- return null;
- }
-
- Attribute attribute = attributes.get( attrName );
- if ( attribute != null )
- {
- try
- {
- Object value = attribute.get();
-
- return String.valueOf( value );
- }
- catch ( NamingException e )
- {
- throw new MappingException(
- "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
- }
- }
-
- return null;
- }
-
- public static String getAttributeValueFromByteArray( Attributes attributes, String attrName,
- String attributeDescription )
- throws MappingException
- {
- if ( attrName == null )
- {
- return null;
- }
-
- Attribute attribute = attributes.get( attrName );
- if ( attribute != null )
- {
- try
- {
- byte[] value = (byte[]) attribute.get();
-
- return new String( value );
- }
- catch ( NamingException e )
- {
- throw new MappingException(
- "Failed to retrieve " + attributeDescription + " (attribute: \'" + attrName + "\').", e );
- }
- }
-
- return null;
- }
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-/**
- * @version $Id$
- */
-public class MappingException
- extends Exception
-{
-
- public MappingException( String message, Throwable cause )
- {
- super( message, cause );
- }
-
- public MappingException( String message )
- {
- super( message );
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.redback.users.User;
-
-import javax.naming.directory.Attributes;
-
-/**
- * @version $Id$
- */
-public interface UserMapper
-{
- LdapUser getUser( Attributes attributes )
- throws MappingException;
-
- Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
- throws MappingException;
-
- UserUpdate getUpdate( LdapUser user )
- throws MappingException;
-
- String[] getUserAttributeNames();
-
- String getEmailAddressAttribute();
-
- String getUserFullNameAttribute();
-
- String getPasswordAttribute();
-
- String getUserIdAttribute();
-
- String getEmailAttribute();
-
- String getUserBaseDn();
-
- String getUserObjectClass();
-
- String getUserFilter();
-
- LdapUser newUserInstance( String username, String fullName, String email );
-
- LdapUser newTemplateUserInstance();
-
- String[] getReturningAttributes();
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import javax.naming.directory.Attributes;
-
-/**
- * @version $Id$
- */
-public class UserUpdate
-{
-
- private final Attributes created;
-
- private final Attributes modified;
-
- private final Attributes removed;
-
- public UserUpdate( Attributes created, Attributes modified, Attributes removed )
- {
- this.created = created;
- this.modified = modified;
- this.removed = removed;
- }
-
- public Attributes getAddedAttributes()
- {
- return created;
- }
-
- public Attributes getModifiedAttributes()
- {
- return modified;
- }
-
- public Attributes getRemovedAttributes()
- {
- return removed;
- }
-
- public boolean hasAdditions()
- {
- return ( created != null ) && ( created.size() > 0 );
- }
-
- public boolean hasModifications()
- {
- return ( modified != null ) && ( modified.size() > 0 );
- }
-
-
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap.connection;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.codehaus.plexus.redback.configuration.UserConfiguration;
-import org.springframework.stereotype.Service;
-
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.naming.InvalidNameException;
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
-import javax.naming.spi.ObjectFactory;
-import javax.naming.spi.StateFactory;
-import java.util.Properties;
-
-/**
- * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
- * @version $Id$
- */
-@Service( "ldapConnectionFactory#configurable" )
-public class ConfigurableLdapConnectionFactory
- implements LdapConnectionFactory
-{
- /**
- *
- */
- private String hostname;
-
- /**
- *
- */
- private int port;
-
- /**
- *
- */
- private boolean ssl;
-
- /**
- *
- */
- private String baseDn;
-
- /**
- *
- */
- private String contextFactory;
-
- /**
- *
- */
- private String bindDn;
-
- /**
- *
- */
- private String password;
-
- /**
- *
- */
- private String authenticationMethod;
-
- /**
- *
- */
- private Properties extraProperties;
-
- private LdapConnectionConfiguration configuration;
-
-
- @Inject
- @Named( value = "userConfiguration" )
- private UserConfiguration userConf;
-
- // ----------------------------------------------------------------------
- // Component Lifecycle
- // ----------------------------------------------------------------------
- @PostConstruct
- public void initialize()
- {
- try
- {
- configuration = new LdapConnectionConfiguration();
- configuration.setHostname( userConf.getString( "ldap.config.hostname", hostname ) );
- configuration.setPort( userConf.getInt( "ldap.config.port", port ) );
- configuration.setSsl( userConf.getBoolean( "ldap.config.ssl", ssl ) );
- configuration.setBaseDn( userConf.getConcatenatedList( "ldap.config.base.dn", baseDn ) );
- configuration.setContextFactory( userConf.getString( "ldap.config.context.factory", contextFactory ) );
- configuration.setBindDn( userConf.getConcatenatedList( "ldap.config.bind.dn", bindDn ) );
- configuration.setPassword( userConf.getString( "ldap.config.password", password ) );
- configuration.setAuthenticationMethod(
- userConf.getString( "ldap.config.authentication.method", authenticationMethod ) );
- configuration.setExtraProperties( extraProperties );
- }
- catch ( InvalidNameException e )
- {
- throw new RuntimeException( "Error while initializing connection factory.", e );
- }
- }
-
- // ----------------------------------------------------------------------
- // LdapConnectionFactory Implementation
- // ----------------------------------------------------------------------
-
- public LdapConnection getConnection()
- throws LdapException
- {
- return new LdapConnection( configuration, null );
- }
-
- public LdapConnection getConnection( Rdn subRdn )
- throws LdapException
- {
- return new LdapConnection( configuration, subRdn );
- }
-
- public LdapConnection getConnection( String bindDn, String password )
- throws LdapException
- {
- return new LdapConnection( configuration, bindDn, password );
- }
-
- public LdapName getBaseDnLdapName()
- throws LdapException
- {
- try
- {
- return new LdapName( baseDn );
- }
- catch ( InvalidNameException e )
- {
- throw new LdapException( "The base DN is not a valid name.", e );
- }
- }
-
- public void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass )
- {
- configuration.getObjectFactories().add( objectFactoryClass );
- }
-
- public void addStateFactory( Class<? extends StateFactory> stateFactoryClass )
- {
- configuration.getStateFactories().add( stateFactoryClass );
- }
-
- // ----------------------------------------------------------------------
- //
- // ----------------------------------------------------------------------
-
- public String toString()
- {
- return "{ConfigurableLdapConnectionFactory: configuration: " + configuration + "}";
- }
-
- public LdapConnectionConfiguration getConfiguration()
- {
- return configuration;
- }
-
- public String getHostname()
- {
- return hostname;
- }
-
- public void setHostname( String hostname )
- {
- this.hostname = hostname;
- }
-
- public int getPort()
- {
- return port;
- }
-
- public void setPort( int port )
- {
- this.port = port;
- }
-
- public boolean isSsl()
- {
- return ssl;
- }
-
- public void setSsl( boolean ssl )
- {
- this.ssl = ssl;
- }
-
- public String getBaseDn()
- {
- return baseDn;
- }
-
- public void setBaseDn( String baseDn )
- {
- this.baseDn = baseDn;
- }
-
- public String getContextFactory()
- {
- return contextFactory;
- }
-
- public void setContextFactory( String contextFactory )
- {
- this.contextFactory = contextFactory;
- }
-
- public String getBindDn()
- {
- return bindDn;
- }
-
- public void setBindDn( String bindDn )
- {
- this.bindDn = bindDn;
- }
-
- public String getPassword()
- {
- return password;
- }
-
- public void setPassword( String password )
- {
- this.password = password;
- }
-
- public String getAuthenticationMethod()
- {
- return authenticationMethod;
- }
-
- public void setAuthenticationMethod( String authenticationMethod )
- {
- this.authenticationMethod = authenticationMethod;
- }
-
- public Properties getExtraProperties()
- {
- return extraProperties;
- }
-
- public void setExtraProperties( Properties extraProperties )
- {
- this.extraProperties = extraProperties;
- }
-
- public UserConfiguration getUserConf()
- {
- return userConf;
- }
-
- public void setUserConf( UserConfiguration userConf )
- {
- this.userConf = userConf;
- }
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap.connection;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import com.sun.jndi.ldap.LdapCtxFactory;
-import org.jvnet.animal_sniffer.IgnoreJRERequirement;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.naming.Context;
-import javax.naming.NamingException;
-import javax.naming.directory.DirContext;
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
-import java.util.Collections;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.Properties;
-
-/**
- * The configuration for a connection will not change.
- *
- * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
- * @version $Id$
- */
-public class LdapConnection
-{
-
- private static LdapCtxFactory ctxFactory;// = new LdapCtxFactory();
-
-
- static
- {
- initCtxFactory();
- }
-
-
- private Logger log = LoggerFactory.getLogger( getClass() );
-
- private LdapConnectionConfiguration config;
-
- private DirContext context;
-
- private List<Rdn> baseDnRdns;
-
- @IgnoreJRERequirement
- private static void initCtxFactory()
- {
- ctxFactory = new LdapCtxFactory();
- }
-
- @IgnoreJRERequirement
- public LdapConnection( LdapConnectionConfiguration config, Rdn subRdn )
- throws LdapException
- {
- this.config = config;
-
- LdapName baseDn = new LdapName( config.getBaseDn().getRdns() );
-
- if ( subRdn != null )
- {
- baseDn.add( subRdn );
- }
-
- baseDnRdns = Collections.unmodifiableList( baseDn.getRdns() );
-
- if ( context != null )
- {
- throw new LdapException( "Already connected." );
- }
-
- Hashtable<Object, Object> e = getEnvironment();
-
- try
- {
- context = (DirContext) ctxFactory.getInitialContext( e );
- }
- catch ( NamingException ex )
- {
- throw new LdapException( "Could not connect to the server.", ex );
- }
- }
-
- /**
- * This ldap connection will attempt to establish a connection using the configuration,
- * replacing the principal and the password
- *
- * @param config
- * @param bindDn
- * @param password
- * @throws LdapException
- */
- @IgnoreJRERequirement
- public LdapConnection( LdapConnectionConfiguration config, String bindDn, String password )
- throws LdapException
- {
- this.config = config;
-
- Hashtable<Object, Object> e = getEnvironment();
-
- e.put( Context.SECURITY_PRINCIPAL, bindDn );
- e.put( Context.SECURITY_CREDENTIALS, password );
-
- try
- {
- context = (DirContext) ctxFactory.getInitialContext( e );
- }
- catch ( NamingException ex )
- {
- throw new LdapException( "Could not connect to the server.", ex );
- }
- }
-
- // ----------------------------------------------------------------------
- // Connection Managment
- // ----------------------------------------------------------------------
-
- public Hashtable<Object, Object> getEnvironment()
- throws LdapException
- {
- Properties env = new Properties();
-
- env.putAll( config.getExtraProperties() );
-
- config.check();
-
- env.put( Context.INITIAL_CONTEXT_FACTORY, config.getContextFactory() );
-
- // REDBACK-289/MRM-1488
- // enable connection pooling when using Sun's LDAP context factory
- if( config.getContextFactory().equals( "com.sun.jndi.ldap.LdapCtxFactory" ) )
- {
- env.put( "com.sun.jndi.ldap.connect.pool", "true");
-
- env.put( "com.sun.jndi.ldap.connect.pool.timeout", "3600" );
- }
-
- if ( config.getHostname() != null )
- {
- String protocol = config.isSsl() ? "ldaps" : "ldap";
- if ( config.getPort() != 0 )
- {
- env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + ":" + config.getPort() + "/" );
- }
- else
- {
- env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + "/" );
- }
- }
-
- if ( config.getAuthenticationMethod() != null )
- {
- env.put( Context.SECURITY_AUTHENTICATION, config.getAuthenticationMethod() );
- }
-
- if ( config.getBindDn() != null )
- {
- env.put( Context.SECURITY_PRINCIPAL, config.getBindDn().toString() );
- }
-
- if ( config.getPassword() != null )
- {
- env.put( Context.SECURITY_CREDENTIALS, config.getPassword() );
- }
-
- // ----------------------------------------------------------------------
- // Object Factories
- // ----------------------------------------------------------------------
-
- String objectFactories = null;
-
- for ( Class<?> objectFactoryClass : config.getObjectFactories() )
- {
- if ( objectFactories == null )
- {
- objectFactories = objectFactoryClass.getName();
- }
- else
- {
- objectFactories += ":" + objectFactoryClass.getName();
- }
- }
-
- if ( objectFactories != null )
- {
- env.setProperty( Context.OBJECT_FACTORIES, objectFactories );
- }
-
- // ----------------------------------------------------------------------
- // State Factories
- // ----------------------------------------------------------------------
-
- String stateFactories = null;
-
- for ( Class<?> stateFactoryClass : config.getStateFactories() )
- {
- if ( stateFactories == null )
- {
- stateFactories = stateFactoryClass.getName();
- }
- else
- {
- stateFactories += ":" + stateFactoryClass.getName();
- }
- }
-
- if ( stateFactories != null )
- {
- env.setProperty( Context.STATE_FACTORIES, stateFactories );
- }
-
- return env;
- }
-
- public void close()
- {
- try
- {
- if ( context != null )
- {
- context.close();
- }
- }
- catch ( NamingException ex )
- {
- log.info( "skip error closing ldap connection {}", ex.getMessage() );
- }
- finally
- {
- context = null;
- }
- }
-
- // ----------------------------------------------------------------------
- // Utils
- // ----------------------------------------------------------------------
-
- public LdapConnectionConfiguration getConfiguration()
- {
- return config;
- }
-
- public List<Rdn> getBaseDnRdns()
- {
- return baseDnRdns;
- }
-
- public DirContext getDirContext()
- {
- return context;
- }
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap.connection;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.commons.lang.StringUtils;
-
-import javax.naming.InvalidNameException;
-import javax.naming.ldap.LdapName;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Properties;
-import java.util.TreeMap;
-
-/**
- * This class contains the configuration for a ldap connection.
- * <p/>
- * Properties of a ldap connection:
- * <ul>
- * <li>Hostname - String, required.
- * <li>Port - int, not required. If 0 then the default value is used by the ldap driver.
- * <li>Ssl - boolean, not required. If true then the ldaps will be used.
- * <li>Base DN - String, required.
- * <li>Context factory - String, required.
- * <li>Bind DN - String, not required.
- * <li>Password - String, not required.
- * </ul>
- * Note that both the bind dn and password must be set if any are set.
- *
- * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
- * @version $Id$
- */
-public class LdapConnectionConfiguration
-{
- private String hostname;
-
- private int port;
-
- private boolean ssl;
-
- private LdapName baseDn;
-
- private String contextFactory;
-
- private LdapName bindDn;
-
- private String password;
-
- private String authenticationMethod;
-
- private List<Class<?>> objectFactories;
-
- private List<Class<?>> stateFactories;
-
- private Properties extraProperties;
-
- public LdapConnectionConfiguration()
- {
- }
-
- public LdapConnectionConfiguration( String hostname, int port, LdapName baseDn, String contextFactory,
- LdapName bindDn, String password, String authenticationMethod,
- Properties extraProperties )
- throws LdapException
- {
- this.hostname = hostname;
-
- this.port = port;
-
- if ( baseDn != null )
- {
- this.baseDn = new LdapName( baseDn.getRdns() );
- }
-
- this.contextFactory = contextFactory;
-
- if ( bindDn != null )
- {
- this.bindDn = new LdapName( bindDn.getRdns() );
- }
-
- this.password = password;
-
- this.authenticationMethod = authenticationMethod;
-
- this.extraProperties = extraProperties;
-
- check();
- }
-
- public LdapConnectionConfiguration( String hostname, int port, String baseDn, String contextFactory, String bindDn,
- String password, String authenticationMethod, Properties extraProperties )
- throws InvalidNameException, LdapException
- {
- this.hostname = hostname;
- this.port = port;
-
- if ( baseDn != null )
- {
- this.baseDn = new LdapName( baseDn );
- }
-
- if ( bindDn != null )
- {
- this.bindDn = new LdapName( bindDn );
- }
-
- this.contextFactory = contextFactory;
-
- this.password = password;
-
- this.authenticationMethod = authenticationMethod;
-
- this.extraProperties = extraProperties;
-
- check();
- }
-
- public LdapConnectionConfiguration( String hostname, int port, LdapName baseDn, String contextFactory )
- throws LdapException
- {
- this.hostname = hostname;
-
- this.port = port;
-
- this.baseDn = baseDn;
-
- this.contextFactory = contextFactory;
-
- check();
- }
-
- // ----------------------------------------------------------------------
- // Accessors
- // ----------------------------------------------------------------------
-
- public String getHostname()
- {
- return hostname;
- }
-
- public void setHostname( String hostname )
- {
- this.hostname = hostname;
- }
-
- public int getPort()
- {
- return port;
- }
-
- public void setPort( int port )
- {
- this.port = port;
- }
-
- public boolean isSsl()
- {
- return ssl;
- }
-
- public void setSsl( boolean ssl )
- {
- this.ssl = ssl;
- }
-
- public LdapName getBaseDn()
- {
- return baseDn;
- }
-
- public void setBaseDn( LdapName baseDn )
- {
- this.baseDn = baseDn;
- }
-
- public void setBaseDn( String baseDn )
- throws InvalidNameException
- {
- if ( baseDn != null )
- {
- this.baseDn = new LdapName( baseDn );
- }
- }
-
- public String getContextFactory()
- {
- return contextFactory;
- }
-
- public void setContextFactory( String contextFactory )
- {
- this.contextFactory = contextFactory;
- }
-
- public LdapName getBindDn()
- {
- return bindDn;
- }
-
- public void setBindDn( LdapName bindDn )
- {
- this.bindDn = bindDn;
- }
-
- public void setBindDn( String bindDn )
- throws InvalidNameException
- {
- if ( bindDn != null )
- {
- this.bindDn = new LdapName( bindDn );
- }
- }
-
- public String getPassword()
- {
- return password;
- }
-
- public void setPassword( String password )
- {
- this.password = password;
- }
-
- public String getAuthenticationMethod()
- {
- return authenticationMethod;
- }
-
- public void setAuthenticationMethod( String authenticationMethod )
- {
- this.authenticationMethod = authenticationMethod;
- }
-
- public List<Class<?>> getObjectFactories()
- {
- if ( objectFactories == null )
- {
- objectFactories = new ArrayList<Class<?>>( 0 );
- }
-
- return objectFactories;
- }
-
- public void setObjectFactories( List<Class<?>> objectFactories )
- {
- this.objectFactories = objectFactories;
- }
-
- public List<Class<?>> getStateFactories()
- {
- if ( stateFactories == null )
- {
- stateFactories = new ArrayList<Class<?>>( 0 );
- }
-
- return stateFactories;
- }
-
- public void setStateFactories( List<Class<?>> stateFactories )
- {
- this.stateFactories = stateFactories;
- }
-
- public Properties getExtraProperties()
- {
- if ( extraProperties == null )
- {
- extraProperties = new Properties();
- }
-
- return extraProperties;
- }
-
- public void setExtraProperties( Properties extraProperties )
- {
- this.extraProperties = extraProperties;
- }
-
- // ----------------------------------------------------------------------
- //
- // ----------------------------------------------------------------------
-
- public void check()
- throws LdapException
- {
- if ( port < 0 || port > 65535 )
- {
- throw new LdapException( "The port must be between 1 and 65535." );
- }
- if ( baseDn == null )
- {
- throw new LdapException( "The base DN must be set." );
- }
- if ( StringUtils.isEmpty( contextFactory ) )
- {
- throw new LdapException( "The context factory must be set." );
- }
- if ( password != null && bindDn == null )
- {
- throw new LdapException( "The password cant be set unless the bind dn is." );
- }
-
- if ( extraProperties == null )
- {
- extraProperties = new Properties();
- }
- }
-
- // ----------------------------------------------------------------------
- //
- // ----------------------------------------------------------------------
-
- public String toString()
- {
- return "{LdapConnectionConfiguration: " +
- "hostname: " + getHostname() + ", " +
- "port: " + getPort() + ", " +
- "ssl: " + isSsl() + ", " +
- "baseDn: " + getBaseDn() + ", " +
- "contextFactory: " + getContextFactory() + ", " +
- "bindDn: " + getBindDn() + ", " +
- "password: " + getPassword() + ", " +
- "authenticationMethod: " + getAuthenticationMethod() + ", " +
- "objectFactories: " + getObjectFactories() + ", " +
- "stateFactories: " + getStateFactories() + ", " +
- "extraProperties: " + new TreeMap<Object, Object>( extraProperties ).toString() + "}";
- }
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap.connection;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
-import javax.naming.spi.ObjectFactory;
-import javax.naming.spi.StateFactory;
-
-
-/**
- * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
- * @version $Id$
- */
-public interface LdapConnectionFactory
-{
- String ROLE = LdapConnectionFactory.class.getName();
-
- LdapConnection getConnection()
- throws LdapException;
-
- LdapConnection getConnection( Rdn subRdn )
- throws LdapException;
-
- LdapConnection getConnection( String bindDn, String password )
- throws LdapException;
-
- LdapName getBaseDnLdapName()
- throws LdapException;
-
- void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass );
-
- void addStateFactory( Class<? extends StateFactory> objectFactoryClass );
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap.connection;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import javax.naming.NamingException;
-
-/**
- * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
- * @version $Id$
- */
-public class LdapException
- extends NamingException
-{
- public LdapException( String message )
- {
- super( message );
- }
-
- public LdapException( String message, Throwable t )
- {
- super( message );
- setRootCause( t );
- }
-}
default-lazy-init="true">
<context:annotation-config />
- <context:component-scan base-package="org.codehaus.plexus.redback.common.ldap"/>
+ <context:component-scan base-package="org.apache.archiva.redback.common.ldap"/>
</beans>
\ No newline at end of file
--- /dev/null
+package org.apache.archiva.redback.common.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.archiva.redback.common.ldap.LdapUserMapper;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+@RunWith( SpringJUnit4ClassRunner.class )
+@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
+public class LdapUserMapperTest
+ extends TestCase
+{
+ @Inject @Named(value = "userMapper#ldap")
+ LdapUserMapper mapper;
+
+ @Test
+ public void testConfiguration()
+ {
+ assertEquals( "o=People,dc=codehaus,dc=org", mapper.getUserBaseDn() );
+ }
+}
--- /dev/null
+package org.apache.archiva.redback.common.ldap.connection;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+@RunWith( SpringJUnit4ClassRunner.class )
+@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
+public class ConfigurableLdapConnectionFactoryTest
+ extends TestCase
+{
+
+ @Inject
+ @Named( value = "ldapConnectionFactory#configurable" )
+ ConfigurableLdapConnectionFactory factory;
+
+ @Test
+ public void testConfiguration()
+ {
+ assertEquals( "dc=codehaus,dc=org", factory.getConfiguration().getBaseDn().toString() );
+ assertEquals( "uid=user,dc=codehaus,dc=org", factory.getConfiguration().getBindDn().toString() );
+ }
+}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import junit.framework.TestCase;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
-public class LdapUserMapperTest
- extends TestCase
-{
- @Inject @Named(value = "userMapper#ldap")
- LdapUserMapper mapper;
-
- @Test
- public void testConfiguration()
- {
- assertEquals( "o=People,dc=codehaus,dc=org", mapper.getUserBaseDn() );
- }
-}
+++ /dev/null
-package org.codehaus.plexus.redback.common.ldap.connection;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import junit.framework.TestCase;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
-public class ConfigurableLdapConnectionFactoryTest
- extends TestCase
-{
-
- @Inject
- @Named( value = "ldapConnectionFactory#configurable" )
- ConfigurableLdapConnectionFactory factory;
-
- @Test
- public void testConfiguration()
- {
- assertEquals( "dc=codehaus,dc=org", factory.getConfiguration().getBaseDn().toString() );
- assertEquals( "uid=user,dc=codehaus,dc=org", factory.getConfiguration().getBindDn().toString() );
- }
-}
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="userConfiguration" class="org.codehaus.plexus.redback.configuration.UserConfiguration">
<property name="configs">
</configuration>
</component>
<component>
- <role>org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory</role>
+ <role>org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory</role>
<role-hint>configurable</role-hint>
- <implementation>org.codehaus.plexus.redback.common.ldap.connection.ConfigurableLdapConnectionFactory</implementation>
+ <implementation>org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory</implementation>
<description></description>
<configuration>
<hostname>localhost</hostname>
*/
+import org.apache.archiva.redback.common.ldap.LdapUser;
+import org.apache.archiva.redback.common.ldap.UserMapper;
import org.apache.archiva.redback.users.AbstractUserManager;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserNotFoundException;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.MappingException;
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapException;
+import org.apache.archiva.redback.common.ldap.MappingException;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
+import org.apache.archiva.redback.common.ldap.connection.LdapException;
import org.apache.archiva.redback.users.UserQuery;
import org.apache.archiva.redback.users.ldap.ctl.LdapController;
import org.apache.archiva.redback.users.ldap.ctl.LdapControllerException;
* under the License.
*/
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
+import org.apache.archiva.redback.common.ldap.UserMapper;
import org.apache.archiva.redback.users.AbstractUserQuery;
public class LdapUserQuery
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
+import org.apache.archiva.redback.common.ldap.LdapUser;
+import org.apache.archiva.redback.common.ldap.LdapUserMapper;
+import org.apache.archiva.redback.common.ldap.UserMapper;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserManager;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.LdapUserMapper;
-import org.codehaus.plexus.redback.common.ldap.MappingException;
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
+import org.apache.archiva.redback.common.ldap.MappingException;
import org.apache.archiva.redback.users.ldap.LdapUserQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
ctls.setDerefLinkFlag( true );
ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
ctls.setReturningAttributes( mapper.getReturningAttributes() );
- ctls.setCountLimit( ( ( LdapUserMapper ) mapper ).getMaxResultCount() );
+ ctls.setCountLimit( ( (LdapUserMapper) mapper ).getMaxResultCount() );
String finalFilter = "(&(objectClass=" + mapper.getUserObjectClass() + ")" +
( mapper.getUserFilter() != null ? mapper.getUserFilter() : "" ) + query.getLdapFilter(mapper) + ")";
* under the License.
*/
+import org.apache.archiva.redback.common.ldap.LdapUser;
import org.apache.archiva.redback.users.User;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.MappingException;
+import org.apache.archiva.redback.common.ldap.MappingException;
import org.apache.archiva.redback.users.ldap.LdapUserQuery;
import javax.naming.directory.DirContext;
* under the License.
*/
-import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
+import org.apache.archiva.redback.common.ldap.LdapUser;
import org.codehaus.plexus.cache.builder.CacheBuilder;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
}
/**
- * @see LdapCacheService#addUser(org.codehaus.plexus.redback.common.ldap.LdapUser)
+ * @see LdapCacheService#addUser(org.apache.archiva.redback.common.ldap.LdapUser)
*/
public void addUser( LdapUser user )
{
* under the License.
*/
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.apache.archiva.redback.common.ldap.LdapUser;
/**
* LdapCacheService
import junit.framework.TestCase;
import org.apache.archiva.redback.users.User;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.codehaus.plexus.redback.policy.PasswordEncoder;
import org.codehaus.plexus.redback.policy.encoders.SHA1PasswordEncoder;
import org.apache.archiva.redback.users.UserManager;
*/
import junit.framework.TestCase;
-import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.apache.archiva.redback.common.ldap.LdapUser;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
<property name="password" value="secret"/>
</bean>
- <bean name="ldapConnectionFactory#configurable" class="org.codehaus.plexus.redback.common.ldap.connection.ConfigurableLdapConnectionFactory">
+ <bean name="ldapConnectionFactory#configurable" class="org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory">
<property name="hostname" value="localhost"/>
<property name="port" value="${ldapPort}"/>
<property name="baseDn" value="dc=redback,dc=plexus,dc=codehaus,dc=org"/>
<property name="userConf" ref="userConfiguration"/>
</bean>
- <bean name="userMapper#ldap" class="org.codehaus.plexus.redback.common.ldap.LdapUserMapper">
+ <bean name="userMapper#ldap" class="org.apache.archiva.redback.common.ldap.LdapUserMapper">
<property name="emailAttribute" value="mail"/>
<property name="fullNameAttribute" value="givenName"/>
<property name="passwordAttribute" value="userPassword"/>