-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwtcontext.go
47 lines (39 loc) · 962 Bytes
/
jwtcontext.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
package bearerware
import (
"net/http"
"sync"
"github.com/dgrijalva/jwt-go"
)
/*
JWTContexter provides and interface for safe access to a shared map to get a jwt
for the current request scope when using net/http.
*/
type JWTContexter interface {
WriteJWT(*http.Request, *jwt.Token)
ReadJWT(*http.Request) (*jwt.Token, bool)
DeleteJWT(*http.Request)
}
type jwtContext struct {
sync.RWMutex
tokens map[*http.Request]*jwt.Token
}
//NewJWTContext creates a new JWTContexter
func NewJWTContext() JWTContexter {
return &jwtContext{tokens: make(map[*http.Request]*jwt.Token)}
}
func (j *jwtContext) WriteJWT(req *http.Request, token *jwt.Token) {
j.Lock()
defer j.Unlock()
j.tokens[req] = token
}
func (j *jwtContext) ReadJWT(req *http.Request) (*jwt.Token, bool) {
j.RLock()
defer j.RUnlock()
token, ok := j.tokens[req]
return token, ok
}
func (j *jwtContext) DeleteJWT(req *http.Request) {
j.Lock()
defer j.Unlock()
delete(j.tokens, req)
}