From aa901790b99e13c6cbf2bb3fda9b8ba3b765d700 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 4 Jan 2024 11:07:12 +0100 Subject: [PATCH] lib/api: Save session & CSRF tokens to database, add option to stay logged in (fixes #9151) (#9284) This adds a "token manager" which handles storing and checking expired tokens, used for both sessions and CSRF tokens. It removes the old, corresponding functionality for CSRFs which saved things in a file. The result is less crap in the state directory, and active login sessions now survive a Syncthing restart (this really annoyed me). It also adds a boolean on login to create a longer-lived session cookie, which is now possible and useful. Thus we can remain logged in over browser restarts, which was also annoying... :) Screenshot 2023-12-12 at 09 56 34 Best viewed with whitespace-insensitive diff, as a bunch of the auth functions became methods instead of closures which changed indentation. --- cmd/syncthing/main.go | 1 + gui/default/index.html | 6 + .../syncthing/core/syncthingController.js | 3 +- lib/api/api.go | 15 +- lib/api/api_auth.go | 212 ++++----- lib/api/api_auth_test.go | 77 ++++ lib/api/api_csrf.go | 108 +---- lib/api/api_test.go | 80 +--- lib/api/tokenmanager.go | 137 ++++++ lib/api/tokenset.pb.go | 411 ++++++++++++++++++ lib/locations/locations.go | 3 - lib/syncthing/syncthing.go | 6 +- proto/generate.go | 2 +- proto/lib/api/tokenset.proto | 8 + 14 files changed, 791 insertions(+), 278 deletions(-) create mode 100644 lib/api/tokenmanager.go create mode 100644 lib/api/tokenset.pb.go create mode 100644 proto/lib/api/tokenset.proto diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index f321ebc8b..ab3c76e73 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -862,6 +862,7 @@ func cleanConfigDirectory() { "backup-of-v0.8": 30 * 24 * time.Hour, // these neither "tmp-index-sorter.*": time.Minute, // these should never exist on startup "support-bundle-*": 30 * 24 * time.Hour, // keep old support bundle zip or folder for a month + "csrftokens.txt": 0, // deprecated, remove immediately } for pat, dur := range patterns { diff --git a/gui/default/index.html b/gui/default/index.html index 6e26461ce..7f5baa99e 100644 --- a/gui/default/index.html +++ b/gui/default/index.html @@ -359,6 +359,12 @@ +
+ +
+