Search in sources :

Example 1 with UsernamePasswordCredentials

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);
    });
}
Also used : UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials) Test(org.junit.Test)

Example 2 with UsernamePasswordCredentials

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);
            }
        });
    });
}
Also used : HttpException(io.vertx.ext.web.handler.HttpException) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials)

Example 3 with UsernamePasswordCredentials

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");
}
Also used : LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) SessionStore(io.vertx.ext.web.sstore.SessionStore) AuthenticationProvider(io.vertx.ext.auth.authentication.AuthenticationProvider) AuthorizationProvider(io.vertx.ext.auth.authorization.AuthorizationProvider) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials)

Example 4 with UsernamePasswordCredentials

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());
                }
            });
        }
    });
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials)

Example 5 with UsernamePasswordCredentials

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();
    });
}
Also used : User(io.vertx.ext.auth.User) UsernamePasswordCredentials(io.vertx.ext.auth.authentication.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (io.vertx.ext.auth.authentication.UsernamePasswordCredentials)6 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 HttpException (io.vertx.ext.web.handler.HttpException)2 MultiMap (io.vertx.core.MultiMap)1 User (io.vertx.ext.auth.User)1 AuthenticationProvider (io.vertx.ext.auth.authentication.AuthenticationProvider)1 AuthorizationProvider (io.vertx.ext.auth.authorization.AuthorizationProvider)1 LocalSessionStore (io.vertx.ext.web.sstore.LocalSessionStore)1 SessionStore (io.vertx.ext.web.sstore.SessionStore)1 Test (org.junit.Test)1