Commit 57e39ed4 authored by jan.koester's avatar jan.koester
Browse files

test

parent 8f871e9d
Loading
Loading
Loading
Loading
+31 −1
Changes for src/auth.cpp: 31 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -271,7 +271,37 @@ void blogi::Auth::Apilogin(const int tid,const char *username,const char *passwo
    _doLoginWithFailover(srcs, user.c_str(), password, authid, tid, nullptr);
}

bool blogi::Auth::isLoggedIn(const int tid,const uuid::uuid &authid){
static constexpr std::chrono::seconds LOGIN_CACHE_TTL{30};
static constexpr size_t LOGIN_CACHE_MAX = 4096;

void blogi::Auth::forget(const uuid::uuid &authid){
      std::lock_guard<std::mutex> lk(_loginCacheLock);
      _loginCache.erase(std::string(authid.c_str()));
}

bool blogi::Auth::isLoggedIn(const int /*tid*/,const uuid::uuid &authid){
      const std::string key(authid.c_str());
      const auto now = std::chrono::steady_clock::now();
      {
          std::lock_guard<std::mutex> lk(_loginCacheLock);
          auto it = _loginCache.find(key);
          if(it != _loginCache.end() && now - it->second.at < LOGIN_CACHE_TTL)
              return it->second.ok;
      }
      bool ok = _checkLoggedIn(authid);
      std::lock_guard<std::mutex> lk(_loginCacheLock);
      if(_loginCache.size() >= LOGIN_CACHE_MAX){
          for(auto it = _loginCache.begin(); it != _loginCache.end(); ){
              if(now - it->second.at >= LOGIN_CACHE_TTL) it = _loginCache.erase(it);
              else ++it;
          }
          if(_loginCache.size() >= LOGIN_CACHE_MAX) _loginCache.clear();
      }
      _loginCache[key] = LoginVerdict{ok, now};
      return ok;
}

bool blogi::Auth::_checkLoggedIn(const uuid::uuid &authid){
      // Plugin-only sources (loginEligible=false) never grant blogi backend
      // access, even if the session happens to authenticate against one --
      // otherwise adding one for a plugin's own domain checks would quietly
+13 −0
Changes for src/auth.h: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,10 @@
 *******************************************************************************/

#include <cstdio>
#include <chrono>
#include <mutex>
#include <string>
#include <unordered_map>

#include <fenrirds/client.h>
#include <uuidp.h>
@@ -50,7 +54,16 @@ namespace blogi {
        bool isLoggedIn(const int tid,const uuid::uuid &authid);
        bool isLoggedIn(const int tid,libhttppp::HttpRequest &curreq,const std::string &ssid);
        std::string getAuthorId(const int tid, const uuid::uuid &authid);
        // Drop a cached isLoggedIn() verdict (logout).
        void forget(const uuid::uuid &authid);
    private:
        bool _checkLoggedIn(const uuid::uuid &authid);
        // isLoggedIn() is called several times per page; every uncached call is a
        // fresh TLS connection + ClientAuth + GPOcheck against authdb (and a new
        // server-side client connection there). Cache the verdict briefly.
        struct LoginVerdict { bool ok; std::chrono::steady_clock::time_point at; };
        std::mutex                                       _loginCacheLock;
        std::unordered_map<std::string, LoginVerdict>    _loginCache;
        void _doLogin(const AuthSource &src, const char *username, const char *password,
                      uuid::uuid &authid, const int tid, const std::string *ssid);
        // Try each source in turn (failover chain for one realm), returning
+1 −0
Changes for src/blogi.cpp: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -1237,6 +1237,7 @@ void blogi::Blogi::logoutPage(libhttppp::HttpRequest &curreq, const int tid, con
    {
        clt.remSession();
    }
    PlgArgs->auth->forget(sessid);

    libhttppp::HttpResponse curres;