Search in sources :

Example 71 with Properties

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

the class LdapLoginModuleTest method testRoleMappingFqdn.

@Test
public void testRoleMappingFqdn() throws Exception {
    Properties options = ldapLoginModuleOptions();
    options.put(LDAPOptions.ROLE_MAPPING, "cn=admin,ou=groups,dc=example,dc=com=karaf;cn=admin,ou=mygroups,dc=example,dc=com=another");
    options.put(LDAPOptions.ROLE_BASE_DN, "ou=groups,dc=example,dc=com");
    options.put(LDAPOptions.ROLE_SEARCH_SUBTREE, "true");
    options.put(LDAPOptions.ROLE_FILTER, "(member=%fqdn)");
    options.put(LDAPOptions.ROLE_NAME_ATTRIBUTE, "description");
    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 72 with Properties

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

the class PublickeyBackingEngineFactory method build.

public BackingEngine build(Map<String, ?> options) {
    PublickeyBackingEngine engine = null;
    String usersFile = (String) options.get(USER_FILE);
    File f = new File(usersFile);
    Properties users;
    try {
        users = new Properties(f);
        return new PublickeyBackingEngine(users);
    } catch (IOException ioe) {
        logger.warn("Cannot open keys file:" + usersFile);
    }
    return engine;
}
Also used : IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File)

Example 73 with Properties

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

the class PublickeyLoginModule method login.

public boolean login() throws LoginException {
    File f = new File(usersFile);
    Properties users;
    try {
        users = new Properties(f);
    } catch (IOException ioe) {
        throw new LoginException("Unable to load user properties file " + f);
    }
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PublickeyCallback();
    try {
        callbackHandler.handle(callbacks);
    } catch (IOException ioe) {
        throw new LoginException(ioe.getMessage());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException(uce.getMessage() + " not available to obtain information from user");
    }
    String user = ((NameCallback) callbacks[0]).getName();
    if (user == null) {
        throw new FailedLoginException("Unable to retrieve user name");
    }
    PublicKey key = ((PublickeyCallback) callbacks[1]).getPublicKey();
    if (key == null) {
        throw new FailedLoginException("Unable to retrieve public key");
    }
    // user infos container read from the users properties file
    String userInfos = null;
    try {
        userInfos = users.get(user);
    } catch (NullPointerException e) {
    // error handled in the next statement
    }
    if (userInfos == null) {
        if (!this.detailedLoginExcepion) {
            throw new FailedLoginException("login failed");
        } else {
            throw new FailedLoginException("User " + user + " does not exist");
        }
    }
    // the password is in the first position
    String[] infos = userInfos.split(",");
    String storedKey = infos[0];
    // check the provided password
    if (!getString(key).equals(storedKey)) {
        if (!this.detailedLoginExcepion) {
            throw new FailedLoginException("login failed");
        } else {
            throw new FailedLoginException("Public key for " + user + " does not match");
        }
    }
    principals = new HashSet<>();
    principals.add(new UserPrincipal(user));
    for (int i = 1; i < infos.length; i++) {
        if (infos[i].trim().startsWith(PropertiesBackingEngine.GROUP_PREFIX)) {
            // it's a group reference
            principals.add(new GroupPrincipal(infos[i].trim().substring(PropertiesBackingEngine.GROUP_PREFIX.length())));
            String groupInfo = users.get(infos[i].trim());
            if (groupInfo != null) {
                String[] roles = groupInfo.split(",");
                for (int j = 1; j < roles.length; j++) {
                    principals.add(new RolePrincipal(roles[j].trim()));
                }
            }
        } else {
            // it's an user reference
            principals.add(new RolePrincipal(infos[i].trim()));
        }
    }
    users.clear();
    if (debug) {
        LOG.debug("Successfully logged in " + user);
    }
    return true;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) File(java.io.File)

Example 74 with Properties

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

Example 75 with Properties

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

the class PropertiesLoader method loadConfigProperties.

/**
 * <p>
 * Loads the configuration properties in the configuration property file
 * associated with the framework installation; these properties
 * are accessible to the framework and to bundles and are intended
 * for configuration purposes. By default, the configuration property
 * file is located in the <tt>conf/</tt> directory of the Felix
 * installation directory and is called "<tt>config.properties</tt>".
 * The installation directory of Felix is assumed to be the parent
 * directory of the <tt>felix.jar</tt> file as found on the system class
 * path property. The precise file from which to load configuration
 * properties can be set by initializing the "<code>felix.config.properties</code>"
 * system property to an arbitrary URL.
 * </p>
 *
 * @param file the config file where to load the properties.
 * @return A <code>Properties</code> instance or <code>null</code> if there was an error.
 * @throws Exception if something wrong occurs.
 */
public static Properties loadConfigProperties(File file) throws Exception {
    // See if the property URL was specified as a property.
    URL configPropURL;
    try {
        configPropURL = file.toURI().toURL();
    } catch (MalformedURLException ex) {
        System.err.print("Main: " + ex);
        return null;
    }
    Properties configProps = loadPropertiesFile(configPropURL, false);
    copySystemProperties(configProps);
    configProps.substitute();
    return configProps;
}
Also used : MalformedURLException(java.net.MalformedURLException) Properties(org.apache.felix.utils.properties.Properties) URL(java.net.URL)

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