-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
85 lines (70 loc) · 1.79 KB
/
middleware.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
// a simple middleware for handling JWT Tokens in Pragma Go backends
package pjwt
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
var SECRET_KEY []byte
func init() {
key := os.Getenv("PRAGMA_JWT_SECRET_KEY")
if key == "" {
panic("JWT Secret not found in environment")
}
SECRET_KEY = []byte(key)
}
type Adapter func(http.Handler) http.Handler
func SetAuthContext() Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := r.Header.Get("Authorization")
if !strings.Contains(v, "bearer") {
h.ServeHTTP(w, r)
return
}
tokenString := strings.SplitAfter(v, " ")[1]
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
return []byte(SECRET_KEY), nil
})
if err != nil {
panic(err)
}
if !token.Valid {
panic(err)
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
// TODO
panic("something not ok")
}
ctx := context.WithValue(r.Context(), "user_id", claims["user_id"])
h.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// create a claims token
func NewClaims(uid string) *jwt.Token {
return jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": uid,
"exp": time.Now().Add(time.Hour * time.Duration(12)).Unix(),
"iat": time.Now().Unix(),
})
}
// create a signed claims string
func NewSignedString(uid string) (string, error) {
return NewClaims(uid).SignedString(SECRET_KEY)
}
func UserIDFromContext(ctx context.Context) (string, bool) {
uid, ok := ctx.Value("user_id").(string)
if uid == "" {
ok = false
}
return uid, ok
}