-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_session.go
91 lines (75 loc) · 2.03 KB
/
auth_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
package auth
import (
"net/http"
"time"
"github.com/doug-martin/goqu/v9"
"github.com/gorilla/sessions"
"github.com/readeck/readeck/internal/auth/users"
)
// SessionAuthProvider is the last authentication provider.
// It's alway enabled in case of every previous provider failing.
type SessionAuthProvider struct {
// A function that returns the request's session
GetSession func(*http.Request) *sessions.Session
// A function that sets a Location header when
// authentication fails.
Redirect func(http.ResponseWriter, *http.Request)
}
// IsActive always returns true. As it's the last provider, when authentication fail it
// will with a redirect to the login page.
func (p *SessionAuthProvider) IsActive(_ *http.Request) bool {
return true
}
// Authenticate checks if the request's session cookie is valid and
// the user exists.
func (p *SessionAuthProvider) Authenticate(w http.ResponseWriter, r *http.Request) (*http.Request, error) {
sess := p.GetSession(r)
u, err := p.checkSession(sess)
if u == nil || err != nil {
p.clearSession(sess, w, r)
return r, err
}
// At this point, the user is granted access.
// We renew its session for another max age duration.
sess.Values["t"] = time.Now().Unix()
sess.Save(r, w)
return SetRequestAuthInfo(r, &Info{
Provider: &ProviderInfo{
Name: "http session",
},
User: u,
}), nil
}
func (p *SessionAuthProvider) checkSession(sess *sessions.Session) (u *users.User, err error) {
if sess.IsNew {
return
}
// Check expired first
st, ok := sess.Values["t"].(int64)
if !ok {
return
}
if time.Now().Unix() > st+int64(sess.Options.MaxAge) {
return
}
seed, ok := sess.Values["s"].(int)
if !ok {
return
}
id, ok := sess.Values["u"].(int)
if !ok {
return
}
if u, err = users.Users.GetOne(goqu.C("id").Eq(id)); err != nil {
return nil, err
}
if u.Seed != seed {
return nil, nil
}
return
}
func (p *SessionAuthProvider) clearSession(sess *sessions.Session, w http.ResponseWriter, r *http.Request) {
sess.Options.MaxAge = -1
sess.Save(r, w)
p.Redirect(w, r)
}