org.webbitserver.handler.authentication.InMemoryPasswords Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webbit Show documentation
Show all versions of webbit Show documentation
A Java event based WebSocket and HTTP server
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();
}
}
}