Commit 89b0a8bf authored by jan.koester's avatar jan.koester
Browse files

basic work login

parent 548fbcd3
Loading
Loading
Loading
Loading
Loading
+28 −1
Changes for data/login.html: 28 added lines, 1 removed line.
Original line number Diff line number Diff line
<!DOCTYPE html>
<html lang="en">
    <head>
        <style>
            body {
                background:#222528;
                color:#ffffff;
                margin:0;
                padding:0;
                min-width:1024px;
            }
        </style>
    </head>
    <body>
        <div id="login">
            <form method="post" id="loginform" >
                <label for="username">Username:</label>
                <input type="text" name="username" id="username">
                <label for="password">Password:</label>
                <input type="password" name="password" id="password">
                <select id="domain" name="domain" >
                    <option value="admin">admin.local</option>
                </select>
                <input type="submit" value="Login">
            </form>
        </div>
    </body>
</html>
+1 −1
Changes for src/CMakeLists.txt: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ list(APPEND authdbSrcs
    user.cpp
    domain.cpp
    group.cpp
    session.h
    session.cpp
    hash.cpp
    hash/sha512.cpp
)
+142 −0
Changes for src/admin.cpp: 142 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -38,12 +38,15 @@
#include "domain.h"
#include "types.h"
#include "hash.h"
#include "hash/sha512.h"
#include "session.h"

namespace authdb {
    class AdminController {
    public:
        AdminController(authdb::AuthBackend &backend,netplus::socket *ssock) : _AdminBackend(backend){
            _Indexpage.loadFile(_IndexElement,"../data/index.html");
            _Loginpage.loadFile(_LoginElement,"../data/login.html");
        };

        void listUsers(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args,const char *url){
@@ -1371,7 +1374,143 @@ namespace authdb {
            rep.send(curreq,out.c_str(),out.size());
        }

        void login(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
            libhttppp::HttpForm form;

            form.parse(curreq);

            std::string username,password,pwhash,domain;

            for(libhttppp::HttpForm::UrlcodedForm::Data *curformdat=form.UrlFormData.getFormData();
                curformdat; curformdat=curformdat->nextData()){
                if(strcmp(curformdat->getKey(),"username")==0)
                    username=curformdat->getValue();
                else if(strcmp(curformdat->getKey(),"password")==0)
                    password=curformdat->getValue();
                else if(strcmp(curformdat->getKey(),"domain")==0)
                    domain=curformdat->getValue();
            }

            if(password.empty() || username.empty())
                throw AuthBackendError("login: password or username are empty!");

            sha512 hash;
            hash.hash(password,pwhash);

            char cdid[255];
            uuid_t did;

            DomainBackend dbackend(_AdminBackend);

            AuthBackend *backend=nullptr;

            if(domain == "admin" ){
                backend=&_AdminBackend;
            }else if(uuid_parse(domain.c_str(),did) == 0 ){
                backend=dbackend.data(did);
            }else{
                throw AuthBackendError("login: could not parse uuid!");
            }

            User user;

            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 UserData udat(cur.uuid);
                    size_t upos=sizeof(authdb::AuthHeader);
                    user.info(*backend,udat,upos);

                    if(username==udat.getUsername() && pwhash==udat.getPwHash() ){
                        libhttppp::HttpCookie cookie;
                        uuid_t sid;
                        SessionData *cses=_Session.addSession(*backend,did,udat.uid);
                        cses->getSid(sid);
                        char csid[25];
                        uuid_unparse(sid,csid);

                        libhttppp::HttpResponse rep;
                        cookie.setcookie(&rep,"authdbid",csid);
                        rep.setState(HTTP307);
                        rep.setVersion(HTTPVERSION(1.1));
                        rep.setHeaderData("Location")->push_back(curreq->getRequestURL());
                        rep.setContentType("text/html");
                        rep.send(curreq,nullptr,0);
                        return;
                    }
                }
            }
            throw AuthBackendError("login: Wrong Password or Username!");
        }

        void SettingsController(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){

            libhttppp::HttpCookie cookie;

            try{
                cookie.parse(curreq);
            }catch(...){
                try {
                    login(curreq,tid,args);
                    return;
                }catch(...){
                    libhtmlpp::HtmlString out,dform;

                    libhtmlpp::HtmlElement index=_LoginElement,*domel=index.getElementbyID("domain");
                    Domain domain;

                    size_t rd=sizeof(authdb::AuthHeader),end=_AdminBackend.end();

                    while(rd<end){
                        authdb::AuthData::Record cur;

                        cur.type=EmptyData;

                        _AdminBackend.setPos(rd);
                        _AdminBackend.read((unsigned char*)&cur,sizeof(AuthData::Record));

                        rd=_AdminBackend.getPos()+cur.datasize;

                        if(cur.type == DataType::DomainData && strcmp(cur.fieldname,"domainname")==0){
                            class DomainData ddat(cur.uuid);

                            try {
                                size_t dpos=sizeof(authdb::AuthHeader);
                                domain.info(_AdminBackend,ddat,dpos);

                                char crdid[20];
                                uuid_unparse(cur.uuid,crdid);

                                dform << "<option value=\"" << crdid << "\" >"
                                << ddat.getDomainName()
                                << "</option>";

                                domel->appendChild(dform.parse());
                                dform.clear();
                            }catch(AuthBackendError &e){
                                std::cerr << e.what() << std::endl;
                            }
                        }
                    }

                    libhtmlpp::print(&index,out,true);
                    libhttppp::HttpResponse rep;
                    rep.setContentType("text/html");
                    rep.send(curreq,out.c_str(),out.size());
                    return;
                }
            }

            char domain[255],url[255];

            if(sscanf(curreq->getRequestURL(),"/settings/%[^/]/%s",domain,url)<0)
@@ -1413,7 +1552,10 @@ namespace authdb {
    private:
        authdb::AuthBackend     _AdminBackend;
        libhtmlpp::HtmlElement  _IndexElement;
        libhtmlpp::HtmlElement  _LoginElement;
        libhtmlpp::HtmlPage     _Indexpage;
        libhtmlpp::HtmlPage     _Loginpage;
        authdb::Session         _Session;
    };

    AdminInterface::AdminInterface(AuthBackend &bck,netplus::socket *ssock) : _bck(bck) {
+17 −0
Changes for src/group.cpp: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -164,6 +164,23 @@ void authdb::GroupData::addMember(const MemberUids &uid){
        }
}

bool authdb::GroupData::isMember(const uuid_t uid){
    if(!_Members)
        return false;

    MemberUids *mem=(MemberUids*)_Members->data;

    char *start=_Members->data+sizeof(MemberUids);

    for(size_t i=0; i<mem->count; ++i){
        uuid_t *ucmp =(uuid_t*)start+i;
        if(uuid_compare(uid,*ucmp)==0)
            return true;
    }

    return false;
}


bool authdb::Group::exits(AuthBackend& backend, uuid_t* gid, const char* name, int& ret){
    authdb::AuthData::Record cur;
+1 −1
Changes for src/group.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ namespace authdb {

        void addMember(const MemberUids &uid);
        void remMember(const MemberUids &uid);
        bool isMember(const MemberUids &uid);
        bool isMember(const uuid_t uid);

    private:
        AuthData::Record *_Name;
Loading