Commit dd29c202 authored by jan.koester's avatar jan.koester
Browse files

testing

parent 7000b0dd
Loading
Loading
Loading
Loading
+46 −0
Changes for src/auth.cpp: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -121,6 +121,52 @@ void blogi::Auth::login(const int tid,const char* username, const char* password
    }
}

void blogi::Auth::Apilogin(const int tid,const char *username,const char *password,uuid::uuid &authid){
    libhttppp::HTTPException httpException;

    authdb::client::ClientConnection authcon;
    authcon.setUrl(_config.getAuthUrl());
    authcon.setClientName(_config.getClientName());
    authcon.setClientSecret(_config.getClientSecret());

    authdb::client::Client clt(authcon);

    if(!clt.ClientAuth()){
        httpException[libhttppp::HTTPException::Error] << "ClientAuth failed!";
        throw httpException;
    }

    authcon.setUsername(username);
    authcon.setUserPassword(password);

    bool ret = clt.UserAuth();
    if(!ret) {
        httpException[libhttppp::HTTPException::Error] << "UserAuth failed (Invalid credentials)!";
        throw httpException;
    }

    try {
        ret = clt.GPOcheck("e7d3b8b3-4825-11f0-ae2b-3cecefce9cb6");

        if(ret){
            authid = authcon.getSessionID();
        } else {
            // GPO check failed, clean up the session
            clt.remSession();
            httpException[libhttppp::HTTPException::Error] << "User GPO check failed!";
            throw httpException;
        }
    } catch (authdb::AuthBackendError &e) {
        try {
            clt.remSession();
        } catch (authdb::AuthBackendError &cleanupE) {
        }
        httpException[libhttppp::HTTPException::Error] << "Authentication backend error: " << e.what();
        throw httpException;
    }
}


