-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
55 lines (42 loc) · 1.32 KB
/
validation.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
package oidc
import (
"errors"
"time"
jwtgoForm3Tech "github.com/form3tech-oss/jwt-go"
"github.com/golang-jwt/jwt"
)
func getPemCert(JWKSResponse *JSONWebKeyResponse, token *jwtgoForm3Tech.Token) (string, error) {
cert := ""
for k := range JWKSResponse.Keys {
if token.Header["kid"] == JWKSResponse.Keys[k].Kid {
cert = "-----BEGIN CERTIFICATE-----\n" + JWKSResponse.Keys[k].X5c[0] + "\n-----END CERTIFICATE-----"
}
}
if cert == "" {
err := errors.New("unable to find appropriate key")
return cert, err
}
return cert, nil
}
func createValidationKeyGetter(doc *DiscoveryDocument) func(token *jwtgoForm3Tech.Token) (interface{}, error) {
return func(token *jwtgoForm3Tech.Token) (interface{}, error) {
//checkAud := token.Claims.(jwt.MapClaims).VerifyAudience(, true)
//if !checkAud {
// return token, errors.New("invalid audience")
//}
checkIss := token.Claims.(jwt.MapClaims).VerifyIssuer(doc.Issuer, true)
if !checkIss {
return token, errors.New("invalid issuer")
}
checkTime := token.Claims.(jwt.MapClaims).VerifyIssuedAt(time.Now().Unix(), true)
if !checkTime {
return token, errors.New("invalid issued at")
}
cert, err := getPemCert(doc.KeyResponse, token)
if err != nil {
panic(err.Error())
}
result, _ := jwt.ParseRSAPublicKeyFromPEM([]byte(cert))
return result, nil
}
}