use of io.vertx.ext.auth.authentication.UsernamePasswordCredentials in project vertx-web by vert-x3.
the class WebClientTest method testBasicAuthentication.
@Test
public void testBasicAuthentication() throws Exception {
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("ém$¨=!$€", "&@#§$*éà#\"'");
testRequest(client -> client.get("somehost", "somepath").authentication(creds), req -> {
String auth = req.headers().get(HttpHeaders.AUTHORIZATION);
assertEquals("Was expecting authorization header to contain a basic authentication string", creds.toHttpAuthorization(), auth);
});
}
use of io.vertx.ext.auth.authentication.UsernamePasswordCredentials in project vertx-web by vert-x3.
the class BasicAuthHandlerImpl method authenticate.
@Override
public void authenticate(RoutingContext context, Handler<AsyncResult<User>> handler) {
parseAuthorization(context, parseAuthorization -> {
if (parseAuthorization.failed()) {
handler.handle(Future.failedFuture(parseAuthorization.cause()));
return;
}
final String suser;
final String spass;
try {
// decode the payload
String decoded = new String(base64Decode(parseAuthorization.result()), StandardCharsets.UTF_8);
int colonIdx = decoded.indexOf(":");
if (colonIdx != -1) {
suser = decoded.substring(0, colonIdx);
spass = decoded.substring(colonIdx + 1);
} else {
suser = decoded;
spass = null;
}
} catch (RuntimeException e) {
handler.handle(Future.failedFuture(new HttpException(400, e)));
return;
}
authProvider.authenticate(new UsernamePasswordCredentials(suser, spass), authn -> {
if (authn.failed()) {
handler.handle(Future.failedFuture(new HttpException(401, authn.cause())));
} else {
handler.handle(authn);
}
});
});
}
use of io.vertx.ext.auth.authentication.UsernamePasswordCredentials in project vertx-web by vert-x3.
the class AuthHandlerTestBase method testAuthorization.
protected void testAuthorization(String username, boolean fail, Authorization authority) throws Exception {
if (requiresSession()) {
router.route().handler(BodyHandler.create());
SessionStore store = getSessionStore();
router.route().handler(SessionHandler.create(store));
}
AuthenticationProvider authNProvider = PropertyFileAuthentication.create(vertx, "login/loginusers.properties");
AuthorizationProvider authZProvider = PropertyFileAuthorization.create(vertx, "login/loginusers.properties");
AuthenticationHandler authNHandler = createAuthHandler(authNProvider);
router.route().handler(rc -> {
// we need to be logged in
if (rc.user() == null) {
UsernamePasswordCredentials authInfo = new UsernamePasswordCredentials(username, "delicious:sausages");
authNProvider.authenticate(authInfo, res -> {
if (res.succeeded()) {
rc.setUser(res.result());
rc.next();
} else {
rc.fail(res.cause());
}
});
}
});
router.route().handler(authNHandler);
if (authority != null) {
router.route().handler(AuthorizationHandler.create(authority).addAuthorizationProvider(authZProvider));
}
router.route().handler(rc -> rc.response().end());
testRequest(HttpMethod.GET, "/", fail ? 403 : 200, fail ? "Forbidden" : "OK");
}
use of io.vertx.ext.auth.authentication.UsernamePasswordCredentials in project vertx-web by vert-x3.
the class EventbusBridgeTest method addLoginHandler.
private void addLoginHandler(Router router, AuthenticationProvider authProvider) {
router.route("/eventbus/*").handler(rc -> {
// we need to be logged in
if (rc.user() == null) {
UsernamePasswordCredentials authInfo = new UsernamePasswordCredentials("tim", "delicious:sausages");
HttpServerRequest request = rc.request();
request.pause();
authProvider.authenticate(authInfo, res -> {
request.resume();
if (res.succeeded()) {
rc.setUser(res.result());
rc.next();
} else {
rc.fail(res.cause());
}
});
}
});
}
use of io.vertx.ext.auth.authentication.UsernamePasswordCredentials in project vertx-web by vert-x3.
the class CustomAuthHandlerTest method newAuthHandler.
private AuthenticationHandler newAuthHandler(AuthenticationProvider authProvider, Handler<Throwable> exceptionProcessor) {
return SimpleAuthenticationHandler.create().authenticate(ctx -> {
final Promise<User> promise = Promise.promise();
authProvider.authenticate(new UsernamePasswordCredentials("user", "pass"), authn -> {
if (authn.failed()) {
if (exceptionProcessor != null) {
exceptionProcessor.handle(authn.cause());
}
promise.fail(authn.cause());
} else {
promise.complete(authn.result());
}
});
return promise.future();
});
}
Aggregations