mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 10:07:34 -05:00
feat(auth-guest): add guest link logout endpoint
This commit is contained in:
1 parent
a7970e779e
commit
f5990666d1
5 files changed
+91
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2026 OpenCloud GmbH <mail@opencloud.eu>
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2026 OpenCloud GmbH <mail@opencloud.eu>
|
||||
// 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)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user