/
auth0.go
126 lines (107 loc) · 2.85 KB
/
auth0.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package auth
import (
"encoding/json"
"errors"
"net/http"
"path"
log "github.com/communitybridge/easycla/cla-backend-go/logging"
"github.com/golang-jwt/jwt/v4"
)
// Validator data model
type Validator struct {
clientID string
usernameClaim string
algorithm string
wellKnownURL string
nameClaim string
emailClaim string
}
// NewAuthValidator creates a new auth0 validator based on the specified parameters
func NewAuthValidator(domain, clientID, usernameClaim, algorithm string) (Validator, error) { // nolint
if domain == "" {
return Validator{}, errors.New("missing Domain")
}
if clientID == "" {
return Validator{}, errors.New("missing ClientID")
}
if usernameClaim == "" {
return Validator{}, errors.New("missing UsernameClaim")
}
if algorithm == "" {
return Validator{}, errors.New("missing Algorithm")
}
validator := Validator{
clientID: clientID,
usernameClaim: usernameClaim,
algorithm: algorithm,
wellKnownURL: "https://" + path.Join(domain, ".well-known/jwks.json"),
nameClaim: "name",
emailClaim: "email",
}
return validator, nil
}
// VerifyToken verifies the specified token
func (av Validator) VerifyToken(token string) (map[string]interface{}, error) {
// Using jwt.MapClaims because our username field is set dynamically
// based on environment
claims := jwt.MapClaims{}
jwtToken, err := jwt.ParseWithClaims(token, claims, av.getPemCert)
if err != nil {
return nil, err
}
if !jwtToken.Valid {
return nil, errors.New("invalid token")
}
allClaims, ok := jwtToken.Claims.(jwt.MapClaims)
if !ok {
return nil, errors.New("unable to map claims")
}
if err = allClaims.Valid(); err != nil {
return nil, errors.New("claims are not valid")
}
return allClaims, nil
}
type jwks struct {
Keys []jsonWebKeys `json:"keys"`
}
type jsonWebKeys struct {
Kty string `json:"kty"`
Kid string `json:"kid"`
Use string `json:"use"`
N string `json:"n"`
E string `json:"e"`
X5c []string `json:"x5c"`
}
func (av Validator) getPemCert(token *jwt.Token) (interface{}, error) {
cert := ""
resp, err := http.Get(av.wellKnownURL)
if err != nil {
return "", err
}
defer func() {
closeErr := resp.Body.Close()
if closeErr != nil {
log.WithError(closeErr).Warn("problem closing response body")
}
}()
var j = jwks{}
err = json.NewDecoder(resp.Body).Decode(&j)
if err != nil {
return "", err
}
for k := range j.Keys {
if token.Header["kid"] == j.Keys[k].Kid {
cert = "-----BEGIN CERTIFICATE-----\n" + j.Keys[k].X5c[0] + "\n-----END CERTIFICATE-----"
}
}
if cert == "" {
return "", errors.New("unable to find appropriate key")
}
rsaPublicKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(cert))
if err != nil {
return "", err
}
return rsaPublicKey, nil
}