/
auth.go
105 lines (87 loc) · 2.74 KB
/
auth.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
// Package auth provides authentication and authorization support.
package auth
import (
"crypto/rsa"
"errors"
"fmt"
"github.com/golang-jwt/jwt"
)
// KeyLookup declares a method set of behavior for looking up
// private and public keys for JWT use.
type KeyLookup interface {
PrivateKey(kid string) (*rsa.PrivateKey, error)
PublicKey(kid string) (*rsa.PublicKey, error)
}
// Auth is used to authenticate clients. It can generate a token for a
// set of user claims and recreate the claims by parsing the token.
type Auth struct {
activeKID string
keyLookup KeyLookup
method jwt.SigningMethod
keyFunc func(t *jwt.Token) (interface{}, error)
parser jwt.Parser
}
// New creates an Auth to support authentication/authorization.
func New(activeKID string, keyLookup KeyLookup) (*Auth, error) {
// The activeKID represents the private key used to signed new tokens.
_, err := keyLookup.PrivateKey(activeKID)
if err != nil {
return nil, errors.New("active KID does not exist in store")
}
method := jwt.GetSigningMethod("RS256")
if method == nil {
return nil, errors.New("configuring algorithm RS256")
}
keyFunc := func(t *jwt.Token) (interface{}, error) {
kid, ok := t.Header["kid"]
if !ok {
return nil, errors.New("missing key id (kid) in token header")
}
kidID, ok := kid.(string)
if !ok {
return nil, errors.New("user token key id (kid) must be string")
}
return keyLookup.PublicKey(kidID)
}
// Create the token parser to use. The algorithm used to sign the JWT must be
// validated to avoid a critical vulnerability:
// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
parser := jwt.Parser{
ValidMethods: []string{"RS256"},
}
a := Auth{
activeKID: activeKID,
keyLookup: keyLookup,
method: method,
keyFunc: keyFunc,
parser: parser,
}
return &a, nil
}
// GenerateToken generates a signed JWT token string representing the user Claims.
func (a *Auth) GenerateToken(claims Claims) (string, error) {
token := jwt.NewWithClaims(a.method, claims)
token.Header["kid"] = a.activeKID
privateKey, err := a.keyLookup.PrivateKey(a.activeKID)
if err != nil {
return "", errors.New("kid lookup failed")
}
str, err := token.SignedString(privateKey)
if err != nil {
return "", fmt.Errorf("signing token: %w", err)
}
return str, nil
}
// ValidateToken recreates the Claims that were used to generate a token. It
// verifies that the token was signed using our key.
func (a *Auth) ValidateToken(tokenStr string) (Claims, error) {
var claims Claims
token, err := a.parser.ParseWithClaims(tokenStr, &claims, a.keyFunc)
if err != nil {
return Claims{}, fmt.Errorf("parsing token: %w", err)
}
if !token.Valid {
return Claims{}, errors.New("invalid token")
}
return claims, nil
}