-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjwt.go
70 lines (60 loc) · 1.54 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
package main
import (
"context"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"net/http"
"time"
"gopkg.in/square/go-jose.v2/jwt"
)
type JWTPubKeyConfig struct {
log *log.Logger
pubKey interface{}
audience string
}
func JWTPubKeyAuth(cfg *JWTPubKeyConfig, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawToken := getToken(r)
if rawToken == "" {
cfg.log.Print("no token found in request")
http.Error(w, http.StatusText(401)+": no token", 401)
return
}
token, err := jwt.ParseSigned(rawToken)
if err != nil {
cfg.log.Print("failed to parse token:", err)
http.Error(w, http.StatusText(401)+": "+err.Error(), 401)
return
}
claims := jwt.Claims{}
if err := token.Claims(cfg.pubKey, &claims); err != nil {
cfg.log.Print("failed to verify token:", err)
http.Error(w, http.StatusText(401)+": "+err.Error(), 401)
return
}
expected := jwt.Expected{
Time: time.Now(),
}
if cfg.audience != "" {
expected.Audience = []string{cfg.audience}
}
err = claims.Validate(expected)
if err != nil {
cfg.log.Print("could not validate token:", err)
http.Error(w, http.StatusText(401)+": "+err.Error(), 401)
return
}
ctx := context.WithValue(r.Context(), "subject", claims.Subject)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func loadPubKey(file string) (interface{}, error) {
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
block, _ := pem.Decode(content)
return x509.ParsePKIXPublicKey(block.Bytes)
}