forked from noxecane/anansi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
120 lines (100 loc) · 2.91 KB
/
sessions.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
112
113
114
115
116
117
118
119
120
package siber
import (
"errors"
"net/http"
"strings"
"time"
"github.com/random-guys/go-siber/jwt"
"github.com/random-guys/go-siber/tokens"
)
var (
ErrAuthorisationFormat = errors.New("Your authorization header format is invalid")
ErrUnsupportedScheme = errors.New("Your scheme is not supported")
ErrEmptyToken = errors.New("There was no token supplied to the authorization header")
ErrHeaderNotSet = errors.New("Authorization header is not set")
)
type SessionStore struct {
store *tokens.Store
timeout time.Duration
secret []byte
scheme string
}
func NewSessionStore(secret []byte, scheme string, timeout time.Duration, store *tokens.Store) *SessionStore {
return &SessionStore{store, timeout, secret, scheme}
}
// Load retrieves a user's session object based on the session key from the Authorization
// header or the session cookie and fails with an error if it faces any issue parsing any of them.
func (s *SessionStore) Load(r *http.Request, session interface{}) {
var err error
scheme, token := getAuthorization(r)
if scheme != s.scheme && scheme != "Bearer" {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: ErrUnsupportedScheme.Error(),
})
}
if token == "" {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: ErrEmptyToken.Error(),
})
}
if scheme == "Bearer" {
err = s.store.Extend(r.Context(), token, s.timeout, session)
} else {
err = jwt.DecodeStruct(s.secret, []byte(token), session)
}
if err != nil {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: "Your token is invalid",
Err: err,
})
}
}
// Secure loads a user session into the request context
func (s *SessionStore) Headless() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scheme, token := getAuthorization(r)
if scheme != s.scheme {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: ErrUnsupportedScheme.Error(),
})
}
if token == "" {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: ErrEmptyToken.Error(),
})
}
// read and discard session data
if err := jwt.DecodeEmbedded(s.secret, []byte(token), &struct{}{}); err != nil {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: "Your token is invalid",
Err: err,
})
}
next.ServeHTTP(w, r)
})
}
}
func getAuthorization(r *http.Request) (scheme, token string) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: ErrHeaderNotSet.Error(),
})
}
splitAuth := strings.Split(strings.TrimSpace(authHeader), " ")
if len(splitAuth) != 2 {
panic(JSendError{
Code: http.StatusUnauthorized,
Message: ErrAuthorisationFormat.Error(),
})
}
return strings.TrimSpace(splitAuth[0]), splitAuth[1]
}