-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
57 lines (50 loc) · 1.29 KB
/
common.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
package auth
import (
"crypto/rsa"
"errors"
"fmt"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/lestrrat-go/jwx/jwk"
)
type (
Claim struct {
jwt.StandardClaims
}
AuthService interface {
auth(token string) (ud *UserDetails, err error)
authWithCheckAUD(token, aud string) (ud *UserDetails, err error)
}
)
var ErrNotValidAudience = errors.New("not valid audience")
func getTokenValidateFunc(endpoint string) func(t *jwt.Token) (interface{}, error) {
return func(t *jwt.Token) (interface{}, error) {
// get the public key for verifying the identity token signature
kid := t.Header["kid"].(string)
set, err := jwk.FetchHTTP(endpoint, jwk.WithHTTPClient(&http.Client{}))
if err != nil {
return nil, err
}
selectedKey := set.Keys[0]
for _, key := range set.Keys {
if key.KeyID() == kid {
selectedKey = key
break
}
}
var pubKeyIface interface{}
selectedKey.Raw(&pubKeyIface)
pubKey, ok := pubKeyIface.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf(`expected RSA public key from %s`, endpoint)
}
return pubKey, nil
}
}
func jwtValidate(token string, out *Claim, endpoint string) (*jwt.Token, error) {
t, err := new(jwt.Parser).ParseWithClaims(token, out, getTokenValidateFunc(endpoint))
if err != nil {
return nil, err
}
return t, nil
}