Search in sources :

Example 1 with RestAuthRequestJson

use of io.kamax.mxisd.backend.rest.RestAuthRequestJson in project mxisd by kamax-io.

the class ExecAuthStore method authenticate.

@Override
public ExecAuthResult authenticate(_MatrixID uId, String password) {
    Objects.requireNonNull(uId);
    Objects.requireNonNull(password);
    log.info("Performing authentication for {}", uId.getId());
    ExecAuthResult result = new ExecAuthResult();
    result.setId(new UserID(UserIdType.Localpart, uId.getLocalPart()));
    Processor<ExecAuthResult> p = new Processor<>(cfg);
    p.addTokenMapper(cfg.getToken().getLocalpart(), uId::getLocalPart);
    p.addTokenMapper(cfg.getToken().getDomain(), uId::getDomain);
    p.addTokenMapper(cfg.getToken().getMxid(), uId::getId);
    p.addTokenMapper(cfg.getToken().getPassword(), () -> password);
    p.addJsonInputTemplate(tokens -> {
        RestAuthRequestJson json = new RestAuthRequestJson();
        json.setLocalpart(tokens.getLocalpart());
        json.setDomain(tokens.getDomain());
        json.setMxid(tokens.getMxid());
        json.setPassword(tokens.getPassword());
        return json;
    });
    p.addInputTemplate(PlainType, tokens -> tokens.getLocalpart() + System.lineSeparator() + tokens.getDomain() + System.lineSeparator() + tokens.getMxid() + System.lineSeparator() + tokens.getPassword() + System.lineSeparator());
    p.withExitHandler(pr -> result.setExitStatus(pr.getExitValue()));
    p.withSuccessHandler(pr -> result.setSuccess(true));
    p.withSuccessDefault(o -> result);
    p.addSuccessMapper(JsonType, output -> {
        JsonObject data = GsonUtil.getObj(GsonUtil.parseObj(output), "auth");
        GsonUtil.findPrimitive(data, "success").map(JsonPrimitive::getAsBoolean).ifPresent(result::setSuccess);
        GsonUtil.findObj(data, "profile").flatMap(profile -> GsonUtil.findString(profile, "display_name")).ifPresent(v -> result.getProfile().setDisplayName(v));
        return result;
    });
    p.addSuccessMapper(PlainType, output -> {
        String[] lines = output.split("\\R");
        if (lines.length > 2) {
            throw new InternalServerError("Exec auth command returned more than 2 lines (" + lines.length + ")");
        }
        result.setSuccess(Optional.ofNullable(StringUtils.isEmpty(lines[0]) ? null : lines[0]).map(v -> StringUtils.equalsAnyIgnoreCase(v, "true", "1")).orElse(result.isSuccess()));
        if (lines.length == 2) {
            Optional.ofNullable(StringUtils.isEmpty(lines[1]) ? null : lines[1]).ifPresent(v -> result.getProfile().setDisplayName(v));
        }
        return result;
    });
    p.withFailureHandler(pr -> result.setSuccess(false));
    p.withFailureDefault(o -> result);
    return p.execute();
}
Also used : JsonObject(com.google.gson.JsonObject) GsonUtil(io.kamax.matrix.json.GsonUtil) Logger(org.slf4j.Logger) AuthenticatorProvider(io.kamax.mxisd.auth.provider.AuthenticatorProvider) RestAuthRequestJson(io.kamax.mxisd.backend.rest.RestAuthRequestJson) LoggerFactory(org.slf4j.LoggerFactory) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) StringUtils(org.apache.commons.lang3.StringUtils) Objects(java.util.Objects) InternalServerError(io.kamax.mxisd.exception.InternalServerError) ExecConfig(io.kamax.mxisd.config.ExecConfig) UserID(io.kamax.mxisd.UserID) Optional(java.util.Optional) UserIdType(io.kamax.mxisd.UserIdType) JsonPrimitive(com.google.gson.JsonPrimitive) RestAuthRequestJson(io.kamax.mxisd.backend.rest.RestAuthRequestJson) UserID(io.kamax.mxisd.UserID) JsonObject(com.google.gson.JsonObject) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Aggregations

JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)1 GsonUtil (io.kamax.matrix.json.GsonUtil)1 UserID (io.kamax.mxisd.UserID)1 UserIdType (io.kamax.mxisd.UserIdType)1 AuthenticatorProvider (io.kamax.mxisd.auth.provider.AuthenticatorProvider)1 RestAuthRequestJson (io.kamax.mxisd.backend.rest.RestAuthRequestJson)1 ExecConfig (io.kamax.mxisd.config.ExecConfig)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1