use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class AuthorizingRealmTest method testRealmWithRolePermissionResolver.
@Test
public void testRealmWithRolePermissionResolver() {
Principal principal = new UsernamePrincipal("rolePermResolver");
PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "testRealmWithRolePermissionResolver");
AuthorizingRealm realm = new AllowAllRealm();
realm.setRolePermissionResolver(new RolePermissionResolver() {
public Collection<Permission> resolvePermissionsInRole(String roleString) {
Collection<Permission> permissions = new HashSet<Permission>();
if (roleString.equals(ROLE)) {
permissions.add(new WildcardPermission(ROLE + ":perm1"));
permissions.add(new WildcardPermission(ROLE + ":perm2"));
permissions.add(new WildcardPermission("other:*:foo"));
}
return permissions;
}
});
assertTrue(realm.hasRole(pCollection, ROLE));
assertTrue(realm.isPermitted(pCollection, ROLE + ":perm1"));
assertTrue(realm.isPermitted(pCollection, ROLE + ":perm2"));
assertFalse(realm.isPermitted(pCollection, ROLE + ":perm3"));
assertTrue(realm.isPermitted(pCollection, "other:bar:foo"));
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class AuthorizingRealmTest method testNullAuthzInfo.
@Test
public void testNullAuthzInfo() {
AuthorizingRealm realm = new AuthorizingRealm() {
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
return null;
}
};
Principal principal = new UsernamePrincipal("blah");
PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "nullAuthzRealm");
List<Permission> permList = new ArrayList<Permission>();
permList.add(new WildcardPermission("stringPerm1"));
permList.add(new WildcardPermission("stringPerm2"));
List<String> roleList = new ArrayList<String>();
roleList.add("role1");
roleList.add("role2");
boolean thrown = false;
try {
realm.checkPermission(pCollection, "stringPermission");
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkPermission(pCollection, new WildcardPermission("stringPermission"));
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkPermissions(pCollection, "stringPerm1", "stringPerm2");
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkPermissions(pCollection, permList);
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkRole(pCollection, "role1");
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkRoles(pCollection, roleList);
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
assertFalse(realm.hasAllRoles(pCollection, roleList));
assertFalse(realm.hasRole(pCollection, "role1"));
assertArrayEquals(new boolean[] { false, false }, realm.hasRoles(pCollection, roleList));
assertFalse(realm.isPermitted(pCollection, "perm1"));
assertFalse(realm.isPermitted(pCollection, new WildcardPermission("perm1")));
assertArrayEquals(new boolean[] { false, false }, realm.isPermitted(pCollection, "perm1", "perm2"));
assertArrayEquals(new boolean[] { false, false }, realm.isPermitted(pCollection, permList));
assertFalse(realm.isPermittedAll(pCollection, "perm1", "perm2"));
assertFalse(realm.isPermittedAll(pCollection, permList));
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testCheckRole.
/*
* Tests that roles can't be checked while the realm is being loaded.
*/
@Test
public void testCheckRole() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
try {
realm.checkRoles(principalCollection, new String[] { "role1", "role2" });
} catch (AuthorizationException ae) {
fail("principal doesn't have all roles when it should");
}
}
});
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class DelegatingSubject method login.
public void login(AuthenticationToken token) throws AuthenticationException {
clearRunAsIdentitiesInternal();
Subject subject = securityManager.login(this, token);
PrincipalCollection principals;
String host = null;
if (subject instanceof DelegatingSubject) {
DelegatingSubject delegating = (DelegatingSubject) subject;
// we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
principals = delegating.principals;
host = delegating.host;
} else {
principals = subject.getPrincipals();
}
if (principals == null || principals.isEmpty()) {
String msg = "Principals returned from securityManager.login( token ) returned a null or " + "empty value. This value must be non null and populated with one or more elements.";
throw new IllegalStateException(msg);
}
this.principals = principals;
this.authenticated = true;
if (token instanceof HostAuthenticationToken) {
host = ((HostAuthenticationToken) token).getHost();
}
if (host != null) {
this.host = host;
}
Session session = subject.getSession(false);
if (session != null) {
this.session = decorate(session);
} else {
this.session = null;
}
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class DelegatingSubject method pushIdentity.
private void pushIdentity(PrincipalCollection principals) throws NullPointerException {
if (isEmpty(principals)) {
String msg = "Specified Subject principals cannot be null or empty for 'run as' functionality.";
throw new NullPointerException(msg);
}
List<PrincipalCollection> stack = getRunAsPrincipalsStack();
if (stack == null) {
stack = new CopyOnWriteArrayList<PrincipalCollection>();
}
stack.add(0, principals);
Session session = getSession();
session.setAttribute(RUN_AS_PRINCIPALS_SESSION_KEY, stack);
}
Aggregations