Search in sources :

Example 1 with USERNAME_PARAM_KEY

use of io.gravitee.am.common.utils.ConstantKeys.USERNAME_PARAM_KEY in project gravitee-access-management by gravitee-io.

the class LoginFormHandler method handle.

@Override
public void handle(RoutingContext context) {
    HttpServerRequest req = context.request();
    if (req.method() != HttpMethod.POST) {
        // Must be a POST
        context.fail(405);
    } else {
        if (!req.isExpectMultipart()) {
            throw new IllegalStateException("Form body not parsed - do you forget to include a BodyHandler?");
        }
        MultiMap params = req.formAttributes();
        String username = params.get(USERNAME_PARAM_KEY);
        String password = params.get(PASSWORD_PARAM_KEY);
        String clientId = params.get(Parameters.CLIENT_ID);
        if (username == null || password == null) {
            logger.warn("No username or password provided in form - did you forget to include a BodyHandler?");
            context.fail(400);
        } else if (clientId == null) {
            logger.warn("No client id in form - did you forget to include client_id query parameter ?");
            context.fail(400);
        } else {
            // build authentication object with ip address and user agent
            JsonObject authInfo = new JsonObject().put(USERNAME_PARAM_KEY, username).put(PASSWORD_PARAM_KEY, password).put(Claims.ip_address, RequestUtils.remoteAddress(req)).put(Claims.user_agent, RequestUtils.userAgent(req)).put(Parameters.CLIENT_ID, clientId);
            authProvider.authenticate(context, authInfo, res -> {
                if (res.failed()) {
                    logger.debug("An error has occurred during the authentication process", res.cause());
                    context.fail(res.cause());
                    return;
                }
                // authentication success
                // set user into the context and continue
                final User result = res.result();
                context.getDelegate().setUser(result);
                context.put(ConstantKeys.USER_CONTEXT_KEY, result.getUser());
                context.next();
            });
        }
    }
}
Also used : RequestUtils(io.gravitee.am.gateway.handler.common.vertx.utils.RequestUtils) Logger(org.slf4j.Logger) HttpServerRequest(io.vertx.reactivex.core.http.HttpServerRequest) LoggerFactory(org.slf4j.LoggerFactory) ConstantKeys(io.gravitee.am.common.utils.ConstantKeys) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) MultiMap(io.vertx.reactivex.core.MultiMap) UserAuthProvider(io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.UserAuthProvider) PASSWORD_PARAM_KEY(io.gravitee.am.common.utils.ConstantKeys.PASSWORD_PARAM_KEY) HttpMethod(io.vertx.core.http.HttpMethod) Claims(io.gravitee.am.common.jwt.Claims) User(io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Parameters(io.gravitee.am.common.oauth2.Parameters) USERNAME_PARAM_KEY(io.gravitee.am.common.utils.ConstantKeys.USERNAME_PARAM_KEY) MultiMap(io.vertx.reactivex.core.MultiMap) User(io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) HttpServerRequest(io.vertx.reactivex.core.http.HttpServerRequest) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Claims (io.gravitee.am.common.jwt.Claims)1 Parameters (io.gravitee.am.common.oauth2.Parameters)1 ConstantKeys (io.gravitee.am.common.utils.ConstantKeys)1 PASSWORD_PARAM_KEY (io.gravitee.am.common.utils.ConstantKeys.PASSWORD_PARAM_KEY)1 USERNAME_PARAM_KEY (io.gravitee.am.common.utils.ConstantKeys.USERNAME_PARAM_KEY)1 RequestUtils (io.gravitee.am.gateway.handler.common.vertx.utils.RequestUtils)1 UserAuthProvider (io.gravitee.am.gateway.handler.common.vertx.web.auth.provider.UserAuthProvider)1 User (io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User)1 Handler (io.vertx.core.Handler)1 HttpMethod (io.vertx.core.http.HttpMethod)1 JsonObject (io.vertx.core.json.JsonObject)1 MultiMap (io.vertx.reactivex.core.MultiMap)1 HttpServerRequest (io.vertx.reactivex.core.http.HttpServerRequest)1 RoutingContext (io.vertx.reactivex.ext.web.RoutingContext)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1