-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
111 lines (91 loc) · 3.04 KB
/
session.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package session
import (
"net/http"
gorilla "github.com/gorilla/sessions"
"github.com/xy-planning-network/trails"
)
// A Session provides all functionality for managing a fully featured session.
//
// Its functionality is implemented by lightly wrapping a gorilla.Session.
type Session struct {
s *gorilla.Session
}
// ClearFlashes removes all Flashes from the Session.
func (s Session) ClearFlashes(w http.ResponseWriter, r *http.Request) {
_ = s.Flashes(w, r)
}
// Delete removes a session by making the MaxAge negative.
func (s Session) Delete(w http.ResponseWriter, r *http.Request) error {
s.s.Options.MaxAge = -1
return s.Save(w, r)
}
// DeregisterUser removes the User from the session.
func (s Session) DeregisterUser(w http.ResponseWriter, r *http.Request) error {
delete(s.s.Values, trails.CurrentUserKey)
return s.Save(w, r)
}
// Flashes retrieves []Flash stored in the session.
func (s Session) Flashes(w http.ResponseWriter, r *http.Request) []Flash {
raw := s.s.Flashes()
if len(raw) == 0 {
return nil
}
fs := make([]Flash, 0)
for _, r := range raw {
f, ok := r.(Flash)
if !ok {
continue
}
fs = append(fs, f)
}
if len(fs) > 0 {
// NOTE(dlk): Flashes are removed after they are accessed,
// but the session needs to be saved for them to be finally removed
if err := s.Save(w, r); err != nil {
return nil
}
}
return fs
}
// Get retrieves a value from the session according to the key passed in.
func (s Session) Get(key any) any {
return s.s.Values[key]
}
// RegisterUserSession stores the user's ID in the session.
func (s Session) RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error {
s.s.Values[trails.CurrentUserKey] = ID
return s.Save(w, r)
}
// ResetExpiry resets the expiration of the session by saving it.
func (s Session) ResetExpiry(w http.ResponseWriter, r *http.Request) error {
return s.Save(w, r)
}
// Save wraps gorilla.Session.Save, saving the session in the request.
func (s Session) Save(w http.ResponseWriter, r *http.Request) error { return s.s.Save(r, w) }
// Set stores a value according to the key passed in on the session.
func (s Session) Set(w http.ResponseWriter, r *http.Request, key trails.Key, val any) error {
s.s.Values[key] = val
return s.Save(w, r)
}
// SetFlash stores the passed in Flash in the session.
func (s Session) SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error {
s.s.AddFlash(flash)
return s.Save(w, r)
}
// UserID gets the user ID out of the session.
// A user ID should be present in a session if the user is successfully authenticated.
// If no user ID can be found, this ErrNoUser is returned.
// This ought to only happen when a user is going through an authentication workflow or hitting unauthenticated pages.
//
// If the value returned from the session is not a uint, ErrNotValid is returned and represents a programming error.
func (s Session) UserID() (uint, error) {
intfVal, ok := s.s.Values[trails.CurrentUserKey]
if !ok {
return 0, ErrNoUser
}
val, ok := intfVal.(uint)
if !ok {
return 0, ErrNotValid
}
return val, nil
}