-
Notifications
You must be signed in to change notification settings - Fork 339
/
logout_handler.go
33 lines (29 loc) · 1.12 KB
/
logout_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package api
import (
"net/http"
"github.com/gorilla/sessions"
"github.com/treeverse/lakefs/pkg/logging"
)
// NewLogoutHandler returns a handler to clear the user sessions and redirect the user to the login page.
func NewLogoutHandler(sessionStore sessions.Store, logger logging.Logger, logoutRedirectURL string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := clearSession(w, r, sessionStore, InternalAuthSessionName)
if err != nil {
logger.WithError(err).Error("Failed to clear internal session during logout")
writeError(w, http.StatusInternalServerError, err)
return
}
err = clearSession(w, r, sessionStore, OIDCAuthSessionName)
if err != nil {
logger.WithError(err).Error("Failed to clear OIDC session during logout")
writeError(w, http.StatusInternalServerError, err)
return
}
http.Redirect(w, r, logoutRedirectURL, http.StatusTemporaryRedirect)
}
}
func clearSession(w http.ResponseWriter, r *http.Request, sessionStore sessions.Store, sessionName string) error {
session, _ := sessionStore.Get(r, sessionName)
session.Options.MaxAge = -1
return session.Save(r, w)
}