Search in sources :

Example 26 with AccessMgr

use of org.apache.directory.fortress.core.AccessMgr 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 27 with AccessMgr

use of org.apache.directory.fortress.core.AccessMgr 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 28 with AccessMgr

use of org.apache.directory.fortress.core.AccessMgr 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)

Example 29 with AccessMgr

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

the class AccessMgrImplTest method createSessionsWithRoles.

/**
 * @param msg
 * @param uArray
 * @param rArray
 */
public static void createSessionsWithRoles(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);
            List<UserRole> rlsRequested = new ArrayList<>();
            int cnt = 0;
            for (String[] rle : rArray) {
                rlsRequested.add(RoleTestData.getUserRole(user.getUserId(), rle));
                user.setRoles(rlsRequested);
                Session session = accessMgr.createSession(user, false);
                assertTrue(CLS_NM + ".createSessionsWithRoles failed role search USER [" + user.getUserId() + "] CNT [" + ++cnt + "] size [" + session.getRoles().size() + "]", cnt == session.getRoles().size());
                String userId = accessMgr.getUserId(session);
                assertTrue(CLS_NM + ".createSessionsWithRoles failed compare found userId [" + userId + "] valid userId [" + UserTestData.getUserId(usr) + "]", userId.equalsIgnoreCase(UserTestData.getUserId(usr)));
                UserTestData.assertEquals(user, usr);
            }
        }
        LOG.debug("createSessionsWithRoles successful");
    } catch (SecurityException ex) {
        LOG.error("createSessionsWithRoles: 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) ArrayList(java.util.ArrayList) SecurityException(org.apache.directory.fortress.core.SecurityException) Session(org.apache.directory.fortress.core.model.Session)

Example 30 with AccessMgr

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

the class AccessMgrImplTest method sessionPermissionsH.

/**
 * @param msg
 * @param uArray
 * @param oArray
 * @param opArray
 */
public static void sessionPermissionsH(String msg, String[][] uArray, String[][] oArray, String[][] opArray) {
    LogUtil.logIt(msg);
    try {
        AccessMgr accessMgr = AccessMgrFactory.createInstance(TestUtils.getContext());
        int i = 0;
        for (String[] usr : uArray) {
            i++;
            User user = UserTestData.getUser(usr);
            Session session = accessMgr.createSession(user, false);
            assertNotNull(session);
            List<Permission> pOps = accessMgr.sessionPermissions(session);
            assertNotNull(pOps);
            // LOG.warn("sessionPermissionsH list size user [" + user.getUserId() + "] expected len=" +
            // (11 - i) * opArray.length + " actual len=" + pOps.size());
            assertEquals(CLS_NM + ".sessionPermissionsH failed list size user[" + user.getUserId() + "]", (11 - i) * opArray.length, pOps.size());
            // Iterate over objs x ops, see if every expected valid permission is contained within the returned list:
            int j = 0;
            for (String[] obj : oArray) {
                j++;
                // positive tests:
                if (i == j || i < j) {
                    int k = 0;
                    for (String[] op : opArray) {
                        k++;
                        Permission validPOp = PermTestData.getOp(PermTestData.getName(obj), op);
                        assertTrue(CLS_NM + ".sessionPermissionsH failed perm list compare USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", pOps.contains(validPOp));
                        boolean result = accessMgr.checkAccess(session, new Permission(PermTestData.getName(obj), PermTestData.getName(op)));
                        assertTrue(CLS_NM + ".sessionPermissionsH failed checkAccess USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", result);
                    }
                } else // negative tests:
                {
                    int k = 0;
                    for (String[] op : opArray) {
                        k++;
                        Permission validPOp = PermTestData.getOp(PermTestData.getName(obj), op);
                        assertTrue(CLS_NM + ".sessionPermissionsH failed negative perm list compare USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", !pOps.contains(validPOp));
                        boolean result = accessMgr.checkAccess(session, new Permission(PermTestData.getName(obj), PermTestData.getName(op)));
                        assertTrue(CLS_NM + ".sessionPermissionsH failed negative checkAccess USER [" + user.getUserId() + "] PERM Obj [" + PermTestData.getName(obj) + "] " + "OPER [" + PermTestData.getName(op) + "]", !result);
                    }
                }
            }
        }
    } catch (SecurityException ex) {
        LOG.error("sessionPermissionsH: 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)

Aggregations

AccessMgr (org.apache.directory.fortress.core.AccessMgr)41 SecurityException (org.apache.directory.fortress.core.SecurityException)41 User (org.apache.directory.fortress.core.model.User)37 Session (org.apache.directory.fortress.core.model.Session)32 UserRole (org.apache.directory.fortress.core.model.UserRole)17 Permission (org.apache.directory.fortress.core.model.Permission)7 AdminMgr (org.apache.directory.fortress.core.AdminMgr)6 PwPolicyMgr (org.apache.directory.fortress.core.PwPolicyMgr)6 DelAccessMgr (org.apache.directory.fortress.core.DelAccessMgr)4 ArrayList (java.util.ArrayList)3 UserAdminRole (org.apache.directory.fortress.core.model.UserAdminRole)3 AdminRole (org.apache.directory.fortress.core.model.AdminRole)2 Role (org.apache.directory.fortress.core.model.Role)2 SDSet (org.apache.directory.fortress.core.model.SDSet)2 CSVWriter (au.com.bytecode.opencsv.CSVWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Enumeration (java.util.Enumeration)1