Search in sources :

Example 16 with NamePasswordCallbackHandler

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

the class JdbcLoginModuleTest method testLoginModuleWithGroups.

@Test
public void testLoginModuleWithGroups() throws Exception {
    JDBCBackingEngine engine = new JDBCBackingEngine(dataSource);
    engine.addGroupRole("group1", "role2");
    engine.addUser("abc", "xyz");
    engine.addRole("abc", "role1");
    engine.addGroup("abc", "group1");
    JDBCLoginModule module = new JDBCLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("abc", "xyz"), null, options);
    module.login();
    module.commit();
    assertTrue(subject.getPrincipals().contains(new UserPrincipal("abc")));
    assertTrue(subject.getPrincipals().contains(new GroupPrincipal("group1")));
    assertTrue(subject.getPrincipals().contains(new RolePrincipal("role1")));
    assertTrue(subject.getPrincipals().contains(new RolePrincipal("role2")));
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Subject(javax.security.auth.Subject) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) Test(org.junit.Test)

Example 17 with NamePasswordCallbackHandler

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

the class PropertiesLoginModuleTest method testBasicLogin.

@Test
public void testBasicLogin() throws Exception {
    File f = File.createTempFile(getClass().getName(), ".tmp");
    try {
        Properties p = new Properties(f);
        PropertiesBackingEngine pbe = new PropertiesBackingEngine(p);
        pbe.addUser("abc", "xyz");
        pbe.addRole("abc", "myrole");
        pbe.addUser("pqr", "abc");
        PropertiesLoginModule module = new PropertiesLoginModule();
        Map<String, String> options = new HashMap<>();
        options.put(PropertiesLoginModule.USER_FILE, f.getAbsolutePath());
        Subject subject = new Subject();
        module.initialize(subject, new NamePasswordCallbackHandler("abc", "xyz"), null, options);
        Assert.assertEquals("Precondition", 0, subject.getPrincipals().size());
        Assert.assertTrue(module.login());
        Assert.assertTrue(module.commit());
        Assert.assertEquals(2, subject.getPrincipals().size());
        boolean foundUser = false;
        boolean foundRole = false;
        for (Principal pr : subject.getPrincipals()) {
            if (pr instanceof UserPrincipal) {
                Assert.assertEquals("abc", pr.getName());
                foundUser = true;
            } else if (pr instanceof RolePrincipal) {
                Assert.assertEquals("myrole", pr.getName());
                foundRole = true;
            }
        }
        Assert.assertTrue(foundUser);
        Assert.assertTrue(foundRole);
        Assert.assertTrue(module.logout());
        Assert.assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
    } finally {
        if (!f.delete()) {
            Assert.fail("Could not delete temporary file: " + f);
        }
    }
}
Also used : HashMap(java.util.HashMap) Properties(org.apache.felix.utils.properties.Properties) Subject(javax.security.auth.Subject) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) File(java.io.File) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) Test(org.junit.Test)

Example 18 with NamePasswordCallbackHandler

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

the class LdapLoginModuleTest method testUserNotFound.

@Test
public void testUserNotFound() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPLoginModule module = new LDAPLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("imnothere", "admin123"), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertFalse(module.login());
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Properties(org.apache.felix.utils.properties.Properties) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 19 with NamePasswordCallbackHandler

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

the class LdapLoginModuleTest method testRoleMappingSimple.

@Test
public void testRoleMappingSimple() throws Exception {
    Properties options = ldapLoginModuleOptions();
    options.put(LDAPOptions.ROLE_MAPPING, "admin=karaf");
    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(2, subject.getPrincipals().size());
    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) {
            assertEquals("karaf", principal.getName());
            foundRole = true;
        }
    }
    assertTrue(foundUser);
    assertTrue(foundRole);
    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) 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)

Example 20 with NamePasswordCallbackHandler

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

the class GSSAPILdapLoginModuleTest method testPasswordFailure.

@Test(expected = LoginException.class)
public void testPasswordFailure() throws Exception {
    Properties options = ldapLoginModuleOptions();
    GSSAPILdapLoginModule module = new GSSAPILdapLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("hnelson", "secret0"), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Properties(org.apache.felix.utils.properties.Properties) Subject(javax.security.auth.Subject) AbstractKerberosITest(org.apache.directory.server.kerberos.kdc.AbstractKerberosITest) 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