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

org.webbitserver.handler.authentication.InMemoryPasswords Maven / Gradle / Ivy

package org.webbitserver.handler.authentication;

import org.webbitserver.HttpRequest;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

/**
 * Implementation of PasswordAuthenticator that verifies usernames and password from a prepopulated hashmap.
 */
public class InMemoryPasswords implements PasswordAuthenticator {

    private final Map usernameToPasswords = new HashMap();

    public InMemoryPasswords add(String username, String password) {
        usernameToPasswords.put(username, password);
        return this;
    }

    @Override
    public void authenticate(HttpRequest request, String username, String password, ResultCallback callback, Executor handlerExecutor) {
        String expectedPassword = usernameToPasswords.get(username);
        if (expectedPassword != null && password.equals(expectedPassword)) {
            callback.success();
        } else {
            callback.failure();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy