]> source.dussan.org Git - archiva.git/commitdiff
package change to o.a.a.r
authorOlivier Lamy <olamy@apache.org>
Sat, 7 Apr 2012 13:42:48 +0000 (13:42 +0000)
committerOlivier Lamy <olamy@apache.org>
Sat, 7 Apr 2012 13:42:48 +0000 (13:42 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1310769 13f79535-47bb-0310-9956-ffa450edef68

redback-users/redback-authentication-users/src/main/java/org/apache/archiva/redback/authentication/users/UserManagerAuthenticator.java [new file with mode: 0644]
redback-users/redback-authentication-users/src/main/java/org/codehaus/plexus/redback/authentication/users/UserManagerAuthenticator.java [deleted file]
redback-users/redback-authentication-users/src/test/java/org/apache/archiva/redback/authentication/users/UserManagerAuthenticatorTest.java [new file with mode: 0644]
redback-users/redback-authentication-users/src/test/java/org/codehaus/plexus/redback/authentication/users/UserManagerAuthenticatorTest.java [deleted file]
redback-users/redback-authentication-users/src/test/resources/spring-context.xml

diff --git a/redback-users/redback-authentication-users/src/main/java/org/apache/archiva/redback/authentication/users/UserManagerAuthenticator.java b/redback-users/redback-authentication-users/src/main/java/org/apache/archiva/redback/authentication/users/UserManagerAuthenticator.java
new file mode 100644 (file)
index 0000000..9c5dad9
--- /dev/null
@@ -0,0 +1,196 @@
+package org.apache.archiva.redback.authentication.users;
+
+/*
+ * 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.UserManager;
+import org.apache.archiva.redback.users.UserNotFoundException;
+import org.codehaus.plexus.redback.authentication.AuthenticationConstants;
+import org.codehaus.plexus.redback.authentication.AuthenticationDataSource;
+import org.codehaus.plexus.redback.authentication.AuthenticationException;
+import org.codehaus.plexus.redback.authentication.AuthenticationResult;
+import org.codehaus.plexus.redback.authentication.Authenticator;
+import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
+import org.codehaus.plexus.redback.policy.AccountLockedException;
+import org.codehaus.plexus.redback.policy.MustChangePasswordException;
+import org.codehaus.plexus.redback.policy.PasswordEncoder;
+import org.codehaus.plexus.redback.policy.PolicyViolationException;
+import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
+import org.apache.archiva.redback.users.User;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * {@link Authenticator} implementation that uses a wrapped {@link UserManager} to authenticate.
+ *
+ * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
+ * @version $Id$
+ */
+@Service( "authenticator#user-manager" )
+public class UserManagerAuthenticator
+    implements Authenticator
+{
+    private Logger log = LoggerFactory.getLogger( UserManagerAuthenticator.class );
+
+    @Inject
+    @Named( value = "userManager#jdo" )
+    private UserManager userManager;
+
+    @Inject
+    private UserSecurityPolicy securityPolicy;
+
+    public String getId()
+    {
+        return "UserManagerAuthenticator";
+    }
+
+    /**
+     * @throws org.codehaus.plexus.redback.policy.AccountLockedException
+     *
+     * @throws MustChangePasswordException
+     * @throws MustChangePasswordException
+     * @throws PolicyViolationException
+     * @see org.codehaus.plexus.redback.authentication.Authenticator#authenticate(org.codehaus.plexus.redback.authentication.AuthenticationDataSource)
+     */
+    public AuthenticationResult authenticate( AuthenticationDataSource ds )
+        throws AuthenticationException, AccountLockedException, MustChangePasswordException
+    {
+        boolean authenticationSuccess = false;
+        String username = null;
+        Exception resultException = null;
+        PasswordBasedAuthenticationDataSource source = (PasswordBasedAuthenticationDataSource) ds;
+        Map<String, String> authnResultExceptionsMap = new HashMap<String, String>();
+
+        try
+        {
+            log.debug( "Authenticate: {}", source );
+            User user = userManager.findUser( source.getPrincipal() );
+            username = user.getUsername();
+
+            if ( user.isLocked() )
+            {
+                throw new AccountLockedException( "Account " + source.getPrincipal() + " is locked.", user );
+            }
+
+            if ( user.isPasswordChangeRequired() && source.isEnforcePasswordChange() )
+            {
+                throw new MustChangePasswordException( "Password expired.", user );
+            }
+
+            PasswordEncoder encoder = securityPolicy.getPasswordEncoder();
+            log.debug( "PasswordEncoder: {}", encoder.getClass().getName() );
+
+            boolean isPasswordValid = encoder.isPasswordValid( user.getEncodedPassword(), source.getPassword() );
+            if ( isPasswordValid )
+            {
+                log.debug( "User {} provided a valid password", source.getPrincipal() );
+
+                try
+                {
+                    securityPolicy.extensionPasswordExpiration( user );
+                }
+                catch ( MustChangePasswordException e )
+                {
+                    user.setPasswordChangeRequired( true );
+                    throw e;
+                }
+
+                authenticationSuccess = true;
+
+                //REDBACK-151 do not make unnessesary updates to the user object
+                if ( user.getCountFailedLoginAttempts() > 0 )
+                {
+                    user.setCountFailedLoginAttempts( 0 );
+                    userManager.updateUser( user );
+                }
+
+                return new AuthenticationResult( true, source.getPrincipal(), null );
+            }
+            else
+            {
+                log.warn( "Password is Invalid for user " + source.getPrincipal() + "." );
+                authnResultExceptionsMap.put( AuthenticationConstants.AUTHN_NO_SUCH_USER,
+                                              "Password is Invalid for user " + source.getPrincipal() + "." );
+
+                try
+                {
+                    securityPolicy.extensionExcessiveLoginAttempts( user );
+                }
+                finally
+                {
+                    userManager.updateUser( user );
+                }
+
+                return new AuthenticationResult( false, source.getPrincipal(), null, authnResultExceptionsMap );
+            }
+        }
+        catch ( UserNotFoundException e )
+        {
+            log.warn( "Login for user " + source.getPrincipal() + " failed. user not found." );
+            resultException = e;
+            authnResultExceptionsMap.put( AuthenticationConstants.AUTHN_NO_SUCH_USER,
+                                          "Login for user \" + source.getPrincipal() + \" failed. user not found." );
+        }
+
+        return new AuthenticationResult( authenticationSuccess, username, resultException, authnResultExceptionsMap );
+    }
+
+    /**
+     * Returns the wrapped {@link UserManager} used by this {@link Authenticator}
+     * implementation for authentication.
+     *
+     * @return the userManager
+     */
+    public UserManager getUserManager()
+    {
+        return userManager;
+    }
+
+    /**
+     * Sets a {@link UserManager} to be used by this {@link Authenticator}
+     * implementation for authentication.
+     *
+     * @param userManager the userManager to set
+     */
+    public void setUserManager( UserManager userManager )
+    {
+        this.userManager = userManager;
+    }
+
+    public boolean supportsDataSource( AuthenticationDataSource source )
+    {
+        return ( source instanceof PasswordBasedAuthenticationDataSource );
+    }
+
+    public UserSecurityPolicy getSecurityPolicy()
+    {
+        return securityPolicy;
+    }
+
+    public void setSecurityPolicy( UserSecurityPolicy securityPolicy )
+    {
+        this.securityPolicy = securityPolicy;
+    }
+}
diff --git a/redback-users/redback-authentication-users/src/main/java/org/codehaus/plexus/redback/authentication/users/UserManagerAuthenticator.java b/redback-users/redback-authentication-users/src/main/java/org/codehaus/plexus/redback/authentication/users/UserManagerAuthenticator.java
deleted file mode 100644 (file)
index 991182d..0000000
+++ /dev/null
@@ -1,196 +0,0 @@
-package org.codehaus.plexus.redback.authentication.users;
-
-/*
- * 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.UserManager;
-import org.apache.archiva.redback.users.UserNotFoundException;
-import org.codehaus.plexus.redback.authentication.AuthenticationConstants;
-import org.codehaus.plexus.redback.authentication.AuthenticationDataSource;
-import org.codehaus.plexus.redback.authentication.AuthenticationException;
-import org.codehaus.plexus.redback.authentication.AuthenticationResult;
-import org.codehaus.plexus.redback.authentication.Authenticator;
-import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
-import org.codehaus.plexus.redback.policy.AccountLockedException;
-import org.codehaus.plexus.redback.policy.MustChangePasswordException;
-import org.codehaus.plexus.redback.policy.PasswordEncoder;
-import org.codehaus.plexus.redback.policy.PolicyViolationException;
-import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
-import org.apache.archiva.redback.users.User;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Service;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * {@link Authenticator} implementation that uses a wrapped {@link UserManager} to authenticate.
- *
- * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
- * @version $Id$
- */
-@Service( "authenticator#user-manager" )
-public class UserManagerAuthenticator
-    implements Authenticator
-{
-    private Logger log = LoggerFactory.getLogger( UserManagerAuthenticator.class );
-
-    @Inject
-    @Named( value = "userManager#jdo" )
-    private UserManager userManager;
-
-    @Inject
-    private UserSecurityPolicy securityPolicy;
-
-    public String getId()
-    {
-        return "UserManagerAuthenticator";
-    }
-
-    /**
-     * @throws org.codehaus.plexus.redback.policy.AccountLockedException
-     *
-     * @throws MustChangePasswordException
-     * @throws MustChangePasswordException
-     * @throws PolicyViolationException
-     * @see org.codehaus.plexus.redback.authentication.Authenticator#authenticate(org.codehaus.plexus.redback.authentication.AuthenticationDataSource)
-     */
-    public AuthenticationResult authenticate( AuthenticationDataSource ds )
-        throws AuthenticationException, AccountLockedException, MustChangePasswordException
-    {
-        boolean authenticationSuccess = false;
-        String username = null;
-        Exception resultException = null;
-        PasswordBasedAuthenticationDataSource source = (PasswordBasedAuthenticationDataSource) ds;
-        Map<String, String> authnResultExceptionsMap = new HashMap<String, String>();
-
-        try
-        {
-            log.debug( "Authenticate: {}", source );
-            User user = userManager.findUser( source.getPrincipal() );
-            username = user.getUsername();
-
-            if ( user.isLocked() )
-            {
-                throw new AccountLockedException( "Account " + source.getPrincipal() + " is locked.", user );
-            }
-
-            if ( user.isPasswordChangeRequired() && source.isEnforcePasswordChange() )
-            {
-                throw new MustChangePasswordException( "Password expired.", user );
-            }
-
-            PasswordEncoder encoder = securityPolicy.getPasswordEncoder();
-            log.debug( "PasswordEncoder: {}", encoder.getClass().getName() );
-
-            boolean isPasswordValid = encoder.isPasswordValid( user.getEncodedPassword(), source.getPassword() );
-            if ( isPasswordValid )
-            {
-                log.debug( "User {} provided a valid password", source.getPrincipal() );
-
-                try
-                {
-                    securityPolicy.extensionPasswordExpiration( user );
-                }
-                catch ( MustChangePasswordException e )
-                {
-                    user.setPasswordChangeRequired( true );
-                    throw e;
-                }
-
-                authenticationSuccess = true;
-
-                //REDBACK-151 do not make unnessesary updates to the user object
-                if ( user.getCountFailedLoginAttempts() > 0 )
-                {
-                    user.setCountFailedLoginAttempts( 0 );
-                    userManager.updateUser( user );
-                }
-
-                return new AuthenticationResult( true, source.getPrincipal(), null );
-            }
-            else
-            {
-                log.warn( "Password is Invalid for user " + source.getPrincipal() + "." );
-                authnResultExceptionsMap.put( AuthenticationConstants.AUTHN_NO_SUCH_USER,
-                                              "Password is Invalid for user " + source.getPrincipal() + "." );
-
-                try
-                {
-                    securityPolicy.extensionExcessiveLoginAttempts( user );
-                }
-                finally
-                {
-                    userManager.updateUser( user );
-                }
-
-                return new AuthenticationResult( false, source.getPrincipal(), null, authnResultExceptionsMap );
-            }
-        }
-        catch ( UserNotFoundException e )
-        {
-            log.warn( "Login for user " + source.getPrincipal() + " failed. user not found." );
-            resultException = e;
-            authnResultExceptionsMap.put( AuthenticationConstants.AUTHN_NO_SUCH_USER,
-                                          "Login for user \" + source.getPrincipal() + \" failed. user not found." );
-        }
-
-        return new AuthenticationResult( authenticationSuccess, username, resultException, authnResultExceptionsMap );
-    }
-
-    /**
-     * Returns the wrapped {@link UserManager} used by this {@link Authenticator}
-     * implementation for authentication.
-     *
-     * @return the userManager
-     */
-    public UserManager getUserManager()
-    {
-        return userManager;
-    }
-
-    /**
-     * Sets a {@link UserManager} to be used by this {@link Authenticator}
-     * implementation for authentication.
-     *
-     * @param userManager the userManager to set
-     */
-    public void setUserManager( UserManager userManager )
-    {
-        this.userManager = userManager;
-    }
-
-    public boolean supportsDataSource( AuthenticationDataSource source )
-    {
-        return ( source instanceof PasswordBasedAuthenticationDataSource );
-    }
-
-    public UserSecurityPolicy getSecurityPolicy()
-    {
-        return securityPolicy;
-    }
-
-    public void setSecurityPolicy( UserSecurityPolicy securityPolicy )
-    {
-        this.securityPolicy = securityPolicy;
-    }
-}
diff --git a/redback-users/redback-authentication-users/src/test/java/org/apache/archiva/redback/authentication/users/UserManagerAuthenticatorTest.java b/redback-users/redback-authentication-users/src/test/java/org/apache/archiva/redback/authentication/users/UserManagerAuthenticatorTest.java
new file mode 100644 (file)
index 0000000..507abb5
--- /dev/null
@@ -0,0 +1,215 @@
+package org.apache.archiva.redback.authentication.users;
+
+/*
+ * 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.authentication.users.UserManagerAuthenticator;
+import org.apache.archiva.redback.users.User;
+import org.apache.archiva.redback.users.UserManager;
+import org.apache.archiva.redback.users.UserNotFoundException;
+import org.codehaus.plexus.redback.authentication.AuthenticationException;
+import org.codehaus.plexus.redback.authentication.AuthenticationResult;
+import org.codehaus.plexus.redback.authentication.Authenticator;
+import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
+import org.codehaus.plexus.redback.policy.AccountLockedException;
+import org.codehaus.plexus.redback.policy.MustChangePasswordException;
+import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
+import org.junit.Before;
+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;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * Tests for {@link org.apache.archiva.redback.authentication.users.UserManagerAuthenticator} implementation.
+ *
+ * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
+ */
+@RunWith( SpringJUnit4ClassRunner.class )
+@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
+public class UserManagerAuthenticatorTest
+    extends TestCase
+{
+    @Inject
+    private UserSecurityPolicy userSecurityPolicy;
+
+    @Inject
+    @Named(value = "authenticator#user-manager")
+    Authenticator component;
+
+    @Inject
+    @Named(value = "userManager#memory")
+    UserManager um;
+
+    @Before
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+        userSecurityPolicy.setEnabled( false );
+    }
+
+    @Test
+    public void testLookup()
+        throws Exception
+    {
+        assertNotNull( component );
+        assertEquals( UserManagerAuthenticator.class.getName(), component.getClass().getName() );
+    }
+
+    @Test
+    public void testAuthenticate()
+        throws Exception
+    {
+        // Set up a few users for the Authenticator
+
+        User user = um.createUser( "test", "Test User", "testuser@somedomain.com" );
+        user.setPassword( "testpass" );
+        um.addUser( user );
+
+        user = um.createUser( "guest", "Guest User", "testuser@somedomain.com" );
+        user.setPassword( "guestpass" );
+        um.addUser( user );
+
+        user = um.createUser( "anonymous", "Anonymous User", "testuser@somedomain.com" );
+        user.setPassword( "nopass" );
+        um.addUser( user );
+
+        // test with valid credentials
+        Authenticator auth = component;
+        assertNotNull( auth );
+
+        AuthenticationResult result = auth.authenticate( createAuthDataSource( "anonymous", "nopass" ) );
+        assertTrue( result.isAuthenticated() );
+
+        // test with invalid password
+        result = auth.authenticate( createAuthDataSource( "anonymous", "wrongpass" ) );
+        assertFalse( result.isAuthenticated() );
+        assertNull( result.getException() );
+
+        // test with unknown user
+        result = auth.authenticate( createAuthDataSource( "unknownuser", "wrongpass" ) );
+        assertFalse( result.isAuthenticated() );
+        assertNotNull( result.getException() );
+        assertEquals( result.getException().getClass().getName(), UserNotFoundException.class.getName() );
+    }
+
+    @Test
+    public void testAuthenticateLockedPassword()
+        throws AuthenticationException, MustChangePasswordException, UserNotFoundException
+    {
+        userSecurityPolicy.setEnabled( true );
+
+        // Set up a user for the Authenticator
+        User user = um.createUser( "testuser", "Test User Locked Password", "testuser@somedomain.com" );
+        user.setPassword( "correctpass1" );
+        user.setValidated( true );
+        user.setPasswordChangeRequired( false );
+        um.addUser( user );
+
+        Authenticator auth = component;
+        assertNotNull( auth );
+
+        boolean hasException = false;
+        AuthenticationResult result = null;
+
+        try
+        {
+            // test password lock
+            for ( int i = 0; i < 11; i++ )
+            {
+                result = auth.authenticate( createAuthDataSource( "testuser", "wrongpass" ) );
+            }
+        }
+        catch ( AccountLockedException e )
+        {
+            hasException = true;
+        }
+        finally
+        {
+            assertNotNull( result );
+            assertFalse( result.isAuthenticated() );
+            assertTrue( hasException );
+        }
+    }
+
+    @Test
+    public void testAuthenticateExpiredPassword()
+        throws AuthenticationException, AccountLockedException, UserNotFoundException
+    {
+        userSecurityPolicy.setEnabled( true );
+        userSecurityPolicy.setPasswordExpirationDays( 15 );
+
+        // Set up a user for the Authenticator
+        User user = um.createUser( "testuser", "Test User Expired Password", "testuser@somedomain.com" );
+        user.setPassword( "expiredpass1" );
+        user.setValidated( true );
+        user.setPasswordChangeRequired( false );
+        um.addUser( user );
+
+        Authenticator auth = component;
+        assertNotNull( auth );
+
+        boolean hasException = false;
+
+        try
+        {
+            // test successful authentication
+            AuthenticationResult result = auth.authenticate( createAuthDataSource( "testuser", "expiredpass1" ) );
+            assertTrue( result.isAuthenticated() );
+
+            // test expired password
+            user = um.findUser( "testuser" );
+
+            Calendar currentDate = Calendar.getInstance();
+            currentDate.set( Calendar.YEAR, currentDate.get( Calendar.YEAR ) - 1 );
+            Date lastPasswordChange = currentDate.getTime();
+            user.setLastPasswordChange( lastPasswordChange );
+
+            um.updateUser( user );
+
+            auth.authenticate( createAuthDataSource( "testuser", "expiredpass1" ) );
+        }
+        catch ( MustChangePasswordException e )
+        {
+            hasException = true;
+        }
+        finally
+        {
+            assertTrue( hasException );
+        }
+    }
+
+    private PasswordBasedAuthenticationDataSource createAuthDataSource( String username, String password )
+    {
+        PasswordBasedAuthenticationDataSource source = new PasswordBasedAuthenticationDataSource();
+
+        source.setPrincipal( username );
+        source.setPassword( password );
+
+        return source;
+
+    }
+}
diff --git a/redback-users/redback-authentication-users/src/test/java/org/codehaus/plexus/redback/authentication/users/UserManagerAuthenticatorTest.java b/redback-users/redback-authentication-users/src/test/java/org/codehaus/plexus/redback/authentication/users/UserManagerAuthenticatorTest.java
deleted file mode 100644 (file)
index 8256d33..0000000
+++ /dev/null
@@ -1,214 +0,0 @@
-package org.codehaus.plexus.redback.authentication.users;
-
-/*
- * 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.users.User;
-import org.apache.archiva.redback.users.UserManager;
-import org.apache.archiva.redback.users.UserNotFoundException;
-import org.codehaus.plexus.redback.authentication.AuthenticationException;
-import org.codehaus.plexus.redback.authentication.AuthenticationResult;
-import org.codehaus.plexus.redback.authentication.Authenticator;
-import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
-import org.codehaus.plexus.redback.policy.AccountLockedException;
-import org.codehaus.plexus.redback.policy.MustChangePasswordException;
-import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
-import org.junit.Before;
-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;
-import java.util.Calendar;
-import java.util.Date;
-
-/**
- * Tests for {@link UserManagerAuthenticator} implementation.
- *
- * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
- */
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
-public class UserManagerAuthenticatorTest
-    extends TestCase
-{
-    @Inject
-    private UserSecurityPolicy userSecurityPolicy;
-
-    @Inject
-    @Named(value = "authenticator#user-manager")
-    Authenticator component;
-
-    @Inject
-    @Named(value = "userManager#memory")
-    UserManager um;
-
-    @Before
-    public void setUp()
-        throws Exception
-    {
-        super.setUp();
-        userSecurityPolicy.setEnabled( false );
-    }
-
-    @Test
-    public void testLookup()
-        throws Exception
-    {
-        assertNotNull( component );
-        assertEquals( UserManagerAuthenticator.class.getName(), component.getClass().getName() );
-    }
-
-    @Test
-    public void testAuthenticate()
-        throws Exception
-    {
-        // Set up a few users for the Authenticator
-
-        User user = um.createUser( "test", "Test User", "testuser@somedomain.com" );
-        user.setPassword( "testpass" );
-        um.addUser( user );
-
-        user = um.createUser( "guest", "Guest User", "testuser@somedomain.com" );
-        user.setPassword( "guestpass" );
-        um.addUser( user );
-
-        user = um.createUser( "anonymous", "Anonymous User", "testuser@somedomain.com" );
-        user.setPassword( "nopass" );
-        um.addUser( user );
-
-        // test with valid credentials
-        Authenticator auth = component;
-        assertNotNull( auth );
-
-        AuthenticationResult result = auth.authenticate( createAuthDataSource( "anonymous", "nopass" ) );
-        assertTrue( result.isAuthenticated() );
-
-        // test with invalid password
-        result = auth.authenticate( createAuthDataSource( "anonymous", "wrongpass" ) );
-        assertFalse( result.isAuthenticated() );
-        assertNull( result.getException() );
-
-        // test with unknown user
-        result = auth.authenticate( createAuthDataSource( "unknownuser", "wrongpass" ) );
-        assertFalse( result.isAuthenticated() );
-        assertNotNull( result.getException() );
-        assertEquals( result.getException().getClass().getName(), UserNotFoundException.class.getName() );
-    }
-
-    @Test
-    public void testAuthenticateLockedPassword()
-        throws AuthenticationException, MustChangePasswordException, UserNotFoundException
-    {
-        userSecurityPolicy.setEnabled( true );
-
-        // Set up a user for the Authenticator
-        User user = um.createUser( "testuser", "Test User Locked Password", "testuser@somedomain.com" );
-        user.setPassword( "correctpass1" );
-        user.setValidated( true );
-        user.setPasswordChangeRequired( false );
-        um.addUser( user );
-
-        Authenticator auth = component;
-        assertNotNull( auth );
-
-        boolean hasException = false;
-        AuthenticationResult result = null;
-
-        try
-        {
-            // test password lock
-            for ( int i = 0; i < 11; i++ )
-            {
-                result = auth.authenticate( createAuthDataSource( "testuser", "wrongpass" ) );
-            }
-        }
-        catch ( AccountLockedException e )
-        {
-            hasException = true;
-        }
-        finally
-        {
-            assertNotNull( result );
-            assertFalse( result.isAuthenticated() );
-            assertTrue( hasException );
-        }
-    }
-
-    @Test
-    public void testAuthenticateExpiredPassword()
-        throws AuthenticationException, AccountLockedException, UserNotFoundException
-    {
-        userSecurityPolicy.setEnabled( true );
-        userSecurityPolicy.setPasswordExpirationDays( 15 );
-
-        // Set up a user for the Authenticator
-        User user = um.createUser( "testuser", "Test User Expired Password", "testuser@somedomain.com" );
-        user.setPassword( "expiredpass1" );
-        user.setValidated( true );
-        user.setPasswordChangeRequired( false );
-        um.addUser( user );
-
-        Authenticator auth = component;
-        assertNotNull( auth );
-
-        boolean hasException = false;
-
-        try
-        {
-            // test successful authentication
-            AuthenticationResult result = auth.authenticate( createAuthDataSource( "testuser", "expiredpass1" ) );
-            assertTrue( result.isAuthenticated() );
-
-            // test expired password
-            user = um.findUser( "testuser" );
-
-            Calendar currentDate = Calendar.getInstance();
-            currentDate.set( Calendar.YEAR, currentDate.get( Calendar.YEAR ) - 1 );
-            Date lastPasswordChange = currentDate.getTime();
-            user.setLastPasswordChange( lastPasswordChange );
-
-            um.updateUser( user );
-
-            auth.authenticate( createAuthDataSource( "testuser", "expiredpass1" ) );
-        }
-        catch ( MustChangePasswordException e )
-        {
-            hasException = true;
-        }
-        finally
-        {
-            assertTrue( hasException );
-        }
-    }
-
-    private PasswordBasedAuthenticationDataSource createAuthDataSource( String username, String password )
-    {
-        PasswordBasedAuthenticationDataSource source = new PasswordBasedAuthenticationDataSource();
-
-        source.setPrincipal( username );
-        source.setPassword( password );
-
-        return source;
-
-    }
-}
index 19c84c5101f183b4ada1001bb2b38c61b3d57354..a1b6c7c2f331bc47217ef12ec724c17bed88c8ba 100644 (file)
@@ -27,7 +27,7 @@
            http://www.springframework.org/schema/context/spring-context-3.0.xsd"
        default-lazy-init="true">
 
-  <bean name="authenticator#user-manager" class="org.codehaus.plexus.redback.authentication.users.UserManagerAuthenticator">
+  <bean name="authenticator#user-manager" class="org.apache.archiva.redback.authentication.users.UserManagerAuthenticator">
     <property name="userManager" ref="userManager#memory"/>
     <property name="securityPolicy" ref="userSecurityPolicy"/>
   </bean>