Search in sources :

Example 1 with NamePubkeyCallbackHandler

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

the class LDAPPubkeyLoginModuleTest method testBadPrivateKey.

@Test
public void testBadPrivateKey() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPPubkeyLoginModule module = new LDAPPubkeyLoginModule();
    Subject subject = new Subject();
    Path pubkeyFile = srcTestResourcePath("org/apache/karaf/jaas/modules/ldap/ldaptest.cheese.id_rsa");
    module.initialize(subject, new NamePubkeyCallbackHandler("admin", pubkeyFile), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(LDAPLoginModule.class);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.OFF);
    try {
        module.login();
        fail("Should have thrown LoginException");
    } catch (LoginException e) {
        assertTrue(e.getMessage().startsWith("Authentication failed"));
    } finally {
        logger.setLevel(oldLevel);
    }
}
Also used : Path(java.nio.file.Path) LoginException(javax.security.auth.login.LoginException) Level(org.apache.log4j.Level) Properties(org.apache.felix.utils.properties.Properties) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 2 with NamePubkeyCallbackHandler

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

the class LDAPPubkeyLoginModuleTest method testUserNotFound.

@Test
public void testUserNotFound() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPPubkeyLoginModule module = new LDAPPubkeyLoginModule();
    Subject subject = new Subject();
    Path pubkeyFile = srcTestResourcePath("org/apache/karaf/jaas/modules/ldap/ldaptest.admin.id_rsa");
    module.initialize(subject, new NamePubkeyCallbackHandler("imnothere", pubkeyFile), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertFalse(module.login());
}
Also used : Path(java.nio.file.Path) Properties(org.apache.felix.utils.properties.Properties) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 3 with NamePubkeyCallbackHandler

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

the class LDAPPubkeyLoginModuleTest method testAdminLogin.

@Test
public void testAdminLogin() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPPubkeyLoginModule module = new LDAPPubkeyLoginModule();
    Subject subject = new Subject();
    Path pubkeyFile = srcTestResourcePath("org/apache/karaf/jaas/modules/ldap/ldaptest.admin.id_rsa");
    module.initialize(subject, new NamePubkeyCallbackHandler("admin", pubkeyFile), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
    assertTrue(module.commit());
    assertEquals(2, subject.getPrincipals().size());
    assertThat(names(subject.getPrincipals(UserPrincipal.class)), containsInAnyOrder("admin"));
    assertThat(names(subject.getPrincipals(RolePrincipal.class)), containsInAnyOrder("admin"));
    assertTrue(module.logout());
    assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
Also used : Path(java.nio.file.Path) Properties(org.apache.felix.utils.properties.Properties) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 4 with NamePubkeyCallbackHandler

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

the class LDAPPubkeyLoginModuleTest method testNonAdminLogin.

@Test
public void testNonAdminLogin() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPPubkeyLoginModule module = new LDAPPubkeyLoginModule();
    Subject subject = new Subject();
    Path pubkeyFile = srcTestResourcePath("org/apache/karaf/jaas/modules/ldap/ldaptest.cheese.id_rsa");
    module.initialize(subject, new NamePubkeyCallbackHandler("cheese", pubkeyFile), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
    assertTrue(module.commit());
    assertEquals(1, subject.getPrincipals().size());
    assertThat(names(subject.getPrincipals(UserPrincipal.class)), containsInAnyOrder("cheese"));
    assertThat(names(subject.getPrincipals(RolePrincipal.class)), Matchers.empty());
    assertTrue(module.logout());
    assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
Also used : Path(java.nio.file.Path) Properties(org.apache.felix.utils.properties.Properties) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Aggregations

Path (java.nio.file.Path)4 Subject (javax.security.auth.Subject)4 Properties (org.apache.felix.utils.properties.Properties)4 NamePubkeyCallbackHandler (org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler)4 Test (org.junit.Test)4 LoginException (javax.security.auth.login.LoginException)1 Level (org.apache.log4j.Level)1