Search in sources :

Example 36 with Permission

use of org.apache.shiro.authz.Permission in project ddf by codice.

the class AuthzRealmTest method testIsPermittedOneMultiple.

@Test
public void testIsPermittedOneMultiple() throws PdpException {
    permissionList.clear();
    KeyValuePermission kvp = new KeyValuePermissionImpl("country", Arrays.asList("AUS", "CAN", "GBR"));
    permissionList.add(kvp);
    String ruleClaim = "FineAccessControls";
    String countryClaim = "CountryOfAffiliation";
    // create a new user here with multiple country permissions to test
    List<Permission> permissions = new ArrayList<Permission>();
    KeyValuePermission rulePermission = new KeyValuePermissionImpl(ruleClaim);
    rulePermission.addValue("A");
    rulePermission.addValue("B");
    permissions.add(rulePermission);
    KeyValuePermission countryPermission = new KeyValuePermissionImpl(countryClaim);
    countryPermission.addValue("USA");
    countryPermission.addValue("AUS");
    permissions.add(countryPermission);
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.addObjectPermission(rulePermission);
    authorizationInfo.addObjectPermission(countryPermission);
    authorizationInfo.addRole("admin");
    AuthzRealm testRealm = new AuthzRealm("src/test/resources/policies", new XmlParser()) {

        @Override
        public AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
            return authorizationInfo;
        }
    };
    testRealm.setSecurityLogger(mock(SecurityLogger.class));
    testRealm.setMatchOneMappings(Arrays.asList("CountryOfAffiliation=country"));
    testRealm.setMatchAllMappings(Arrays.asList("FineAccessControls=rule"));
    testRealm.setRolePermissionResolver(roleString -> Arrays.asList(new KeyValuePermissionImpl("role", Arrays.asList(roleString))));
    boolean[] permittedArray = testRealm.isPermitted(mockSubjectPrincipal, permissionList);
    for (boolean permitted : permittedArray) {
        Assert.assertEquals(true, permitted);
    }
}
Also used : XmlParser(org.codice.ddf.parser.xml.XmlParser) AuthzRealm(ddf.security.pdp.realm.AuthzRealm) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) KeyValuePermissionImpl(ddf.security.permission.impl.KeyValuePermissionImpl) CollectionPermission(ddf.security.permission.CollectionPermission) KeyValuePermission(ddf.security.permission.KeyValuePermission) Permission(org.apache.shiro.authz.Permission) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) KeyValuePermission(ddf.security.permission.KeyValuePermission) SecurityLogger(ddf.security.audit.SecurityLogger) Test(org.junit.Test)

Example 37 with Permission

use of org.apache.shiro.authz.Permission in project airpal by airbnb.

the class ExampleLDAPRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    Set<String> roles = Sets.newHashSet("user");
    Set<Permission> permissions = Sets.newHashSet();
    Collection<AllowAllUser> principalsCollection = principals.byType(AllowAllUser.class);
    if (principalsCollection.isEmpty()) {
        throw new AuthorizationException("No principals!");
    }
    for (AllowAllUser user : principalsCollection) {
        for (UserGroup userGroup : groups) {
            if (userGroup.representedByGroupStrings(user.getGroups())) {
                permissions.addAll(userGroup.getPermissions());
                break;
            }
        }
    }
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(roles);
    authorizationInfo.setObjectPermissions(permissions);
    return authorizationInfo;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) AuthorizationException(org.apache.shiro.authz.AuthorizationException) Permission(org.apache.shiro.authz.Permission)

Example 38 with Permission

use of org.apache.shiro.authz.Permission in project ddf by codice.

the class OperationPluginTest method makeDecision.

private Answer<Boolean> makeDecision() {
    Map<String, List<String>> testRoleMap = new HashMap<String, List<String>>();
    List<String> testRoles = new ArrayList<String>();
    testRoles.add("A");
    testRoles.add("B");
    testRoleMap.put("Roles", testRoles);
    final KeyValueCollectionPermission testUserPermission = new PermissionsImpl().buildKeyValueCollectionPermission(CollectionPermission.READ_ACTION, testRoleMap);
    return new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            Permission incomingPermission = (Permission) args[1];
            return testUserPermission.implies(incomingPermission);
        }
    };
}
Also used : KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) PermissionsImpl(ddf.security.permission.impl.PermissionsImpl) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CollectionPermission(ddf.security.permission.CollectionPermission) Permission(org.apache.shiro.authz.Permission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) ArrayList(java.util.ArrayList) List(java.util.List)

Example 39 with Permission

use of org.apache.shiro.authz.Permission in project ddf by codice.

the class MatchOneCollectionPermission method implies.

