Search in sources :

Example 11 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project ANNIS by korpling.

the class ANNISUserRealm method clearCacheForUser.

public void clearCacheForUser(String userName) {
    SimplePrincipalCollection principals = new SimplePrincipalCollection(userName, ANNISUserRealm.class.getName());
    clearCache(principals);
}
Also used : SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection)

Example 12 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.

the class SimpleAuthenticationInfo method merge.

/**
 * Takes the specified <code>info</code> argument and adds its principals and credentials into this instance.
 *
 * @param info the <code>AuthenticationInfo</code> to add into this instance.
 */
@SuppressWarnings("unchecked")
public void merge(AuthenticationInfo info) {
    if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
        return;
    }
    if (this.principals == null) {
        this.principals = info.getPrincipals();
    } else {
        if (!(this.principals instanceof MutablePrincipalCollection)) {
            this.principals = new SimplePrincipalCollection(this.principals);
        }
        ((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
    }
    // since 1.1:
    if (this.credentialsSalt == null && info instanceof SaltedAuthenticationInfo) {
        this.credentialsSalt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();
    }
    Object thisCredentials = getCredentials();
    Object otherCredentials = info.getCredentials();
    if (otherCredentials == null) {
        return;
    }
    if (thisCredentials == null) {
        this.credentials = otherCredentials;
        return;
    }
    if (!(thisCredentials instanceof Collection)) {
        Set newSet = new HashSet();
        newSet.add(thisCredentials);
        setCredentials(newSet);
    }
    // At this point, the credentials should be a collection
    Collection credentialCollection = (Collection) getCredentials();
    if (otherCredentials instanceof Collection) {
        credentialCollection.addAll((Collection) otherCredentials);
    } else {
        credentialCollection.add(otherCredentials);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) MutablePrincipalCollection(org.apache.shiro.subject.MutablePrincipalCollection) Collection(java.util.Collection) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) MutablePrincipalCollection(org.apache.shiro.subject.MutablePrincipalCollection) HashSet(java.util.HashSet)

Example 13 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection 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"));
}
Also used : RolePermissionResolver(org.apache.shiro.authz.permission.RolePermissionResolver) Permission(org.apache.shiro.authz.Permission) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Principal(java.security.Principal) Test(org.junit.Test)

Example 14 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection 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));
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Permission(org.apache.shiro.authz.Permission) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Principal(java.security.Principal) Test(org.junit.Test)

Example 15 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection 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");
            }
        }
    });
}
Also used : AuthorizationException(org.apache.shiro.authz.AuthorizationException) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Test(org.junit.Test)

Aggregations

SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)55 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)26 Test (org.junit.Test)25 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)11 ArrayList (java.util.ArrayList)7 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)7 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)7 Realm (org.apache.shiro.realm.Realm)7 Principal (java.security.Principal)6 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)6 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)6 Subject (org.apache.shiro.subject.Subject)6 Subject (ddf.security.Subject)5 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)5 SimpleAccount (org.apache.shiro.authc.SimpleAccount)5 AuthorizingRealm (org.apache.shiro.realm.AuthorizingRealm)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SecurityAssertion (ddf.security.assertion.SecurityAssertion)4 HashSet (java.util.HashSet)4 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)4