1 package org.codehaus.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.codehaus.plexus.redback.policy.UserSecurityPolicy;
24 import org.codehaus.plexus.redback.rbac.Operation;
25 import org.codehaus.plexus.redback.rbac.Permission;
26 import org.codehaus.plexus.redback.rbac.RBACManager;
27 import org.codehaus.plexus.redback.rbac.Resource;
28 import org.codehaus.plexus.redback.rbac.Role;
29 import org.codehaus.plexus.redback.rbac.UserAssignment;
30 import org.codehaus.plexus.redback.users.User;
31 import org.codehaus.plexus.redback.users.UserManager;
32 import org.jsecurity.authc.IncorrectCredentialsException;
33 import org.jsecurity.authc.UsernamePasswordToken;
34 import org.jsecurity.mgt.DefaultSecurityManager;
35 import org.jsecurity.subject.PrincipalCollection;
36 import org.jsecurity.subject.SimplePrincipalCollection;
37 import org.jsecurity.subject.Subject;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45 import javax.inject.Inject;
46 import javax.inject.Named;
49 @RunWith( SpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
51 public class RedbackRealmTest
54 private DefaultSecurityManager securityManager;
56 private RedbackRealm realm;
59 @Named( value = "userManager#memory" )
60 private UserManager userManager;
63 @Named( value = "rBACManager#memory" )
64 private RBACManager rbacManager;
67 private UserSecurityPolicy userSecurityPolicy;
76 securityManager = new DefaultSecurityManager();
80 realm = new RedbackRealm( userManager, rbacManager, userSecurityPolicy );
81 securityManager.setRealm( realm );
83 user = userManager.createUser( "test1", "John Tester", "jtester@redback.codehaus.org" );
84 user.setPassword( "password1" );
85 userManager.addUser( user );
86 userManager.updateUser( user );
90 public void tearDown()
94 securityManager.destroy();
95 securityManager = null;
99 protected String getPlexusConfigLocation()
104 public void testThrowsExceptionIfUserAccountLocked()
107 user.setLocked( true );
108 userManager.updateUser( user );
111 securityManager.login( new UsernamePasswordToken( "test1", "password1" ) );
112 fail( "Should not be able to login" );
114 catch ( PrincipalLockedException e )
121 public void testThrowsExceptionIfUserAccountNeedsPasswordChange()
124 user.setPasswordChangeRequired( true );
125 userManager.updateUser( user );
128 securityManager.login( new UsernamePasswordToken( "test1", "password1" ) );
129 fail( "Should not be able to login" );
131 catch ( PrincipalPasswordChangeRequiredException e )
138 public void testUnsuccessfullAuthAttemptsLockAccount()
141 assertFalse( user.isLocked() );
142 userSecurityPolicy.setLoginAttemptCount( 2 );
145 securityManager.login( new UsernamePasswordToken( "test1", "incorrectpassowrd" ) );
146 fail( "password should be incorrect" );
148 catch ( IncorrectCredentialsException e )
150 assertFalse( user.isLocked() );
155 securityManager.login( new UsernamePasswordToken( "test1", "incorrectpassowrd" ) );
156 fail( "password should be incorrect" );
158 catch ( IncorrectCredentialsException e )
160 assertTrue( user.isLocked() );
165 public void testBasic()
168 assertEquals( 1, userManager.getUsers().size() );
170 Role role1 = rbacManager.createRole( "role1" );
171 Permission permission = rbacManager.createPermission( "Allowed to write to repository" );
172 Operation operation = rbacManager.createOperation( "myop" );
173 Resource resource = rbacManager.createResource( "filesystem" );
175 permission.setOperation( operation );
176 permission.setPermanent( false );
177 permission.setResource( resource );
179 role1.addPermission( permission );
180 rbacManager.savePermission( permission );
181 rbacManager.saveRole( role1 );
183 Role role2 = rbacManager.createRole( "role2" );
185 UserAssignment assignment = rbacManager.createUserAssignment( user.getUsername() );
186 assignment.addRoleName( "role1" );
187 rbacManager.saveUserAssignment( assignment );
189 Subject subject = securityManager.login( new UsernamePasswordToken( "test1", "password1" ) );
190 assertTrue( subject.isAuthenticated() );
191 assertTrue( subject.hasRole( "role1" ) );
192 assertFalse( subject.hasRole( "role2" ) );
194 PrincipalCollection principals = new SimplePrincipalCollection( "test1", realm.getName() );
196 assertTrue( securityManager.isPermitted( principals, "Allowed to write to repository" ) );