Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2011-2014 the original author or authors.
*/
package com.jetdrone.vertx.yoke.middleware;
import com.jetdrone.vertx.yoke.Middleware;
import org.jetbrains.annotations.NotNull;
import org.vertx.java.core.Handler;
import org.vertx.java.core.json.JsonObject;
import javax.xml.bind.DatatypeConverter;
/**
* # BasicAuth
*
* Enfore basic authentication by providing a AuthHandler.handler(user, pass), which must return true in order to gain
* access. Populates request.user. The final alternative is simply passing username / password strings.
*/
public class BasicAuth extends Middleware {
/**
* Realm name for the application
*/
private final String realm;
/**
* AuthHandler for validating this instance authentication requests.
*/
private final AuthHandler authHandler;
/**
* Creates a new BasicAuth middleware with a master username / password and a given realm.
*
* Yoke yoke = new Yoke(...);
* yoke.use("/admin", new BasicAuth("admin", "s3cr37",
* "MyApp Auth Required"));
*
*
* @param username the security principal user name
* @param password the security principal password
* @param realm the security realm
*/
public BasicAuth(@NotNull final String username, @NotNull final String password, @NotNull String realm) {
this.realm = realm;
authHandler = new AuthHandler() {
@Override
public void handle(String _username, String _password, Handler result) {
boolean success = username.equals(_username) && password.equals(_password);
if (success) {
result.handle(new JsonObject().putString("username", _username));
} else {
result.handle(null);
}
}
};
}
/**
* Creates a new BasicAuth middleware with a master username / password. By default the realm will be `Authentication required`.
*
*
* Yoke yoke = new Yoke(...);
* yoke.use("/admin", new BasicAuth("admin", "s3cr37"));
*
*
* @param username the security principal user name
* @param password the security principal password
*/
public BasicAuth(@NotNull String username, @NotNull String password) {
this (username, password, "Authentication required");
}
/**
* Creates a new BasicAuth middleware with a AuthHandler and a given realm.
*
*
* Yoke yoke = new Yoke(...);
* yoke.use("/admin", new AuthHandler() {
* public void handle(String user, String password, Handler next) {
* // a better example would be fetching user from a DB
* if ("user".equals(user) && "pass".equals(password)) {
* next.handle(true);
* } else {
* next.handle(false);
* }
* }
* }, "My App Auth");
*
*
* @param authHandler the authentication handler
* @param realm the security realm
*/
public BasicAuth(@NotNull String realm, @NotNull AuthHandler authHandler) {
this.realm = realm;
this.authHandler = authHandler;
}
/**
* Creates a new BasicAuth middleware with a AuthHandler.
*
*
* Yoke yoke = new Yoke(...);
* yoke.use("/admin", new AuthHandler() {
* public void handle(String user, String password, Handler next) {
* // a better example would be fetching user from a DB
* if ("user".equals(user) && "pass".equals(password)) {
* next.handle(true);
* } else {
* next.handle(false);
* }
* }
* });
*
* @param authHandler the authentication handler
*/
public BasicAuth(@NotNull AuthHandler authHandler) {
this("Authentication required", authHandler);
}
/**
* Handle all forbidden errors, in this case we need to add a special header to the response
*
* @param request yoke request
* @param next middleware to be called next
*/
private void handle401(final YokeRequest request, final Handler