-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
70 lines (56 loc) · 1.47 KB
/
validator.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 identity
import (
"crypto/rsa"
"errors"
"fmt"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/myshkin5/effective-octo-garbanzo/logs"
)
type Validator struct {
publicKeys map[string]*rsa.PublicKey
}
type CustomClaims struct {
Org string `json:"custom:org"`
jwt.StandardClaims
}
func (c *CustomClaims) Valid() error {
err := c.StandardClaims.Valid()
if err != nil {
return err
}
if len(c.Org) == 0 {
return errors.New("org not specified in claims")
}
return nil
}
const bearerPrefix = "bearer "
func NewValidator(publicKeys map[string]*rsa.PublicKey) *Validator {
return &Validator{
publicKeys: publicKeys,
}
}
func (v *Validator) IsValid(authHeader string) (ok bool, org string) {
if !strings.HasPrefix(strings.ToLower(authHeader), bearerPrefix) {
logs.Logger.Infof("Authentication header lacks %sprefix", bearerPrefix)
return false, ""
}
tokenString := authHeader[len(bearerPrefix):]
var claims CustomClaims
_, err := jwt.ParseWithClaims(tokenString, &claims, func(token *jwt.Token) (interface{}, error) {
keyId, ok := token.Header["kid"].(string)
if !ok {
return nil, fmt.Errorf("key id (kid) is not a string in headers: %v", token.Header)
}
publicKey, ok := v.publicKeys[keyId]
if !ok {
return nil, fmt.Errorf("no public key found for key id (kid) %v", keyId)
}
return publicKey, nil
})
if err != nil {
logs.Logger.Infof("Error parsing authentication header, %v", err)
return false, ""
}
return true, claims.Org
}