/**
 * Overrides the implies method to handle checking for the existence of one attribute - the "match
 * one" scenario rather than the "match all" behavior of the overridden classes. Specifically,
 * this permission will imply another permission if that permission matches at least one of our
 * permission attributes.
 *
 * @param p the permission to check for behavior/functionality comparison.
 * @return {@code true} if this current instance <em>implies</em> the specified {@code Permission}
 *     argument, {@code false} otherwise.
 */
@Override
public boolean implies(Permission p) {
    if (permissionList.isEmpty()) {
        return false;
    }
    if (p instanceof CollectionPermission) {
        for (Permission perm : ((CollectionPermission) p).getPermissionList()) {
            boolean result = false;
            for (Permission ourPerm : permissionList) {
                // the permission.
                if (ourPerm instanceof KeyValuePermission) {
                    for (String value : ((KeyValuePermission) ourPerm).getValues()) {
                        // Since this is "match one" we know that only one of these values needs
                        // to match in order
                        // for the entire permission at that key to be implied
                        // So here we loop through all of the values assigned to that key and
                        // create new
                        // single valued key value permissions
                        KeyValuePermission kvp = new KeyValuePermissionImpl(((KeyValuePermission) ourPerm).getKey());
                        kvp.addValue(value);
                        if (perm.implies(kvp)) {
                            result = true;
                            break;
                        }
                    }
                // Currently we use key value permissions for everything. However, we still need
                // to be able to handle
                // permissions other than KV, so this else block will serve as the catch all for
                // everything else.
                } else {
                    // the implies to make it match one
                    if (perm.implies(ourPerm)) {
                        result = true;
                        break;
                    }
                }
            }
            if (!result) {
                return false;
            }
        }
        return true;
    }
    // default catch all permission check
    for (Permission permission : permissionList) {
        // to make it match one
        if (p.implies(permission)) {
            return true;
        }
    }
    return false;
}
Also used : CollectionPermission(ddf.security.permission.CollectionPermission) KeyValuePermission(ddf.security.permission.KeyValuePermission) Permission(org.apache.shiro.authz.Permission) CollectionPermission(ddf.security.permission.CollectionPermission) KeyValuePermission(ddf.security.permission.KeyValuePermission)

Example 40 with Permission

use of org.apache.shiro.authz.Permission in project ddf by codice.

the class AuthzRealm method isPermitted.

/**
 * Checks if the corresponding Subject/user implies the given Permissions and returns a boolean
 * array indicating which permissions are implied.
 *
 * <p>
 *
 * <p>More specifically, this method should determine if each <tt>Permission</tt> in the array is
 * {@link Permission#implies(Permission) implied} by permissions already associated with the
 * subject.
 *
 * <p>
 *
 * <p>This is primarily a performance-enhancing method to help reduce the number of {@link
 * #isPermitted} invocations over the wire in client/server systems.
 *
 * @param subjectPrincipal the application-specific subject/user identifier.
 * @param permissions the permissions that are being checked.
 * @return an array of booleans whose indices correspond to the index of the permissions in the
 *     given list. A true value at an index indicates the user is permitted for for the associated
 *     <tt>Permission</tt> object in the list. A false value at an index indicates otherwise.
 */
@Override
public boolean[] isPermitted(PrincipalCollection subjectPrincipal, List<Permission> permissions) {
    boolean[] results = new boolean[permissions.size()];
    AuthorizationInfo authorizationInfo = getAuthorizationInfo(subjectPrincipal);
    List<Permission> expandedPermissions = expandPermissions(permissions);
    int i = 0;
    for (Permission permission : expandedPermissions) {
        results[i++] = isPermitted(subjectPrincipal, permission, authorizationInfo);
    }
    return results;
}
Also used : CollectionPermission(ddf.security.permission.CollectionPermission) KeyValuePermission(ddf.security.permission.KeyValuePermission) Permission(org.apache.shiro.authz.Permission) MatchOneCollectionPermission(ddf.security.permission.impl.MatchOneCollectionPermission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo)

Aggregations

Permission (org.apache.shiro.authz.Permission)42 CollectionPermission (ddf.security.permission.CollectionPermission)22 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)21 KeyValuePermission (ddf.security.permission.KeyValuePermission)20 Test (org.junit.Test)15 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)8 WildcardPermission (org.apache.shiro.authz.permission.WildcardPermission)8 ArrayList (java.util.ArrayList)7 KeyValuePermissionImpl (ddf.security.permission.impl.KeyValuePermissionImpl)6 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 KeyValueCollectionPermissionImpl (ddf.security.permission.impl.KeyValueCollectionPermissionImpl)4 MatchOneCollectionPermission (ddf.security.permission.impl.MatchOneCollectionPermission)4 AuthorizationException (org.apache.shiro.authz.AuthorizationException)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 RolePermissionResolver (org.apache.shiro.authz.permission.RolePermissionResolver)3 GRN (org.graylog.grn.GRN)3 CaseSensitiveWildcardPermission (org.graylog.security.permissions.CaseSensitiveWildcardPermission)3