1 package org.apache.archiva.redback.jsecurity;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.redback.jsecurity.PrincipalLockedException;
24 import org.apache.archiva.redback.jsecurity.PrincipalPasswordChangeRequiredException;
25 import org.apache.archiva.redback.jsecurity.RedbackRealm;
26 import org.apache.archiva.redback.policy.UserSecurityPolicy;
27 import org.apache.archiva.redback.rbac.Operation;
28 import org.apache.archiva.redback.rbac.Permission;
29 import org.apache.archiva.redback.rbac.RBACManager;
30 import org.apache.archiva.redback.rbac.Resource;
31 import org.apache.archiva.redback.rbac.Role;
32 import org.apache.archiva.redback.rbac.UserAssignment;
33 import org.apache.archiva.redback.users.User;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.jsecurity.authc.IncorrectCredentialsException;
36 import org.jsecurity.authc.UsernamePasswordToken;
37 import org.jsecurity.mgt.DefaultSecurityManager;
38 import org.jsecurity.subject.PrincipalCollection;
39 import org.jsecurity.subject.SimplePrincipalCollection;
40 import org.jsecurity.subject.Subject;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 import javax.inject.Inject;
49 import javax.inject.Named;
52 @RunWith( SpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
54 public class RedbackRealmTest
57 private DefaultSecurityManager securityManager;
59 private RedbackRealm realm;
62 @Named( value = "userManager#memory" )
63 private UserManager userManager;
66 @Named( value = "rBACManager#memory" )
67 private RBACManager rbacManager;
70 private UserSecurityPolicy userSecurityPolicy;
79 securityManager = new DefaultSecurityManager();
81 realm = new RedbackRealm( userManager, rbacManager, userSecurityPolicy );
82 securityManager.setRealm( realm );
84 user = userManager.createUser( "test1", "John Tester", "jtester@redback.codehaus.org" );
85 user.setPassword( "password1" );
86 userManager.addUser( user );
87 userManager.updateUser( user );
91 public void tearDown()
95 securityManager.destroy();
96 securityManager = null;
100 protected String getPlexusConfigLocation()
105 public void testThrowsExceptionIfUserAccountLocked()
108 user.setLocked( true );
109 userManager.updateUser( user );
112 securityManager.login( new UsernamePasswordToken( "test1", "password1" ) );
113 fail( "Should not be able to login" );
115 catch ( PrincipalLockedException e )
122 public void testThrowsExceptionIfUserAccountNeedsPasswordChange()
125 user.setPasswordChangeRequired( true );
126 userManager.updateUser( user );
129 securityManager.login( new UsernamePasswordToken( "test1", "password1" ) );
130 fail( "Should not be able to login" );
132 catch ( PrincipalPasswordChangeRequiredException e )
139 public void testUnsuccessfullAuthAttemptsLockAccount()
142 assertFalse( user.isLocked() );
143 userSecurityPolicy.setLoginAttemptCount( 2 );
146 securityManager.login( new UsernamePasswordToken( "test1", "incorrectpassowrd" ) );
147 fail( "password should be incorrect" );
149 catch ( IncorrectCredentialsException e )
151 assertFalse( user.isLocked() );
156 securityManager.login( new UsernamePasswordToken( "test1", "incorrectpassowrd" ) );
157 fail( "password should be incorrect" );
159 catch ( IncorrectCredentialsException e )
161 assertTrue( user.isLocked() );
166 public void testBasic()
169 assertEquals( 1, userManager.getUsers().size() );
171 Role role1 = rbacManager.createRole( "role1" );
172 Permission permission = rbacManager.createPermission( "Allowed to write to repository" );
173 Operation operation = rbacManager.createOperation( "myop" );
174 Resource resource = rbacManager.createResource( "filesystem" );
176 permission.setOperation( operation );
177 permission.setPermanent( false );
178 permission.setResource( resource );
180 role1.addPermission( permission );
181 rbacManager.savePermission( permission );
182 rbacManager.saveRole( role1 );
184 Role role2 = rbacManager.createRole( "role2" );
186 UserAssignment assignment = rbacManager.createUserAssignment( user.getUsername() );
187 assignment.addRoleName( "role1" );
188 rbacManager.saveUserAssignment( assignment );
190 Subject subject = securityManager.login( new UsernamePasswordToken( "test1", "password1" ) );
191 assertTrue( subject.isAuthenticated() );
192 assertTrue( subject.hasRole( "role1" ) );
193 assertFalse( subject.hasRole( "role2" ) );
195 PrincipalCollection principals = new SimplePrincipalCollection( "test1", realm.getName() );
197 assertTrue( securityManager.isPermitted( principals, "Allowed to write to repository" ) );