Search in sources :

Example 66 with Properties

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

the class LdapLoginModuleTest method testNonAdminLogin.

@Test
public void testNonAdminLogin() throws Exception {
    Properties options = ldapLoginModuleOptions();
    LDAPLoginModule module = new LDAPLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("cheese", "foodie"), 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 : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Properties(org.apache.felix.utils.properties.Properties) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 67 with Properties

use of org.apache.felix.utils.properties.Properties 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());
    assertThat(names(subject.getPrincipals(UserPrincipal.class)), containsInAnyOrder("admin"));
    assertThat(names(subject.getPrincipals(RolePrincipal.class)), containsInAnyOrder("karaf"));
    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) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 68 with Properties

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

the class LdapLoginModuleTest method testRoleMappingParsing.

@Test
public void testRoleMappingParsing() 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());
    assertThat(names(subject.getPrincipals(UserPrincipal.class)), containsInAnyOrder("admin"));
    assertThat(names(subject.getPrincipals(RolePrincipal.class)), containsInAnyOrder("karaf", "test", "another"));
    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) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 69 with Properties

use of org.apache.felix.utils.properties.Properties 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());
    assertThat(names(subject.getPrincipals(UserPrincipal.class)), containsInAnyOrder("admin"));
    assertThat(names(subject.getPrincipals(RolePrincipal.class)), containsInAnyOrder("karaf", "test", "another"));
    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) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 70 with Properties

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

the class LdapLoginModuleTest method testTrimmedUsernameLogin.

@Test
public void testTrimmedUsernameLogin() throws Exception {
    Properties options = ldapLoginModuleOptions();
    options.put("usernames.trim", "true");
    LDAPLoginModule module = new LDAPLoginModule();
    Subject subject = new Subject();
    module.initialize(subject, new NamePasswordCallbackHandler("cheese   ", "foodie"), 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 : NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Properties(org.apache.felix.utils.properties.Properties) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Aggregations

Properties (org.apache.felix.utils.properties.Properties)95 IOException (java.io.IOException)35 File (java.io.File)33 Test (org.junit.Test)27 Subject (javax.security.auth.Subject)25 NamePasswordCallbackHandler (org.apache.karaf.jaas.modules.NamePasswordCallbackHandler)21 Path (java.nio.file.Path)13 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 FileInputStream (java.io.FileInputStream)8 URL (java.net.URL)8 MalformedURLException (java.net.MalformedURLException)7 HashSet (java.util.HashSet)6 Hashtable (java.util.Hashtable)6 LinkedHashMap (java.util.LinkedHashMap)6 TreeMap (java.util.TreeMap)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 LoginException (javax.security.auth.login.LoginException)5