bool blogi::Auth::isLoggedIn(const int tid,libhttppp::HttpRequest &curreq,const std::string &ssid){
        try{
            libhttppp::HttpCookie cook;
+1 −0
Changes for src/auth.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ namespace blogi {
        Auth(std::vector<std::unique_ptr<dbpp::Database>> &pcon,const blogi::Config &cfg);
        ~Auth();
        void login(const int tid,const char *username,const char *password,uuid::uuid &authid,const std::string &ssid);
        void Apilogin(const int tid,const char *username,const char *password,uuid::uuid &authid);
        bool isLoggedIn(const int tid,libhttppp::HttpRequest &curreq,const std::string &ssid);
    private:
        std::vector<std::unique_ptr<dbpp::Database>> &_dbconn;
+142 −0
Changes for src/blogi.cpp: 142 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -176,6 +176,145 @@ blogi::Blogi::Blogi(netplus::socket &serversocket,bool debug) : HttpEvent(server
blogi::Blogi::~Blogi(){
}

void blogi::Blogi::api(libhttppp::HttpRequest& curreq, const int tid){
    uuid::uuid aid;
    size_t clen = curreq.getContentLength();
    libhttppp::HttpResponse resp;

    // 1. Basic Content Length Check
    if(clen > curreq.RecvData.size() || clen == 0){
        resp.setState(HTTP400); // Bad Request
        resp.setContentType("text/plain");
        const char* err = "Api: Incomplete or empty request.";
        resp.send(curreq, err, strlen(err));
        return;
    }

    std::vector<char> content;
    // Copy only the content length number of bytes
    std::copy(curreq.RecvData.begin(), curreq.RecvData.begin() + clen, std::back_inserter(content));
    content.emplace_back('\0');

    json_tokener_error jerr;
    json_object *jrequest = nullptr, *jresponse = nullptr;

    // 2. Parse JSON Request
    if (!(jrequest = json_tokener_parse_verbose(content.data(), &jerr))) {
        resp.setState(HTTP400); // Bad Request
        resp.setContentType("text/plain");
        const char* err = json_tokener_error_desc(jerr);
        resp.send(curreq, err, strlen(err));
        return;
    }

    // Request must be a JSON array of commands
    if (!json_object_is_type(jrequest, json_type_array)) {
        json_object_put(jrequest);
        resp.setState(HTTP400); // Bad Request
        resp.setContentType("text/plain");
        const char* err = "Api: Request must be a JSON array.";
        resp.send(curreq, err, strlen(err));
        return;
    }

    jresponse = json_object_new_array();
    size_t fcount = json_object_array_length(jrequest);

    // 3. Process each command in the request array
    for (size_t i = 0; i < fcount; ++i) {
        json_object* api_call = json_object_array_get_idx(jrequest, i);

        if (!json_object_is_type(api_call, json_type_object)) {
            // Skip non-object array elements
            continue;
        }

        json_object_object_foreach(api_call, key, val) {
            // key = command (e.g., "login", "auth")
            // val = parameters object

            json_object* respid = json_object_new_object(); // Response for this command

            if (strcmp(key, "login") == 0) {
                // --- LOGIN COMMAND (username/password) ---
                const char* username = nullptr;
                const char* password = nullptr;

                json_object* juser = nullptr;
                json_object* jpass = nullptr;

                if (json_object_object_get_ex(val, "username", &juser) &&
                    json_object_object_get_ex(val, "password", &jpass))
                {
                    username = json_object_get_string(juser);
                    password = json_object_get_string(jpass);

                    if (username && password) {
                        try {
                            // Call the authentication service
                            PlgArgs->auth->Apilogin(tid, username, password, aid);

                            // Success response: returns the new authid
                            json_object* jsuccess = json_object_new_object();
                            json_object_object_add(jsuccess, "authid", json_object_new_string(aid.c_str()));
                            json_object_object_add(respid, "login_success", jsuccess);

                        } catch (libhttppp::HTTPException &e) {
                            // Failure response (e.g., invalid credentials, GPO check failed)
                            json_object* jfail = json_object_new_object();
                            json_object_object_add(jfail, "error", json_object_new_string(e.what().c_str()));
                            json_object_object_add(respid, "login_failure", jfail);
                        }
                    } else {
                        // Missing/invalid username/password strings
                        json_object* jfail = json_object_new_object();
                        json_object_object_add(jfail, "error", json_object_new_string("Missing username or password value."));
                        json_object_object_add(respid, "login_failure", jfail);
                    }
                } else {
                    // Missing keys in parameter object
                    json_object* jfail = json_object_new_object();
                    json_object_object_add(jfail, "error", json_object_new_string("Missing 'username' or 'password' fields."));
                    json_object_object_add(respid, "login_failure", jfail);
                }
            }
            else if (strcmp(key, "auth") == 0) {
                // --- AUTH CHECK COMMAND (Placeholder for existing authid status check) ---
                // This block is based on the original snippet's intent to check an existing auth ID.
                // In a full implementation, you would check if the session is still valid.

                json_object* value_obj  = json_object_new_object();
                // Assuming `val` contains the auth ID string or an object with the ID.
                const char* id = json_object_get_string(val); // Simplified extraction

                // Placeholder: Assume success for GPO status check
                json_object_object_add(value_obj, "authid", json_object_new_string(id));
                json_object_object_add(value_obj, "gpo_status", json_object_new_string("active"));

                json_object_object_add(respid, "gpo", value_obj);
            }

            // Add the response for this specific command to the response array
            json_object_array_add(jresponse, respid);

            // Only process one command per array element
            break;
        }
    }

    // 4. Send JSON Response
    size_t outsize = 0;
    const char* out = json_object_to_json_string_length(jresponse, 0, &outsize);

    resp.setContentType("application/json"); // Correct Content-Type for API
    resp.setState(HTTP200);
    resp.send(curreq, out, outsize);

    // 5. Cleanup
    json_object_put(jresponse);
    json_object_put(jrequest);
}

void blogi::Blogi::loginPage(libhttppp::HttpRequest &curreq,const int tid,const std::string &sessionid){
    libhttppp::HTTPException excep;
    if(PlgArgs->auth->isLoggedIn(tid,curreq,sessionid)){
@@ -578,6 +717,9 @@ RETRY_REQUEST:
                curres.setContentType("text/html");
                curres.send(curreq,"", 0);
                return;
            }else if(curreq.getRequestURL().compare(0,PlgArgs->config->buildurl("api").length(),PlgArgs->config->buildurl("api"))==0){
                api(curreq,tid);
                return;
            }else if(curreq.getRequestURL().compare(0,PlgArgs->config->buildurl("logout").length(),PlgArgs->config->buildurl("logout"))==0){
                logoutPage(curreq,tid,sessid);
                return;
+1 −0
Changes for src/blogi.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ namespace blogi {
    public:
        Blogi(netplus::socket &serversocket,bool debug);
        ~Blogi();
        void api(libhttppp::HttpRequest &curreq,const int tid);
        void loginPage(libhttppp::HttpRequest &curreq,const int tid,const std::string &sessionid);
        void logoutPage(libhttppp::HttpRequest &curreq,const int tid,const std::string &sessionid);
        void settingsPage(libhttppp::HttpRequest &curreq,const int tid,const std::string &sessionid);
+1 −1
Changes for src/plugin.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ void blogi::PluginApi::GetSiteMap(const int tid,std::map<libhttppp::HttpUrl,std:
    return;
}

void blogi::PluginApi::JsonApi(json_object* request, json_object* response,const uuid::uuid &authid){
void blogi::PluginApi::JsonApi(const int tid,json_object* request, json_object* response,const uuid::uuid &authid){
    return;
}

Loading