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

editor: refresh MCP API token authid on every login

API tokens snapshot the blog authid of the session that created them.
Once that auth session expired, every MCP publish failed with 403
"Auth session not valid on blog server", and logging in again did not
help — only creating a new token did.

On each successful editor login (password login and SSO hydration via
the authid cookie) re-point all api_tokens of that uid/domain at the
fresh authid. Best effort: a DB error never fails the login.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent d4bf3676
Loading
Loading
Loading
Loading
+18 −0
Changes for editor/src/webedit_db.cpp: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -838,6 +838,24 @@ void webedit::Database::touchApiTokenLastUsed(const std::string &tokenId) {
    execWithRetry(sql, res);
}

void webedit::Database::refreshApiTokenAuthid(const std::string &ownerUid,
                                              const std::string &domain,
                                              const std::string &authid) {
    if (ownerUid.empty() || authid.empty())
        return;

    dbpp::SQL sql;
    dbpp::DBResult res;
    std::vector<char> eAuthid, eOwner, eDomain;

    sql << "UPDATE api_tokens SET authid='"
        << dbpp::SQL::escaped(eAuthid, authid.c_str()) << "' WHERE owner_uid='"
        << dbpp::SQL::escaped(eOwner, ownerUid.c_str()) << "' AND domain='"
        << dbpp::SQL::escaped(eDomain, domain.c_str()) << "';";

    execWithRetry(sql, res);
}

// --- AI Chat Messages ---

void webedit::Database::saveChatMessages(const std::string &ownerUid,
+4 −0
Changes for editor/src/webedit_db.h: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -97,6 +97,10 @@ namespace webedit {
        bool findTokenOwner(const std::string &tokenHash, std::string &ownerUid,
                            std::string &domain, std::string &authid, std::string &tokenId);
        void touchApiTokenLastUsed(const std::string &tokenId);
        // Re-point every token of this user/domain at a fresh blog auth session,
        // so MCP calls keep working after the snapshotted authid expired.
        void refreshApiTokenAuthid(const std::string &ownerUid, const std::string &domain,
                                   const std::string &authid);

        // Survey management — visible/editable by every user of the same domain
        std::string saveSurvey(const std::string &ownerUid, const std::string &domain,
+16 −0
Changes for editor/src/webedit_server.cpp: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -407,6 +407,20 @@ void webedit::Server::invalidateAuthCache(const std::string &authid) {
    _authCache.erase(authid);
}

// MCP API tokens snapshot the blog authid of the session that created them.
// That auth session expires, after which every MCP publish failed with 403
// until a new token was created — re-logging in did not help. Re-point the
// user's tokens at each fresh auth session instead. Best effort: a DB hiccup
// must never fail the login itself.
void webedit::Server::refreshMcpTokens(const std::string &uid,
                                       const std::string &domain,
                                       const std::string &authid) {
    try {
        _db.refreshApiTokenAuthid(uid, domain, authid);
    } catch (...) {
    }
}

void webedit::Server::ensureSessionFromAuthid(const std::string &authid,
                                              const std::string &sessionid) {
    if (authid.empty() || _authPools.empty())
@@ -442,6 +456,7 @@ void webedit::Server::ensureSessionFromAuthid(const std::string &authid,
            _session.addData(sessionid, "uid", uidStr);
            _session.addData(sessionid, "username", displayname);
            _session.addData(sessionid, "domain", _config.getAuthSource(i).domain);
            refreshMcpTokens(uidStr, _config.getAuthSource(i).domain, authid);
            return;
        } catch (...) {
            continue;
@@ -570,6 +585,7 @@ void webedit::Server::handleLogin(libhttppp::HttpRequest &curreq,
        // silently desyncs document ownership lookups (documents.domain)
        // from whatever this session's "domain" resolves to on future logins.
        _session.addData(sessionid, "domain", src->domain);
        refreshMcpTokens(uidStr, src->domain, authid);

        // Set authid cookie and return success
        libhttppp::HttpResponse resp;
+2 −0
Changes for editor/src/webedit_server.h: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -66,6 +66,8 @@ namespace webedit {
        bool isAuthenticated(const std::string &authid);
        void invalidateAuthCache(const std::string &authid);
        void ensureSessionFromAuthid(const std::string &authid, const std::string &sessionid);
        void refreshMcpTokens(const std::string &uid, const std::string &domain,
                              const std::string &authid);
        void handleLogin(libhttppp::HttpRequest &curreq, const std::string &sessionid);
        void handleLogout(libhttppp::HttpRequest &curreq, const std::string &sessionid);
        void handleAuthCheck(libhttppp::HttpRequest &curreq, const std::string &sessionid);