Commit 3ac363bd authored by jan.koester's avatar jan.koester
Browse files

client login finished

parent 7d7caef2
Loading
Loading
Loading
Loading
Loading
+57 −3
Changes for src/api.cpp: 57 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -29,19 +29,73 @@
#include <uuid/uuid.h>

#include <json-c/json.h>
#include <json_object.h>
#include <json-c/json_types.h>

#include "hash.h"
#include "hash/sha512.h"
#include "authdb.h"
#include "backend.h"
#include "api.h"
#include "user.h"

namespace authdb {
    class Api {
    public:

        bool Login(json_object *request,json_object *response){
            json_object *login=json_tokener_parse("login");
        bool clientLogin(json_object *request,json_object *response){
            json_object *cmd=json_tokener_parse("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,"login")==0){
                                std::string name,pw,pwsalt;
                                int ocount = json_object_array_length(cmd);
                                for(int ii =0; ii<ocount; ++ii) {
                                    json_object_object_foreach(val,okey,oval){
                                        if(strcmp(okey,"type")==0){
                                                if( strcmp(json_object_get_string(oval),"client")!=0 )
                                                    continue;
                                        }else if(strcmp(okey,"name")==0){
                                                name=json_object_get_string(oval);
                                        }else if(strcmp(okey,"key")==0){
                                                pw=json_object_get_string(oval);
                                        }
                                    }
                                }

                                if(name.empty() || pw.empty())
                                    return false;

                                sha512 sha;
                                sha.hash(pw,pwsalt);

                                size_t rd=sizeof(authdb::AuthHeader),end=_Backend->end();

                                while(rd<end){
                                        authdb::AuthData::Record cur;
                                        cur.type=EmptyData;

                                        _Backend->setPos(rd);
                                        _Backend->read((unsigned char*)&cur,sizeof(AuthData::Record));
                                        rd=_Backend->getPos()+cur.datasize;

                                        if(cur.type == UserData && strcmp(cur.fieldname,"username")==0){
                                            class User user;
                                            class UserData udat(cur.uuid);
                                            size_t upos = sizeof(AuthData::Record);
                                            user.info(*_Backend,udat,upos);

                                            if(name==udat.getUsername()){
                                                if(pwsalt==udat.getPwHash())
                                                    return true;
                                                return false;
                                            }
                                        }
                                }
                        }
                }
            }
            return false;
        }

@@ -68,7 +122,7 @@ namespace authdb {

            jresponse = json_object_new_array();

            if(!Login(jrequest,jresponse)){
            if(!clientLogin(jrequest,jresponse)){
                const char *ret=json_object_to_json_string(jresponse);
                rep.setState(HTTP401);
                rep.setContentType("text/json");