Search in sources :

Example 26 with NamePasswordCallbackHandler

use of org.apache.karaf.jaas.modules.NamePasswordCallbackHandler in project karaf by apache.

the class LdapLoginModuleTest method testRoleMappingAdvanced.

@Test
public void testRoleMappingAdvanced() throws Exception {
    Properties options = ldapLoginModuleOptions();
    options.put(LDAPOptions.ROLE_MAPPING, "admin=karaf,test;admin=another");
    LDAPLoginModule module = new LDAPLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("admin", "admin123"), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
    assertTrue(module.commit());
    assertEquals(4, subject.getPrincipals().size());
    final List<String> roles = new ArrayList<>(Arrays.asList("karaf", "test", "another"));
    boolean foundUser = false;
    boolean foundRole = false;
    for (Principal principal : subject.getPrincipals()) {
        if (principal instanceof UserPrincipal) {
            assertEquals("admin", principal.getName());
            foundUser = true;
        } else if (principal instanceof RolePrincipal) {
            assertTrue(roles.remove(principal.getName()));
            foundRole = true;
        }
    }
    assertTrue(foundUser);
    assertTrue(foundRole);
    assertTrue(roles.isEmpty());
    assertTrue(module.logout());
    assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) ArrayList(java.util.ArrayList) Properties(org.apache.felix.utils.properties.Properties) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Subject(javax.security.auth.Subject) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Principal(java.security.Principal) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) Test(org.junit.Test)

Aggregations

Subject (javax.security.auth.Subject)26 NamePasswordCallbackHandler (org.apache.karaf.jaas.modules.NamePasswordCallbackHandler)26 Test (org.junit.Test)25 Properties (org.apache.felix.utils.properties.Properties)21 Principal (java.security.Principal)13 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)13 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)13 AbstractKerberosITest (org.apache.directory.server.kerberos.kdc.AbstractKerberosITest)8 File (java.io.File)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)3 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)2 KerberosTicket (javax.security.auth.kerberos.KerberosTicket)2 FailedLoginException (javax.security.auth.login.FailedLoginException)2 LoginException (javax.security.auth.login.LoginException)2 Attribute (javax.naming.directory.Attribute)1 Attributes (javax.naming.directory.Attributes)1 BasicAttribute (javax.naming.directory.BasicAttribute)1 BasicAttributes (javax.naming.directory.BasicAttributes)1