-
Notifications
You must be signed in to change notification settings - Fork 26
/
jwt.go
103 lines (86 loc) 路 2.55 KB
/
jwt.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
package service
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/src-d/code-annotation/server/model"
jwt "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
)
// Strips 'Bearer ' prefix from bearer token string
func stripBearerPrefixFromTokenString(tok string) (string, error) {
// Should be a bearer token
if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " {
return tok[7:], nil
}
return tok, nil
}
var extractor = &request.PostExtractionFilter{
Extractor: &request.MultiExtractor{
request.HeaderExtractor{"Authorization"},
request.ArgumentExtractor{"jwt_token"},
},
Filter: stripBearerPrefixFromTokenString,
}
// JWTConfig defines enviroment variables for JWT
type JWTConfig struct {
SigningKey string `envconfig:"SIGNING_KEY" required:"true"`
}
// JWT service abstracts JWT implementation
type JWT struct {
signingKey []byte
}
// NewJWT return new JWT service
func NewJWT(signingKey string) *JWT {
return &JWT{signingKey: []byte(signingKey)}
}
type userIDContext int
const userIDKey userIDContext = 1
type jwtClaim struct {
ID int
jwt.StandardClaims
}
// MakeToken generates token string for a user
func (j *JWT) MakeToken(user *model.User) (string, error) {
claims := &jwtClaim{ID: user.ID}
t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := t.SignedString(j.signingKey)
if err != nil {
return "", fmt.Errorf("can't sign jwt token: %s", err)
}
return ss, nil
}
// Middleware return http.Handler which validates token and set user id in context
func (j *JWT) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var claims jwtClaim
_, err := request.ParseFromRequestWithClaims(r, extractor, &claims, func(token *jwt.Token) (interface{}, error) {
return j.signingKey, nil
})
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
r = r.WithContext(SetUserID(r.Context(), claims.ID))
next.ServeHTTP(w, r)
})
}
// getUserInt gets the value stored in the Context for the key userIDKey, bool
// is true on success
func getUserInt(ctx context.Context) (int, bool) {
i, ok := ctx.Value(userIDKey).(int)
return i, ok
}
// SetUserID sets the user ID to the context
func SetUserID(ctx context.Context, userID int) context.Context {
return context.WithValue(ctx, userIDKey, userID)
}
// GetUserID gets the user ID set by the JWT middleware in the Context
func GetUserID(ctx context.Context) (int, error) {
id, ok := getUserInt(ctx)
if !ok {
return 0, fmt.Errorf("User ID is not set in the context")
}
return id, nil
}