Search in sources :

Example 1 with NamePasswordCallbackHandler

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

the class PropertiesLoginModuleTest method testCannotLoginAsGroupDirectly.

private void testCannotLoginAsGroupDirectly(final String name) throws IOException, LoginException {
    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");
        pbe.addGroup("pqr", "group1");
        pbe.addGroupRole("group1", "r1");
        PropertiesLoginModule module = new PropertiesLoginModule();
        Map<String, String> options = new HashMap<>();
        options.put(PropertiesLoginModule.USER_FILE, f.getAbsolutePath());
        module.initialize(new Subject(), new NamePasswordCallbackHandler(name, "group"), null, options);
        try {
            module.login();
            Assert.fail("The login should have failed as you cannot log in under a group name directly");
        } catch (FailedLoginException fle) {
        // good
        }
    } finally {
        if (!f.delete()) {
            Assert.fail("Could not delete temporary file: " + f);
        }
    }
}
Also used : FailedLoginException(javax.security.auth.login.FailedLoginException) HashMap(java.util.HashMap) NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File) Subject(javax.security.auth.Subject)

Example 2 with NamePasswordCallbackHandler

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

the class JdbcLoginModuleTest method testLoginModule.

@Test
public void testLoginModule() throws Exception {
    JDBCBackingEngine engine = new JDBCBackingEngine(dataSource);
    engine.addUser("abc", "xyz");
    engine.addRole("abc", "role1");
    JDBCLoginModule module = new JDBCLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("abc", "xyz"), null, options);
    module.login();
    module.commit();
    assertFalse(subject.getPrincipals(UserPrincipal.class).isEmpty());
    assertEquals("abc", subject.getPrincipals(UserPrincipal.class).iterator().next().getName());
    assertFalse(subject.getPrincipals(RolePrincipal.class).isEmpty());
    assertEquals("role1", subject.getPrincipals(RolePrincipal.class).iterator().next().getName());
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 3 with NamePasswordCallbackHandler

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

the class LdapLoginModuleTest method testEmptyPassword.

@Test
public void testEmptyPassword() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPLoginModule module = new LDAPLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("imnothere", ""), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    try {
        module.login();
        fail("Should have failed");
    } catch (LoginException e) {
        assertTrue(e.getMessage().equals("Empty passwords not allowed"));
    }
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) LoginException(javax.security.auth.login.LoginException) Properties(org.apache.felix.utils.properties.Properties) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 4 with NamePasswordCallbackHandler

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

the class Krb5LoginModuleTest method testLoginUsernameFailure.

@Test(expected = LoginException.class)
public void testLoginUsernameFailure() throws Exception {
    Subject subject = new Subject();
    Krb5LoginModule module = new Krb5LoginModule();
    module.initialize(subject, new NamePasswordCallbackHandler("hnelson0", "secret"), null, new HashMap<>());
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    Assert.assertFalse(module.login());
}
Also used : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 5 with NamePasswordCallbackHandler

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

the class Krb5LoginModuleTest method testLoginSuccess.

@Test
public void testLoginSuccess() throws Exception {
    Subject subject = new Subject();
    Krb5LoginModule module = new Krb5LoginModule();
    module.initialize(subject, new NamePasswordCallbackHandler("hnelson", "secret"), null, new HashMap<>());
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    Assert.assertTrue(module.login());
    Assert.assertTrue(module.commit());
    assertEquals(1, subject.getPrincipals().size());
    assertThat(names(subject.getPrincipals(KerberosPrincipal.class)), containsInAnyOrder("hnelson@EXAMPLE.COM"));
    KerberosTicket ticket = subject.getPrivateCredentials(KerberosTicket.class).iterator().next();
    assertEquals("hnelson@EXAMPLE.COM", ticket.getClient().getName());
    assertEquals("krbtgt/EXAMPLE.COM@EXAMPLE.COM", ticket.getServer().getName());
    Assert.assertTrue(module.logout());
}
Also used : KerberosTicket(javax.security.auth.kerberos.KerberosTicket) NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Subject(javax.security.auth.Subject) 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 KarafKerberosITest (org.apache.karaf.jaas.modules.krb5.KarafKerberosITest)5 File (java.io.File)4 HashMap (java.util.HashMap)4 KerberosTicket (javax.security.auth.kerberos.KerberosTicket)2 FailedLoginException (javax.security.auth.login.FailedLoginException)2 LoginException (javax.security.auth.login.LoginException)2 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)2 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)2 Principal (java.security.Principal)1 DirContext (javax.naming.directory.DirContext)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)1 GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)1 Level (org.apache.log4j.Level)1