use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class HashedCredentialsMatcherTest method testBackwardsCompatibleSaltedAuthenticationInfo.
/**
* Test backwards compatibility of salted credentials before
* <a href="https://issues.apache.org/jira/browse/SHIRO-186">SHIRO-186</a> edits.
*/
@Test
public void testBackwardsCompatibleSaltedAuthenticationInfo() {
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(Sha1Hash.ALGORITHM_NAME);
// enable this for Shiro 1.0 backwards compatibility:
matcher.setHashSalted(true);
// simulate an account with SHA-1 hashed password, using the username as the salt
// (BAD IDEA, but backwards-compatible):
final String username = "username";
final String password = "password";
final Object hashedPassword = new Sha1Hash(password, username).getBytes();
AuthenticationInfo account = new AuthenticationInfo() {
public PrincipalCollection getPrincipals() {
return new SimplePrincipalCollection(username, "realmName");
}
public Object getCredentials() {
return hashedPassword;
}
};
// simulate a username/password (plaintext) token created in response to a login attempt:
AuthenticationToken token = new UsernamePasswordToken("username", "password");
// verify the hashed token matches what is in the account:
assertTrue(matcher.doCredentialsMatch(token, account));
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testHasRole.
/*
* Tests that roles can't be read while the realm is being loaded.
*/
@Test
public void testHasRole() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
assertTrue("principal doesn't have role when it should", realm.hasRole(principalCollection, "role2"));
assertTrue("principal doesn't have all roles when it should", realm.hasAllRoles(principalCollection, Arrays.asList(new String[] { "role1", "role2" })));
}
});
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testCheckPermission.
/*
* Tests that a principal's permissions can't be checked while the realm is being loaded.
*/
@Test
public void testCheckPermission() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
try {
realm.checkPermission(principalCollection, "role1_permission1");
realm.checkPermissions(principalCollection, new String[] { "role1_permission1", "role2_permission2" });
} catch (AuthorizationException ae) {
fail("principal doesn't have permission when it should");
}
}
});
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testIsPermitted.
/*
* Tests that a principal's permissions can't be checked while the realm is being loaded.
*/
@Test
public void testIsPermitted() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
assertTrue("permission not permitted when it should be", realm.isPermitted(principalCollection, "role1_permission1"));
assertTrue("permission not permitted when it should be", realm.isPermittedAll(principalCollection, new String[] { "role1_permission1", "role2_permission2" }));
}
});
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class DefaultWebSecurityManagerTest method testBuildNonWebSubjectWithDefaultServletContainerSessionManager.
/**
* Asserts fix for <a href="https://issues.apache.org/jira/browse/SHIRO-350">SHIRO-350</a>.
*/
@Test
public void testBuildNonWebSubjectWithDefaultServletContainerSessionManager() {
Ini ini = new Ini();
Ini.Section section = ini.addSection(IniRealm.USERS_SECTION_NAME);
section.put("user1", "user1");
WebIniSecurityManagerFactory factory = new WebIniSecurityManagerFactory(ini);
WebSecurityManager securityManager = (WebSecurityManager) factory.getInstance();
PrincipalCollection principals = new SimplePrincipalCollection("user1", "iniRealm");
Subject subject = new Subject.Builder(securityManager).principals(principals).buildSubject();
assertNotNull(subject);
assertEquals("user1", subject.getPrincipal());
}
Aggregations