syncthing/lib
Emil Lundberg 2f15670094
lib/api: Extract session store (#9425)
This is an extract from PR #9175, which can be reviewed in isolation to
reduce the volume of changes to review all at once in #9175. There are
about to be several services and API handlers that read and set cookies
and session state, so this abstraction will prove helpful.

In particular a motivating cause for this is that with the current
architecture in PR #9175, in `api.go` the [`webauthnService` needs to
access the
session](https://github.com/syncthing/syncthing/pull/9175/files#diff-e2e14f22d818b8e635572ef0ee7718dee875c365e07225d760a6faae8be7772dR309-R310)
for authentication purposes but needs to be instantiated before the
`configMuxBuilder` for config purposes, because the WebAuthn additions
to config management need to perform WebAuthn registration ceremonies,
but currently the session management is embedded in the
`basicAuthAndSessionMiddleware` which is [instantiated much
later](https://github.com/syncthing/syncthing/pull/9175/files#diff-e2e14f22d818b8e635572ef0ee7718dee875c365e07225d760a6faae8be7772dL371-R380)
and only if authentication is enabled in `guiCfg`. This refactorization
extracts the session management out from `basicAuthAndSessionMiddleware`
so that `basicAuthAndSessionMiddleware` and `webauthnService` can both
use the same shared session management service to perform session
management logic.

### Testing

This is a refactorization intended to not change any externally
observable behaviour, so existing tests (e.g., `api_auth_test.go`)
should cover this where appropriate. I have manually verified that:

- Appending `+ "foo"` to the cookie name in `createSession` causes
`TestHtmlFormLogin/invalid_URL_returns_403_before_auth_and_404_after_auth`
and `TestHtmlFormLogin/UTF-8_auth_works` to fail
- Inverting the return value of `hasValidSession` cases a whole bunch of
tests in `TestHTTPLogin` and `TestHtmlFormLogin` to fail
- (Fixed) Changing the cookie to `MaxAge: 1000` in `destroySession` does
NOT cause any tests to fail!
- Added tests `TestHtmlFormLogin/Logout_removes_the_session_cookie`,
`TestHTTPLogin/*/Logout_removes_the_session_cookie`,
`TestHtmlFormLogin/Session_cookie_is_invalid_after_logout` and
`TestHTTPLogin/200_path#01/Session_cookie_is_invalid_after_logout` to
cover this.
- Manually verified that these tests pass both before and after the
changes in this PR, and that changing the cookie to `MaxAge: 1000` or
not calling `m.tokens.Delete(cookie.Value)` in `destroySession` makes
the respective pair of tests fail.
2024-03-21 08:09:47 -04:00
..
api lib/api: Extract session store (#9425) 2024-03-21 08:09:47 -04:00
assets lib/assets: MIME types, time formats (#8351) 2022-05-22 22:10:18 +02:00
automaxprocs all: Use own automaxprocs package that doesn't log (ref #9436) (#9437) 2024-02-27 13:05:19 +01:00
beacon lib/beacon, lib/discover: Send IPv4 limited broadcast when address listing fails (fixes #1628) (#9087) 2023-09-12 14:28:17 +02:00
build lib/build: Allow semver build in version regex (fixes #9267) (#9316) 2024-01-02 20:43:22 +01:00
config Hide log out button when auth is not enabled (#9158) 2023-10-15 14:10:41 +02:00
connections all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
db lib/model, lib/protocol: Remove FileInfoBatch reuse behavior (#9399) 2024-02-10 19:16:27 +01:00
dialer lib: Removal global connection registry (#8254) 2022-04-09 16:04:56 +02:00
discover lib/beacon, lib/discover: Send IPv4 limited broadcast when address listing fails (fixes #1628) (#9087) 2023-09-12 14:28:17 +02:00
events all: Add Prometheus-style metrics to expose some internal performance counters (fixes #5175) (#9003) 2023-08-04 19:57:30 +02:00
fs lib/fs: Add invalid UTF-8 guards to watcher (fixes #9369) (#9372) 2024-01-28 19:50:26 +01:00
httpcache cmd/stupgrades: Cache should apply to HEAD as well as GET 2023-02-22 12:22:52 +01:00
ignore lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340) 2024-01-15 10:13:22 +00:00
locations all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
logger all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
model lib/model: Don't bump seq on error in index handler (#9459) 2024-03-11 07:30:21 +01:00
nat all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
netutil all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
osutil lib/osutil, lib/upnp: Check FlagRunning (fixes #8767) (#9047) 2023-08-21 14:49:28 +00:00
pmp lib/nat, lib/upnp: IPv6 UPnP support (#9010) 2023-12-11 07:36:18 +01:00
protocol all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
rand all: Remove unused method receivers (#8462) 2022-07-28 17:32:45 +02:00
rc all: Remove unused method receivers (#8462) 2022-07-28 17:32:45 +02:00
relay cmd/strelaysrv: Add optional auth token (fixes #3987) (#8561) 2022-10-01 20:41:02 +01:00
scanner lib/scanner: Fix ticker leak in scanner (fixes #9417) (#9451) 2024-03-05 19:04:26 +01:00
semaphore all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
sha256 all: Use crypt/rand through its buffered version, but not in benchmarks (#7420) 2021-03-02 19:17:20 +01:00
signature all: Use crypt/rand through its buffered version, but not in benchmarks (#7420) 2021-03-02 19:17:20 +01:00
sliceutil all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
stats all: Truncate some timestamps (fixes #7457) (#7459) 2021-03-12 10:35:10 +01:00
stringutil all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
structutil all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
stun all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
svcutil all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
sync lib/model: Use a single lock (phase two: cleanup) (#9276) 2023-12-11 22:06:45 +01:00
syncthing lib/api: Improve folder summary event, verbose service (#9370) 2024-01-31 08:24:39 +01:00
testutil all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
tlsutil lib/tlsutil: Use crypto.Signer interface (#8526) 2022-09-09 14:22:38 +02:00
upgrade lib/upgrade: Extract signing key to embedded file (fixes #9247) (#9296) 2023-12-18 19:47:57 +00:00
upnp lib/nat, lib/upnp: IPv6 UPnP support (#9010) 2023-12-11 07:36:18 +01:00
ur all: Remove lib/util package (#9049) 2023-08-21 19:44:33 +02:00
versioner all: Use some Go 1.21 features (#9409) 2024-02-10 21:02:42 +01:00
watchaggregator lib/config: Allow sub-second watcher delay (fixes #7859) (#7864) 2023-03-18 08:50:38 +01:00
weakhash all: Remove usage of deprecated io/ioutil (#7971) 2021-11-22 08:59:47 +01:00