All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.scattersphere.registry.AuthRegistry Maven / Gradle / Ivy

The newest version!
package net.scattersphere.registry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * This class serves as an authentication storage class.
 *
 * Created by kenji on 3/13/15.
 */
public class AuthRegistry {

    private static final AuthRegistry instance = new AuthRegistry();

    private final Map registry = new HashMap<>();
    private final Logger LOG = LoggerFactory.getLogger(AuthRegistry.class);

    /**
     * Retrieves the singleton instance.
     *
     * @return {@code AuthRegistry} object.
     */
    public static AuthRegistry instance() {
        return instance;
    }

    /**
     * Loads in the user registry from a specified {@link Properties} class.
     *
     * @param props {@link Properties} class to load data from.
     */
    public void loadFromProperties(Properties props) {
        Enumeration propKeys = props.keys();

        while(propKeys.hasMoreElements()) {
            String key = (String) propKeys.nextElement();
            String pass = props.getProperty(key);

            registry.put(key, pass);
        }
    }

    /**
     * Performs simple authentication.
     *
     * @param user The username to check.
     * @param pass The password to check.
     * @return {@code true} if matched, {@code false} otherwise.
     */
    public boolean authenticate(String user, String pass) {
        return pass.equals(registry.get(user));
    }

    /**
     * Returns the number of users registered.
     *
     * @return The number of users registered in the registry.
     */
    public int size() {
        return registry.size();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy