-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
verify-jwt.go
266 lines (228 loc) · 7.04 KB
/
verify-jwt.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright 2020 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/tls"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"log"
"math/big"
"net/http"
"os"
"strings"
"time"
"github.com/go-jose/go-jose/v3/jwt"
)
// jwk is a JSON Web Key, described in detail in RFC 7517.
type jwk struct {
// KeyType is the type of asymmetric key used.
KeyType string `json:"kty"`
// Algorithm used to sign.
Algorithm string `json:"alg"`
// N is the modulus of the public key.
N string `json:"n"`
// E is the exponent of the public key.
E string `json:"e"`
// Curve identifies the cryptographic curve used with an ECDSA public key.
Curve string `json:"crv,omitempty"`
// X is the x coordinate parameter of an ECDSA public key.
X string `json:"x,omitempty"`
// Y is the y coordinate parameter of an ECDSA public key.
Y string `json:"y,omitempty"`
}
// jwksResponse is the response format for the JWK endpoint.
type jwksResponse struct {
// Keys is a list of public keys in JWK format.
Keys []jwk `json:"keys"`
}
// claims represents public and private claims for a JWT token.
type claims struct {
// Claims represents public claim values (as specified in RFC 7519).
jwt.Claims
// Username returns the Teleport identity of the user.
Username string `json:"username"`
// Roles returns the list of roles assigned to the user within Teleport.
Roles []string `json:"roles"`
// Traits returns the mapping of traits assigned to the user within Teleport.
Traits map[string][]string `json:"traits"`
}
// getPublicKey fetches the public key from the JWK endpoint.
func getPublicKey(url string, insecureSkipVerify bool) (crypto.PublicKey, error) {
// Fetch JWKs.
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
},
}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Parse JWKs response.
var response jwksResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
if len(response.Keys) == 0 {
return nil, fmt.Errorf("no keys found")
}
// Construct a crypto.PublicKey from the response.
jwk := response.Keys[0]
switch jwk.KeyType {
case "RSA":
return unmarshalRSAJWK(jwk)
case "EC":
return unmarshalECDSAJWK(jwk)
default:
return nil, fmt.Errorf("unsupported key type %v", jwk.KeyType)
}
}
func unmarshalRSAJWK(jwk jwk) (*rsa.PublicKey, error) {
if jwk.Algorithm != "RS256" {
return nil, fmt.Errorf("unsupported algorithm %v", jwk.Algorithm)
}
n, err := base64.RawURLEncoding.DecodeString(jwk.N)
if err != nil {
return nil, err
}
e, err := base64.RawURLEncoding.DecodeString(jwk.E)
if err != nil {
return nil, err
}
return &rsa.PublicKey{
N: new(big.Int).SetBytes(n),
E: int(new(big.Int).SetBytes(e).Uint64()),
}, nil
}
func unmarshalECDSAJWK(jwk jwk) (*ecdsa.PublicKey, error) {
if jwk.Algorithm != "ES256" {
return nil, fmt.Errorf("unsupported algorithm %v", jwk.Algorithm)
}
if jwk.Curve != elliptic.P256().Params().Name {
return nil, fmt.Errorf("unsupported curve %v", jwk.Curve)
}
x, err := base64.RawURLEncoding.DecodeString(jwk.X)
if err != nil {
return nil, err
}
y, err := base64.RawURLEncoding.DecodeString(jwk.Y)
if err != nil {
return nil, err
}
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: new(big.Int).SetBytes(x),
Y: new(big.Int).SetBytes(y),
}, nil
}
// verify will verify the JWT.
func verify(publicKey crypto.PublicKey, token string) (*claims, error) {
// Parse the raw token.
t, err := jwt.ParseSigned(token)
if err != nil {
return nil, err
}
// Validate the signature on the JWT token.
var out claims
if err := t.Claims(publicKey, &out); err != nil {
return nil, err
}
return &out, nil
}
// validate validates the passed in claims against received claims.
func validate(claims *claims, issuer string, subject string, audience string) error {
// Validate the claims on the JWT.
expectedClaims := jwt.Expected{
Issuer: issuer,
Subject: subject,
Audience: jwt.Audience{audience},
Time: time.Now(),
}
return claims.Validate(expectedClaims)
}
func printClaims(claims *claims) {
fmt.Printf("JWT Claims\n")
fmt.Printf("-----------\n")
fmt.Printf("Username: %v.\n", claims.Username)
fmt.Printf("Roles: %v.\n", strings.Join(claims.Roles, ","))
// Calculate the spacing between trait name and trait values.
maxLength := 0
printTraits := false
for name := range claims.Traits {
printTraits = true
nameLength := len(name)
if nameLength > maxLength {
maxLength = nameLength
}
}
maxLength++ // Increment by one for aligned trait values.
// Pretty print the traits (if there are any)
if printTraits {
fmt.Println("Traits:")
for name, values := range claims.Traits {
fmt.Printf(" %-*s %s\n", maxLength, name+":", strings.Join(values, ","))
}
}
fmt.Printf("Issuer: %v.\n", claims.Issuer)
fmt.Printf("Subject: %v.\n", claims.Subject)
fmt.Printf("Audience: %v.\n", claims.Audience)
}
func main() {
// Parse flags.
jwks := flag.String("jwks-url", "https://localhost:3080/.well-known/jwks.json", "JWK URL.")
skipVerify := flag.Bool("insecure-skip-verify", false, "Skip server certificate validation.")
jwt := flag.String("jwt", "", "JWT token to verify.")
validateClaims := flag.Bool("validate-claims", true, "Validate the claims received match expected.")
issuer := flag.String("issuer", "", "Issuer is name of the Teleport cluster.")
subject := flag.String("subject", "", "Subject is the identity of the Teleport user.")
audience := flag.String("audience", "", "Audience is the URI of the application.")
flag.Parse()
// Validate all required flags are set.
if *jwt == "" {
log.Fatal("JWT missing, required for validation.")
}
if *validateClaims {
if *issuer == "" || *subject == "" || *audience == "" {
log.Fatal("Issuer, Subject, and Audience required for validation.")
}
}
// Fetch and construct the public key that will be used to verify the JWT.
publicKey, err := getPublicKey(*jwks, *skipVerify)
if err != nil {
log.Fatalf("Failed to fetch JWKs needed to verify JWT: %v.", err)
}
// Verify the signature on the JWT.
claims, err := verify(publicKey, *jwt)
if err != nil {
log.Fatalf("JWT signature verification failed: %v.", err)
}
// Validate the claims if requested.
if *validateClaims {
if err := validate(claims, *issuer, *subject, *audience); err != nil {
log.Printf("Claim validation failed: %v.", err)
printClaims(claims)
os.Exit(1)
}
}
// Print claims and exit.
printClaims(claims)
}