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

added logout

parent b097b98a
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
@@ -260,3 +260,58 @@ void authdb::client::Client::SessionInfo(class SessionData& sdat){
    }

}

bool authdb::client::Client::UserLogout(){
    json_object *jresponse = json_object_new_object();
    json_object *cmd=json_object_new_array();
    json_object *obj=json_object_new_object();

    json_object_object_add(obj,"usersession", json_object_new_string(_Con.getSessionID().c_str()));
    json_object_object_add(obj,"logout",json_object_new_string(_Con.getSessionID().c_str()));

    json_object_array_add(cmd,obj);

    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');

    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 authdb::AuthBackendError(json_tokener_error_desc(json_tokener_get_error(jtok)));
    }

    if(json_object_get_type(jrequest)!=json_type::json_type_array)
        throw(authdb::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,"logout")==0){
                         if(strcmp(_Con.getSessionID().c_str(),json_object_get_string(val)) == 0 ){
                             json_object_put(jrequest);
                             json_tokener_free(jtok);
                             return true;
                        }
                 }
        }
    }
    json_object_put(jrequest);
    json_tokener_free(jtok);
    return false;
}
+1 −0
Changes for client/client.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ namespace authdb {

            bool UserAuth();
            bool ClientAuth();
            bool UserLogout();

            void addConnection(const std::string &clientname,const std::string &cltsecret);
            void remConnection(const std::string &clientname);
+23 −0
Changes for src/api.cpp: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -366,6 +366,28 @@ namespace authdb {
            }
        }

        void UserLogout(json_object *request,json_object *response){
            std::lock_guard<std::mutex> lock(AuthClientLock);
            json_object *cmd=json_object_object_get(request,"commands");

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

            int fcount = json_object_array_length(cmd);
            for(int i =0; i<fcount; ++i) {
                json_object_object_foreach(json_object_array_get_idx(cmd,i), key, val) {
                    if(strcmp(key,"logout")==0){
                        uuid tmp;
                        json_object *slogoff=json_object_new_object();
                        _SessionData->getUid(tmp);
                        AuthSession.remSession(tmp);
                        json_object_object_add(slogoff,"logout", json_object_new_string(tmp.c_str()));
                        json_object_array_add(response,slogoff);
                    }
                }
            }
        }

        Api(AuthBackend &admin,class uuid did, libhttppp::HttpRequest* curreq, const int tid, ULONG_PTR args) : _Backend(admin), _Did(did){

            _SessionData=nullptr;
@@ -417,6 +439,7 @@ namespace authdb {

            Connection(jrequest,jresponse);
            SessionInfo(jrequest,jresponse);
            UserLogout(jrequest,jresponse);

            if(! (curcon=checkConnection(jrequest,jresponse)) ){
                if(! (curcon=ClientLogin(jrequest,jresponse)) ){