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

client login

parent 62c71539
Loading
Loading
Loading
Loading
Loading
+55 −0
Changes for client/client.cpp: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -70,6 +70,61 @@ authdb::Client::~Client(){
}

bool authdb::Client::ClientAuth(){
    json_object *jresponse = json_object_new_object();
    json_object *cmd=json_object_new_array();
    json_object *login=json_object_new_object();
    json_object *userauth=json_object_new_object();

    json_object_set_string(userauth,"login");
    json_object_object_add(userauth,"type",json_object_new_string("client"));
    json_object_object_add(userauth,"name",json_object_new_string(_Con._UserName.c_str()));
    json_object_object_add(userauth,"key",json_object_new_string(_Con._UserPassword.c_str()));

    json_object_array_add(cmd,login);
    json_object_object_add(login,"login",userauth);
    json_object_object_add(jresponse,"commands",cmd);

    size_t outsize=0;

    std::vector<char> out;

    const char *tmp=json_object_to_json_string_length(jresponse,0,&outsize);

    std::copy(tmp,tmp+outsize,std::back_inserter(out));

    std::vector<char> resp(_Con._Client->Post(_Con._Url.getPath(),"text/json",out));

    resp.push_back('\0');

    std::cout << resp.size()<< std::endl;

    json_object_put(jresponse);

    json_object *jrequest;
    json_tokener*jtok=json_tokener_new();

    jrequest=json_tokener_parse_ex(jtok,resp.data(),resp.size());

    if(!jrequest){
            throw AuthBackendError(json_tokener_error_desc(json_tokener_get_error(jtok)));
    }

    if(json_object_get_type(jrequest)!=json_type::json_type_array)
                throw(AuthBackendError("Invalid Api Commands"));

    int fcount = json_object_array_length(jrequest);
    for(int i =0; i<fcount; ++i) {
        json_object_object_foreach(json_object_array_get_idx(jrequest,i), key, val) {
                 if(strcmp(key,"clientid")==0){
                         _Con._SessionID=json_object_get_string(val);
                         if(!_Con._SessionID.empty()){
                             json_tokener_free(jtok);
                             return true;
                         }
                 }
        }
    }
    json_tokener_free(jtok);
    return false;
}

+4 −0
Changes for client/examples/CMakeLists.txt: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
add_executable(userlogin userlogin.cpp)

target_link_libraries(userlogin PRIVATE client-static)

add_executable(clientlogin clientlogin.cpp)

target_link_libraries(clientlogin PRIVATE client-static)
+44 −0
Changes for client/examples/clientlogin.cpp: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include "../client.h"

int main(int argc, char *argv[]){
    authdb::ClientConnection con;
    con.setUrl(argv[1]);
    con.setUsername(argv[2]);
    con.setUserPassword(argv[3]);

    authdb::Client clt(con);
    if(clt.ClientAuth()){
        std::cout << "Sucessfully login !" << std::endl;
        return 0;
    }
    std::cout << "Failed login !" << std::endl;
    return -1;
}