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
34
35
36
37
38
39
40
41
|
package handlers
import (
"auth-proxy/config"
"auth-proxy/logging"
"auth-proxy/models"
"net/http"
"time"
)
// LogoutHandler invalidates the user's token by expiring the cookie.
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
ip := logging.GetClientIP(r)
cookie, err := r.Cookie("auth_token")
if err != nil {
// No cookie, nothing to do.
http.Redirect(w, r, config.Paths.Login, http.StatusFound)
return
}
err = models.DeleteAuthToken(cookie.Value)
if err != nil {
// Log the error but still proceed with logging the user out client-side.
logging.AppLog.Error("Failed to delete token on logout", "error", err, "ip", ip)
}
// Expire the cookie by setting its max age to a negative number.
http.SetCookie(w, &http.Cookie{
Name: "auth_token",
Value: "",
Path: "/",
Expires: time.Unix(0, 0),
MaxAge: -1,
HttpOnly: true,
Secure: r.TLS != nil,
SameSite: http.SameSiteLaxMode,
})
logging.SecurityLog.Info("LOGOUT SUCCESS", "ip", ip)
http.Redirect(w, r, config.Paths.Login, http.StatusFound)
}
|