diff --git a/services/auth-guest/README.md b/services/auth-guest/README.md index 209465ad45..470a816df8 100644 --- a/services/auth-guest/README.md +++ b/services/auth-guest/README.md @@ -222,6 +222,10 @@ sequenceDiagram before the next attempt; after the maximum number of failed attempts the PIN is invalidated and while the backoff is active the endpoint answers `429`. Verifying the PIN does not consume the magic link. +5. **Log out** — the guest posts to + `POST /graph/v1beta1/extensions/org.libregraph/guestLinks/logout`, which + clears the session cookie. Sessions are stateless JWTs, so the session token + itself stays valid until it expires. ### Endpoints @@ -264,9 +268,14 @@ Rate-limited responses (`429`) also carry a `retryAfter` field (seconds) and a `404 shareNotFound`, `410 shareExpired`, `429 tooManyRequests` (with `Retry-After`), `500 internalError` +#### `POST .../guestLinks/logout` + +- **Request body:** none +- **Success:** `204`, clears the session cookie, also without a session cookie + ## Frontend integration -The three endpoints are unauthenticated and rely on cookies, so the client must +The endpoints are unauthenticated and rely on cookies, so the client must send credentials with every request (for example `credentials: 'include'` in `fetch`). Cross-origin deployments also need the service CORS to allow the origin and credentials; the default `AUTH_GUEST_CORS_ALLOW_ORIGINS` of `*` is diff --git a/services/auth-guest/pkg/server/http/logout.go b/services/auth-guest/pkg/server/http/logout.go new file mode 100644 index 0000000000..393d485036 --- /dev/null +++ b/services/auth-guest/pkg/server/http/logout.go @@ -0,0 +1,19 @@ +// Copyright 2026 OpenCloud GmbH +// SPDX-License-Identifier: Apache-2.0 + +package http + +import ( + "net/http" + + "github.com/opencloud-eu/opencloud/services/auth-guest/pkg/config" +) + +// LogoutHandler ends the guest session in this browser by clearing the session +// cookie. The session token stays valid until it expires. +func LogoutHandler(cfg *config.Config) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, _ *http.Request) { + clearSessionCookie(w, cfg) + w.WriteHeader(http.StatusNoContent) + } +} diff --git a/services/auth-guest/pkg/server/http/logout_test.go b/services/auth-guest/pkg/server/http/logout_test.go new file mode 100644 index 0000000000..09ba770061 --- /dev/null +++ b/services/auth-guest/pkg/server/http/logout_test.go @@ -0,0 +1,49 @@ +// Copyright 2026 OpenCloud GmbH +// SPDX-License-Identifier: Apache-2.0 + +package http + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/opencloud-eu/opencloud/services/auth-guest/pkg/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestLogoutHandler(t *testing.T) { + tests := []struct { + name string + sessionCookie bool + }{ + {name: "with session cookie", sessionCookie: true}, + {name: "without session cookie", sessionCookie: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := &config.Config{JWT: config.JWT{CookieName: testCookieName}} + req := httptest.NewRequest(http.MethodPost, "/", nil) + if tt.sessionCookie { + req.AddCookie(&http.Cookie{Name: testCookieName, Value: "session-token"}) + } + + rr := httptest.NewRecorder() + LogoutHandler(cfg)(rr, req) + + assert.Equal(t, http.StatusNoContent, rr.Code) + cookies := rr.Result().Cookies() + require.Len(t, cookies, 1) + cookie := cookies[0] + assert.Equal(t, testCookieName, cookie.Name) + assert.Empty(t, cookie.Value) + assert.Equal(t, -1, cookie.MaxAge) + assert.Equal(t, "/", cookie.Path) + assert.True(t, cookie.HttpOnly) + assert.True(t, cookie.Secure) + assert.Equal(t, http.SameSiteLaxMode, cookie.SameSite) + }) + } +} diff --git a/services/auth-guest/pkg/server/http/server.go b/services/auth-guest/pkg/server/http/server.go index 8578d6aff3..206b040def 100644 --- a/services/auth-guest/pkg/server/http/server.go +++ b/services/auth-guest/pkg/server/http/server.go @@ -62,6 +62,7 @@ func Server(opts ...Option) (ohttp.Service, error) { r.Post("/v1beta1/extensions/org.libregraph/guestLinks/verify/token", VerifyTokenHandler(options.Logger, options.Service, options.Config)) r.Post("/v1beta1/extensions/org.libregraph/guestLinks/renew", RenewHandler(options.Logger, options.Service, options.Config)) r.Post("/v1beta1/extensions/org.libregraph/guestLinks/verify/pin", VerifyPinHandler(options.Logger, options.Service, options.Config)) + r.Post("/v1beta1/extensions/org.libregraph/guestLinks/logout", LogoutHandler(options.Config)) }) err = micro.RegisterHandler(newService.Server(), mux) diff --git a/services/auth-guest/pkg/server/http/verify_token.go b/services/auth-guest/pkg/server/http/verify_token.go index 3a81b0109b..a865b946a6 100644 --- a/services/auth-guest/pkg/server/http/verify_token.go +++ b/services/auth-guest/pkg/server/http/verify_token.go @@ -78,6 +78,18 @@ func setSessionCookie(w http.ResponseWriter, cfg *config.Config, sessionToken st }) } +func clearSessionCookie(w http.ResponseWriter, cfg *config.Config) { + http.SetCookie(w, &http.Cookie{ + Name: cfg.JWT.CookieName, + Value: "", + Path: "/", + HttpOnly: true, + Secure: true, + SameSite: http.SameSiteLaxMode, + MaxAge: -1, + }) +} + func setNonceCookie(w http.ResponseWriter, cfg *config.Config, shareID, nonce string) { http.SetCookie(w, &http.Cookie{ Name: nonceCookieName(cfg.Nonce.CookieBaseName, shareID),