Search in sources :

Example 46 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method checkAccess.

public static void checkAccess(String msg, String[][] uArray, String[][] oArray, String[][] opArray, String[][] oArrayBad, String[][] opArrayBad) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            int i = 0;
            for (String[] obj : oArray) {
                int j = 0;
                for (String[] op : opArray) {
                    Permission goodPerm;
                    if (StringUtils.isNotEmpty(PermTestData.getObjId(opArray[j]))) {
                        // with an objectId:
                        goodPerm = new Permission(PermTestData.getName(obj), PermTestData.getName(op), PermTestData.getObjId(opArray[j]));
                    } else {
                        // without an objectId:
                        goodPerm = new Permission(PermTestData.getName(obj), PermTestData.getName(op));
                    }
                    // Positive test case, call checkAccess method, should return 'true':
                    assertTrue(CLS_NM + ".checkAccess failed userId [" + user.getUserId() + "] Perm objName [" + PermTestData.getName(obj) + "] operationName [" + PermTestData.getName(op) + "]", accessMgr.checkAccess(session, goodPerm));
                    Permission badPerm;
                    if (StringUtils.isNotEmpty(PermTestData.getObjId(opArrayBad[j]))) {
                        // with an objectId:
                        badPerm = new Permission(PermTestData.getName(oArrayBad[i]), PermTestData.getName(opArrayBad[j]), PermTestData.getObjId(opArrayBad[j]));
                    } else {
                        // without an objectId:
                        badPerm = new Permission(PermTestData.getName(oArrayBad[i]), PermTestData.getName(opArrayBad[j]));
                    }
                    // LOG.warn("Assert False userId [" + user.getUserId() + "], perm: " + badPerm);
                    // Negative test case, call checkAccess method again, should return 'false':
                    assertFalse(CLS_NM + ".checkAccess failed userId [" + user.getUserId() + "] Perm objName [" + PermTestData.getName(oArrayBad[i]) + "] operationName [" + PermTestData.getName(opArrayBad[j]) + "]", accessMgr.checkAccess(session, badPerm));
                    j++;
                }
                i++;
            }
        }
        LOG.debug("checkAccess successful");
    } catch (SecurityException ex) {
        LOG.error("checkAccess: failed with SecurityException rc=" + ex.getErrorId() + ", " + "msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) Permission(org.apache.directory.fortress.core.model.Permission) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 47 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method getUsers.

/**
 * @param msg
 * @param uArray
 */
public static void getUsers(String msg, String[][] uArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            user = accessMgr.getUser(session);
            UserTestData.assertEquals(user, usr);
        }
        LOG.debug("getUsers successful");
    } catch (SecurityException ex) {
        LOG.error("getUsers: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 48 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method addActiveRolesDSD.

public static void addActiveRolesDSD(String msg, String[][] uArray, String[][] sArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        int i = 0;
        for (String[] usr : uArray) {
            SDSet dsd = RoleTestData.getSDSet(sArray[i++]);
            // Set<String> roles = dsd.getMembers().keySet();
            Set<String> roles = dsd.getMembers();
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.authenticate(user.getUserId(), user.getPassword());
            int j = 0;
            for (String role : roles) {
                j++;
                try {
                    assertNotNull(session);
                    // Activate Role(s):
                    accessMgr.addActiveRole(session, new UserRole(role));
                    if (j >= dsd.getCardinality()) {
                        fail(CLS_NM + ".addActiveRolesDSD user [" + user.getUserId() + "] role [" + role + "] ssd [" + dsd.getName() + "] cardinality [" + dsd.getCardinality() + "] count [" + j + "] failed");
                    }
                } catch (SecurityException ex) {
                    assertTrue(CLS_NM + ".addActiveRolesDSD cardinality test failed user [" + user.getUserId() + "] role [" + role + "] ssd [" + dsd.getName() + "] cardinality [" + dsd.getCardinality() + "] count [" + j + "]", j >= (dsd.getCardinality()));
                    assertTrue(CLS_NM + ".addActiveRolesDSD cardinality test failed [" + UserTestData.getUserId(usr) + "]", ex.getErrorId() == GlobalErrIds.DSD_VALIDATION_FAILED);
                    // still good, break from loop, we're done here
                    break;
                }
            }
        }
    } catch (SecurityException ex) {
        LOG.error("addActiveRolesDSD caught SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : SDSet(org.apache.directory.fortress.core.model.SDSet) User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) UserRole(org.apache.directory.fortress.core.model.UserRole) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 49 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method sessionRoles.

/**
 * @param msg
 * @param uArray
 * @param rArray
 */
public static void sessionRoles(String msg, String[][] uArray, String[][] rArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            String userId = accessMgr.getUserId(session);
            assertTrue(CLS_NM + ".sessionRoles failed compare found userId [" + userId + "] valid userId [" + UserTestData.getUserId(usr) + "]", userId.equalsIgnoreCase(UserTestData.getUserId(usr)));
            UserTestData.assertEquals(user, usr);
            List<UserRole> uRoles = accessMgr.sessionRoles(session);
            assertNotNull(uRoles);
            assertEquals(CLS_NM + ".sessionRoles user role check failed list size user [" + user.getUserId() + "]", rArray.length, uRoles.size());
            for (String[] rle : rArray) {
                assertTrue(CLS_NM + ".sessionRoles failed role search USER [" + user.getUserId() + "] ROLE1 [" + RoleTestData.getName(rle) + "] should be present", uRoles.contains(RoleTestData.getUserRole(UserTestData.getUserId(usr), rle)));
            }
        }
        LOG.debug("sessionRoles successful");
    } catch (SecurityException ex) {
        LOG.error("sessionRoles: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) UserRole(org.apache.directory.fortress.core.model.UserRole) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 50 with Session

use of org.apache.directory.fortress.core.model.Session in project directory-fortress-core by apache.

the class AccessMgrImplTest method authenticateUsers.

/**
 * @param msg
 * @param uArray
 * @param multiplier
 */
private static void authenticateUsers(String msg, String[][] uArray, int multiplier) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        for (String[] usr : uArray) {
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.authenticate(user.getUserId(), user.getPassword());
            assertNotNull(session);
            // now try negative test case:
            try {
                accessMgr.authenticate(user.getUserId(), "wrongpw");
                fail("authenticateUsers failed negative test");
            } catch (SecurityException se) {
                assertTrue("authenticateUsers reset excep id check", se.getErrorId() == GlobalErrIds.USER_PW_INVLD);
            // pass
            }
        }
        LOG.debug("authenticateUsers successful");
    } catch (SecurityException ex) {
        LOG.error("authenticateUsers: failed with SecurityException rc=" + ex.getErrorId() + ", msg=" + ex.getMessage(), ex);
        fail(ex.getMessage());
    }
}
Also used : User(org.apache.directory.fortress.core.model.User) AccessMgr(org.apache.directory.fortress.core.AccessMgr) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Aggregations

Session (org.apache.directory.fortress.core.model.Session)70 SecurityException (org.apache.directory.fortress.core.SecurityException)62 User (org.apache.directory.fortress.core.model.User)51 AccessMgr (org.apache.directory.fortress.core.AccessMgr)32 UserRole (org.apache.directory.fortress.core.model.UserRole)28 AccelMgr (org.apache.directory.fortress.core.AccelMgr)12 Permission (org.apache.directory.fortress.core.model.Permission)12 FortRequest (org.apache.directory.fortress.core.model.FortRequest)10 FortResponse (org.apache.directory.fortress.core.model.FortResponse)10 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 DelAccessMgr (org.apache.directory.fortress.core.DelAccessMgr)4 UserAdminRole (org.apache.directory.fortress.core.model.UserAdminRole)4 PasswordException (org.apache.directory.fortress.core.PasswordException)3 lombok.val (lombok.val)2 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 org.apache.directory.fortress.core (org.apache.directory.fortress.core)2 AdminRole (org.apache.directory.fortress.core.model.AdminRole)2 ObjectFactory (org.apache.directory.fortress.core.model.ObjectFactory)2 Role (org.apache.directory.fortress.core.model.Role)2