use of org.apache.shiro.authc.UsernamePasswordToken in project shiro by apache.
the class DelegatingSubjectTest method testRunAs.
@Test
public void testRunAs() {
Ini ini = new Ini();
Ini.Section users = ini.addSection("users");
users.put("user1", "user1,role1");
users.put("user2", "user2,role2");
users.put("user3", "user3,role3");
IniSecurityManagerFactory factory = new IniSecurityManagerFactory(ini);
SecurityManager sm = factory.getInstance();
// login as user1
Subject subject = new Subject.Builder(sm).buildSubject();
subject.login(new UsernamePasswordToken("user1", "user1"));
assertFalse(subject.isRunAs());
assertEquals("user1", subject.getPrincipal());
assertTrue(subject.hasRole("role1"));
assertFalse(subject.hasRole("role2"));
assertFalse(subject.hasRole("role3"));
// no previous principals since we haven't called runAs yet
assertNull(subject.getPreviousPrincipals());
// runAs user2:
subject.runAs(new SimplePrincipalCollection("user2", IniSecurityManagerFactory.INI_REALM_NAME));
assertTrue(subject.isRunAs());
assertEquals("user2", subject.getPrincipal());
assertTrue(subject.hasRole("role2"));
assertFalse(subject.hasRole("role1"));
assertFalse(subject.hasRole("role3"));
// assert we still have the previous (user1) principals:
PrincipalCollection previous = subject.getPreviousPrincipals();
assertFalse(previous == null || previous.isEmpty());
assertTrue(previous.getPrimaryPrincipal().equals("user1"));
// test the stack functionality: While as user2, run as user3:
subject.runAs(new SimplePrincipalCollection("user3", IniSecurityManagerFactory.INI_REALM_NAME));
assertTrue(subject.isRunAs());
assertEquals("user3", subject.getPrincipal());
assertTrue(subject.hasRole("role3"));
assertFalse(subject.hasRole("role1"));
assertFalse(subject.hasRole("role2"));
// assert we still have the previous (user2) principals in the stack:
previous = subject.getPreviousPrincipals();
assertFalse(previous == null || previous.isEmpty());
assertTrue(previous.getPrimaryPrincipal().equals("user2"));
// drop down to user2:
subject.releaseRunAs();
// assert still run as:
assertTrue(subject.isRunAs());
assertEquals("user2", subject.getPrincipal());
assertTrue(subject.hasRole("role2"));
assertFalse(subject.hasRole("role1"));
assertFalse(subject.hasRole("role3"));
// assert we still have the previous (user1) principals:
previous = subject.getPreviousPrincipals();
assertFalse(previous == null || previous.isEmpty());
assertTrue(previous.getPrimaryPrincipal().equals("user1"));
// drop down to original user1:
subject.releaseRunAs();
// assert we're no longer runAs:
assertFalse(subject.isRunAs());
assertEquals("user1", subject.getPrincipal());
assertTrue(subject.hasRole("role1"));
assertFalse(subject.hasRole("role2"));
assertFalse(subject.hasRole("role3"));
// no previous principals in orig state
assertNull(subject.getPreviousPrincipals());
subject.logout();
LifecycleUtils.destroy(sm);
}
use of org.apache.shiro.authc.UsernamePasswordToken in project shiro by apache.
the class DefaultSecurityManagerTest method testDefaultConfig.
@Test
public void testDefaultConfig() {
Subject subject = SecurityUtils.getSubject();
AuthenticationToken token = new UsernamePasswordToken("guest", "guest");
subject.login(token);
assertTrue(subject.isAuthenticated());
assertTrue("guest".equals(subject.getPrincipal()));
assertTrue(subject.hasRole("guest"));
Session session = subject.getSession();
session.setAttribute("key", "value");
assertEquals(session.getAttribute("key"), "value");
subject.logout();
assertNull(subject.getSession(false));
assertNull(subject.getPrincipal());
assertNull(subject.getPrincipals());
}
use of org.apache.shiro.authc.UsernamePasswordToken in project shiro by apache.
the class JDBCRealmTest method testRolePresent.
@Test
public void testRolePresent() throws Exception {
String testMethodName = name.getMethodName();
JdbcRealm realm = realmMap.get(testMethodName);
createDefaultSchema(testMethodName, false);
realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
Subject.Builder builder = new Subject.Builder(securityManager);
Subject currentUser = builder.buildSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
currentUser.login(token);
Assert.assertTrue(currentUser.hasRole(testRole));
}
use of org.apache.shiro.authc.UsernamePasswordToken in project shiro by apache.
the class JDBCRealmTest method testExternalSuccess.
@Test
public void testExternalSuccess() throws Exception {
String testMethodName = name.getMethodName();
JdbcRealm realm = realmMap.get(testMethodName);
createDefaultSchema(testMethodName, true);
realm.setSaltStyle(JdbcRealm.SaltStyle.EXTERNAL);
Subject.Builder builder = new Subject.Builder(securityManager);
Subject currentUser = builder.buildSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
currentUser.login(token);
currentUser.logout();
}
use of org.apache.shiro.authc.UsernamePasswordToken in project shiro by apache.
the class JDBCRealmTest method testRoleNotPresent.
@Test
public void testRoleNotPresent() throws Exception {
String testMethodName = name.getMethodName();
JdbcRealm realm = realmMap.get(testMethodName);
createDefaultSchema(testMethodName, false);
realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
Subject.Builder builder = new Subject.Builder(securityManager);
Subject currentUser = builder.buildSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
currentUser.login(token);
Assert.assertFalse(currentUser.hasRole("Game Overall Director"));
}
Aggregations