forked from bokwoon95/notebrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogout.go
84 lines (81 loc) · 2.34 KB
/
logout.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package notebrew
import (
"encoding/hex"
"fmt"
"html/template"
"net/http"
"strings"
"github.com/bokwoon95/notebrew/sq"
"golang.org/x/crypto/blake2b"
)
func (nbrew *Notebrew) logout(w http.ResponseWriter, r *http.Request, user User) {
if user.UserID.IsZero() {
http.Redirect(w, r, "/users/login/", http.StatusFound)
return
}
var sessionToken string
header := r.Header.Get("Authorization")
if header != "" {
if strings.HasPrefix(header, "Bearer ") {
sessionToken = strings.TrimPrefix(header, "Bearer ")
}
} else {
cookie, _ := r.Cookie("session")
if cookie != nil {
sessionToken = cookie.Value
}
}
if sessionToken == "" {
http.Redirect(w, r, "/users/login/", http.StatusFound)
return
}
sessionTokenBytes, err := hex.DecodeString(fmt.Sprintf("%048s", sessionToken))
if err != nil || len(sessionTokenBytes) != 24 {
http.Redirect(w, r, "/users/login/", http.StatusFound)
return
}
var sessionTokenHash [8 + blake2b.Size256]byte
checksum := blake2b.Sum256(sessionTokenBytes[8:])
copy(sessionTokenHash[:8], sessionTokenBytes[:8])
copy(sessionTokenHash[8:], checksum[:])
switch r.Method {
case "GET", "HEAD":
funcMap := map[string]any{
"stylesCSS": func() template.CSS { return template.CSS(StylesCSS) },
"baselineJS": func() template.JS { return template.JS(BaselineJS) },
"referer": func() string { return r.Referer() },
}
tmpl, err := template.New("logout.html").Funcs(funcMap).ParseFS(RuntimeFS, "embed/logout.html")
if err != nil {
nbrew.GetLogger(r.Context()).Error(err.Error())
nbrew.InternalServerError(w, r, err)
return
}
w.Header().Set("Content-Security-Policy", nbrew.ContentSecurityPolicy)
nbrew.ExecuteTemplate(w, r, tmpl, nil)
case "POST":
http.SetCookie(w, &http.Cookie{
Path: "/",
Name: "session",
Value: "0",
MaxAge: -1,
})
if sessionTokenHash != [len(sessionTokenHash)]byte{} {
_, err := sq.Exec(r.Context(), nbrew.DB, sq.Query{
Dialect: nbrew.Dialect,
Format: "DELETE FROM session WHERE session_token_hash = {sessionTokenHash}",
Values: []any{
sq.BytesParam("sessionTokenHash", sessionTokenHash[:]),
},
})
if err != nil {
nbrew.GetLogger(r.Context()).Error(err.Error())
nbrew.InternalServerError(w, r, err)
return
}
}
http.Redirect(w, r, "/users/login/", http.StatusFound)
default:
nbrew.MethodNotAllowed(w, r)
}
}