Search in sources :

Example 26 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class LdapLoginModuleTest method testAdminLogin.

@Test
public void testAdminLogin() throws Exception {
    Properties options = ldapLoginModuleOptions();
    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 pr : subject.getPrincipals()) {
        if (pr instanceof UserPrincipal) {
            assertEquals("admin", pr.getName());
            foundUser = true;
        } else if (pr instanceof RolePrincipal) {
            assertEquals("admin", pr.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 27 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class GSSAPILdapLoginModuleTest method testNoRealm.

@Test(expected = LoginException.class)
public void testNoRealm() throws Exception {
    Properties options = ldapLoginModuleOptions();
    options.remove(GSSAPILdapLoginModule.REALM_PROPERTY);
    GSSAPILdapLoginModule module = new GSSAPILdapLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("hnelson0", "secret"), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    // should throw LoginException
    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)

Example 28 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class GSSAPILdapLoginModuleTest method testUsernameFailure.

@Test(expected = LoginException.class)
public void testUsernameFailure() throws Exception {
    Properties options = ldapLoginModuleOptions();
    GSSAPILdapLoginModule module = new GSSAPILdapLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("hnelson0", "secret"), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    // should throw LoginException
    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)

Example 29 with Properties

use of org.apache.felix.utils.properties.Properties in project karaf by apache.

the class GSSAPILdapLoginModuleTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    Properties options = ldapLoginModuleOptions();
    GSSAPILdapLoginModule module = new GSSAPILdapLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("hnelson", "secret"), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
    assertTrue(module.commit());
    assertEquals(3, subject.getPrincipals().size());
    boolean foundKrb5User = false;
    boolean foundUser = false;
    boolean foundRole = false;
    boolean foundTicket = false;
    for (Principal pr : subject.getPrincipals()) {
        if (pr instanceof KerberosPrincipal) {
            assertEquals("hnelson@EXAMPLE.COM", pr.getName());
            foundKrb5User = true;
        } else if (pr instanceof UserPrincipal) {
            assertEquals("hnelson", pr.getName());
            foundUser = true;
        } else if (pr instanceof RolePrincipal) {
            assertEquals("admin", pr.getName());
            foundRole = true;
        }
    }
    for (Object crd : subject.getPrivateCredentials()) {
        if (crd instanceof KerberosTicket) {
            assertEquals("hnelson@EXAMPLE.COM", ((KerberosTicket) crd).getClient().getName());
            assertEquals("krbtgt/EXAMPLE.COM@EXAMPLE.COM", ((KerberosTicket) crd).getServer().getName());
            foundTicket = true;
            break;
        }
    }
    assertTrue("Principals should contains kerberos user", foundKrb5User);
    assertTrue("Principals should contains ldap user", foundUser);
    assertTrue("Principals should contains ldap role", foundRole);
    assertTrue("PricatePrincipals should contains kerberos ticket", foundTicket);
    assertTrue(module.logout());
    assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) 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) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) AbstractKerberosITest(org.apache.directory.server.kerberos.kdc.AbstractKerberosITest) Test(org.junit.Test)

Example 30 with Properties

use of org.apache.felix.utils.properties.Properties 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)

Aggregations

Properties (org.apache.felix.utils.properties.Properties)64 File (java.io.File)26 Test (org.junit.Test)22 IOException (java.io.IOException)21 Subject (javax.security.auth.Subject)21 NamePasswordCallbackHandler (org.apache.karaf.jaas.modules.NamePasswordCallbackHandler)21 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)16 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)16 Principal (java.security.Principal)12 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)6 Map (java.util.Map)5 AbstractKerberosITest (org.apache.directory.server.kerberos.kdc.AbstractKerberosITest)5 FileInputStream (java.io.FileInputStream)4 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 LinkedHashMap (java.util.LinkedHashMap)4 TreeMap (java.util.TreeMap)4 FailedLoginException (javax.security.auth.login.FailedLoginException)4