/
authn.go
616 lines (526 loc) · 15.2 KB
/
authn.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package meshauth
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log/slog"
"math/big"
"net/http"
"net/url"
"strings"
"time"
)
// Minimal implementatio of OIDC, matching K8S. Other helpers for platform-specific tokens.
// Also includes RequestAuthentication/TrustConfig support based on Istio API.
//
// A more common option is github.com/coreos/go-oidc, which depends on
// gopkg.in/square/go-jose.v2 The meshauth/oidc package is using that library to download
// and convert the public keys.
//
// GCP also uses (https://github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/blob/v0.2.0/auth/auth.go):
// https://securetoken.googleapis.com/v1/identitybindingtoken
// "serviceAccount:<project>.svc.id.goog[<namespace>/<sa>]"
//
// In Istio, the WorkloadID token can be exchanged for access tokens:
// POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/
// service-<GCP project number>@gcp-sa-meshdataplane.iam.gserviceaccount.com:generateAccessToken
// Content-Type: application/json
// Authorization: Bearer <federated token>
// {
// "Delegates": [],
// "Scope": [
// https://www.googleapis.com/auth/cloud-platform
// ],
// }
//
// curl http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/my-gsa@my-project.iam.gserviceaccount.com/token"
// -H'Metadata-Flavor:Google'
//
// Access tokens:
// https://developers.google.com/identity/toolkit/reference/securetoken/rest/v1/token
// POST https://securetoken.googleapis.com/v1/token
// grant_type=authorization_code&code=ID_TOKEN
// grant_type=refresh_token&refresh_token=TOKEN
// Resp: {
// "access_token": string,
// "expires_in": string,
// "token_type": string,`
// "refresh_token": string,
//}
//
//
// K8S k8s.io/apiserver/plugin/pkg/authenticator/token/oidc
// The test has a static JWK parser and helpers to convert from PEM
// Also uses jose.Thumbprint to compute Kid
// Authn handles JWK/OIDC authentication.
//
// A server may have different Authn configs for different listeners/hosts/routes - but typically one global
// config is more common.
type Authn struct {
Cfg *AuthnConfig
Verify func(context.Context, *TrustConfig, string) error
Client *http.Client
// Issuers is the map - config is using a list
Issuers map[string]*TrustConfig `json:-`
}
func NewAuthn(cfg *AuthnConfig) *Authn {
if cfg == nil {
cfg = &AuthnConfig{}
}
an := &Authn{Cfg: cfg, Issuers: map[string]*TrustConfig{}}
an.Client = http.DefaultClient
for _, v := range cfg.Issuers {
an.Issuers[v.Issuer] = v
}
return an
}
// Authn extracts credentials from request, applies the authn Rules to extact claims and
// sets the result in headers and context.
func (jauthn *Authn) Auth(actx *AuthContext, r *http.Request) error {
a := r.Header["Authorization"]
if len(a) == 0 {
return nil
}
rawa := strings.TrimSpace(a[0])
if strings.HasPrefix(rawa, BearerPrefix) ||
strings.HasPrefix(rawa, "bearer ") { // cloudrun is lower case for some reason
t := rawa[7:]
jt, err := jauthn.CheckJWT(t)
if err != nil {
return err
}
if jt.Email != "" {
r.Header["X-User"] = []string{jt.Email}
} else {
r.Header["X-User"] = []string{jt.Sub}
}
} else if strings.HasPrefix(rawa, "vapid") {
tok, pub, err := CheckVAPID(rawa, time.Now())
if err != nil {
return err
}
r.Header["X-VAPID-Pub"] = []string{string(pub)}
r.Header["X-VAPID-Sub"] = []string{tok.Sub}
}
return nil
}
var (
TransientIssuerError = errors.New("transient issuer error")
)
// CheckJWT will validate the JWT and return the 'sub' (subject) of the token.
//
// If the JWT is invalid - fails signature, invalid claims - error is set.
//
// If the OIDC keys can't be fetched - a 500 response should be returned (?)
// This is indicated with a nil error and nil jwt.
func (ja *Authn) CheckJWT(token string) (jwt *JWT, e error) {
idt := DecodeJWT(token)
if idt == nil {
return nil, errors.New("Invalid JWT")
}
if ja.Cfg.CloudrunIAM && idt.Iss == "https://accounts.google.com" {
// Special case: Cloudrun IAM will check the token and forward it without
// signature, for the developer token.
return idt, nil
}
now := time.Now()
expT := idt.Expiry()
if expT.Before(now) {
return nil, errors.New("expired token")
}
issuer := ja.Issuers[idt.Iss]
if issuer == nil {
return nil, errors.New("Unknown issuer " + idt.Iss)
}
var err error
kid := idt.Head.Kid
k := issuer.KeysByKid[kid]
if k == nil {
// Lazy Init
issuer.m.Lock()
k = issuer.KeysByKid[kid]
if k == nil {
err = ja.UpdateKeys(context.Background(), issuer)
}
issuer.m.Unlock()
if err != nil {
return nil, TransientIssuerError
}
}
//if ja.Verify != nil {
//err := ja.Verify(context.Background(), issuer, token)
//if err != nil {
//return nil, err
//}
k = issuer.KeysByKid[kid]
if k == nil {
// TODO: if single public, try it.
return nil, errors.New("Unknown kid")
}
err = idt.VerifySignature(k)
if err != nil {
return nil, err
}
// TODO: check audience
aud := ""
for _, a := range idt.Aud {
for _, b := range ja.Cfg.Audiences {
if a == b || strings.HasPrefix(a, b) {
aud = a
}
}
}
if aud == "" {
slog.Info("Invalid aud ", "iss", idt.Iss,
"aud", idt.Aud, "email", idt.Email) // , "tok", string(password))
return nil, errors.New("invalid audience")
}
slog.Info("AuthJwt", "iss", idt.Iss,
"aud", idt.Aud, "email", idt.Email) // , "tok", string(password))
// Use email as sub
if idt.Email != "" {
idt.Sub = idt.Email
}
return idt, nil
}
func (a *Authn) CheckJwtMap(password string) (tok map[string]string, e error) {
idt, err := a.CheckJWT(password)
if err != nil {
return nil, err
}
// TODO: check audience against config, domain
return map[string]string{"sub": idt.Sub}, nil
}
const BearerPrefix = "Bearer "
// WIP: discovery document returned when fetching the 'issuer' well known location
//
// wellKnown := strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration"
//
// Example: curl -v https://accounts.google.com/.well-known/openid-configuration
type OIDCDiscDoc struct {
// Should match the one in the URL
Issuer string `json:"issuer,omitempty"`
// Same as the URI in the Istio config - contains the keys.
// Example: "https://www.googleapis.com/oauth2/v3/certs"
JWKSURL string `json:"jwks_uri,omitempty"`
// Not used
AuthURL string `json:"authorization_endpoint,omitempty"`
DeviceAuthURL string `json:"device_authorization_endpoint,omitempty"`
TokenURL string `json:"token_endpoint,omitempty"`
UserInfoURL string `json:"userinfo_endpoint,omitempty"`
Algorithms []string `json:"id_token_signing_alg_values_supported,omitempty"`
}
// UpdateWellKnown downloads the JWKS from the well-known location
// Extracted from go-oidc
func (ja *Authn) UpdateWellKnown(ctx context.Context, issuer string, td *TrustConfig) error {
wellKnown := strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration"
req, err := http.NewRequestWithContext(ctx, "GET", wellKnown, nil)
if err != nil {
return err
}
resp, err := ja.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unable to read response body: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %s", resp.Status, body)
}
td.OIDC = &OIDCDiscDoc{}
err = json.Unmarshal(body, td.OIDC)
if err != nil {
return fmt.Errorf("oidc: failed to decode provider discovery object: %v", err)
}
if td.OIDC.Issuer != issuer {
return fmt.Errorf("oidc: issuer did not match the issuer returned by provider, expected %q got %q", issuer,
td.OIDC.Issuer)
}
td.JwksUri = td.OIDC.JWKSURL
return nil
}
// Init the JWT map - can also be used to reconfigure.
func (ja *Authn) FetchAllKeys(ctx context.Context, issuers []*TrustConfig) error {
if len(issuers) == 0 {
return nil
}
t0 := time.Now()
errs := []error{}
for _, i := range issuers {
err := ja.UpdateKeys(ctx, i)
if err != nil {
errs = append(errs, err)
}
}
if time.Since(t0) > 1 * time.Second {
slog.Info("Issuer init ", "d", time.Since(t0))
}
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}
// UpdateKeys will populate the Keys field, by fetching the keys.
func (ja *Authn) UpdateKeys(ctx context.Context, i *TrustConfig) error {
if len(i.JwksUri) == 0 {
err := ja.UpdateWellKnown(context.Background(), i.Issuer, i)
if err != nil {
return err
}
}
if len(i.JwksUri) > 0 {
// oidc.KeySet embed and refresh cached jose.JSONWebKey
// It is unfortunately just an interface - doesn't expose the actual keys.
err := ja.FetchKeys(ctx, i)
if err != nil {
return err
}
}
if len(i.Jwks) > 0 {
err := ja.ConvertJWKS(i)
if err != nil {
return err
}
}
return nil
}
func (ja *Authn) FetchKeys(ctx context.Context, i *TrustConfig) error {
url := i.JwksUri
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return err
}
resp, err := ja.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("oidc: get keys failed: %s %s", resp.Status, body)
}
// TODO: If the server doesn't provide cache control headers, assume the
// keys expire in 24h.
i.Jwks = string(body)
return nil
}
func (ja *Authn) ConvertJWKS(i *TrustConfig) error {
body := []byte(i.Jwks)
var keySet JSONWebKeySet
err := json.Unmarshal(body, &keySet)
if err != nil {
return fmt.Errorf("oidc: failed to decode keys: %v %s", err, body)
}
// Map of 'kid' to key
i.KeysByKid = map[string]interface{}{}
for _, ks := range keySet.Keys {
i.KeysByKid[ks.Kid], err = ks.Key()
if err != nil {
return err
}
}
return nil
}
type JSONWebKeySet struct {
Keys []rawJSONWebKey `json:"keys"`
}
// rawJSONWebKey represents a public or private key in JWK format, used for parsing/serializing.
// From jose.
type rawJSONWebKey struct {
Use string `json:"use,omitempty"`
Kty string `json:"kty,omitempty"`
Kid string `json:"kid,omitempty"`
Crv string `json:"crv,omitempty"`
Alg string `json:"alg,omitempty"`
K *byteBuffer `json:"k,omitempty"`
X *byteBuffer `json:"x,omitempty"`
Y *byteBuffer `json:"y,omitempty"`
N *byteBuffer `json:"n,omitempty"`
E *byteBuffer `json:"e,omitempty"`
// -- Following fields are only used for private keys --
// RSA uses D, P and Q, while ECDSA uses only D. Fields Dp, Dq, and Qi are
// completely optional. Therefore for RSA/ECDSA, D != nil is a contract that
// we have a private key whereas D == nil means we have only a public key.
D *byteBuffer `json:"d,omitempty"`
P *byteBuffer `json:"p,omitempty"`
Q *byteBuffer `json:"q,omitempty"`
Dp *byteBuffer `json:"dp,omitempty"`
Dq *byteBuffer `json:"dq,omitempty"`
Qi *byteBuffer `json:"qi,omitempty"`
// Certificates
X5c []string `json:"x5c,omitempty"`
X5u *url.URL `json:"x5u,omitempty"`
X5tSHA1 string `json:"x5t,omitempty"`
X5tSHA256 string `json:"x5t#S256,omitempty"`
}
// byteBuffer represents a slice of bytes that can be serialized to url-safe base64.
type byteBuffer struct {
data []byte
}
func (b *byteBuffer) MarshalJSON() ([]byte, error) {
return json.Marshal(b.base64())
}
func (b *byteBuffer) UnmarshalJSON(data []byte) error {
var encoded string
err := json.Unmarshal(data, &encoded)
if err != nil {
return err
}
if encoded == "" {
return nil
}
decoded, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil {
return err
}
*b = *newBuffer(decoded)
return nil
}
func newBuffer(data []byte) *byteBuffer {
if data == nil {
return nil
}
return &byteBuffer{
data: data,
}
}
func (b *byteBuffer) base64() string {
return base64.RawURLEncoding.EncodeToString(b.data)
}
func (b byteBuffer) bigInt() *big.Int {
return new(big.Int).SetBytes(b.data)
}
func (b byteBuffer) toInt() int {
return int(b.bigInt().Int64())
}
func (key *rawJSONWebKey) rsaPublicKey() (*rsa.PublicKey, error) {
if key.N == nil || key.E == nil {
return nil, fmt.Errorf("square/go-jose: invalid RSA key, missing n/e values")
}
return &rsa.PublicKey{
N: key.N.bigInt(),
E: key.E.toInt(),
}, nil
}
func fromRsaPublicKey(pub *rsa.PublicKey) *rawJSONWebKey {
return &rawJSONWebKey{
Kty: "RSA",
N: newBuffer(pub.N.Bytes()),
E: newBufferFromInt(uint64(pub.E)),
}
}
func (key *rawJSONWebKey) ecPublicKey() (*ecdsa.PublicKey, error) {
var curve elliptic.Curve
switch key.Crv {
case "P-256":
curve = elliptic.P256()
case "P-384":
curve = elliptic.P384()
case "P-521":
curve = elliptic.P521()
default:
return nil, fmt.Errorf("square/go-jose: unsupported elliptic curve '%s'", key.Crv)
}
if key.X == nil || key.Y == nil {
return nil, errors.New("square/go-jose: invalid EC key, missing x/y values")
}
// The length of this octet string MUST be the full size of a coordinate for
// the curve specified in the "crv" parameter.
// https://tools.ietf.org/html/rfc7518#section-6.2.1.2
if curveSize(curve) != len(key.X.data) {
return nil, fmt.Errorf("square/go-jose: invalid EC public key, wrong length for x")
}
if curveSize(curve) != len(key.Y.data) {
return nil, fmt.Errorf("square/go-jose: invalid EC public key, wrong length for y")
}
x := key.X.bigInt()
y := key.Y.bigInt()
if !curve.IsOnCurve(x, y) {
return nil, errors.New("square/go-jose: invalid EC key, X/Y are not on declared curve")
}
return &ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
}, nil
}
func (key *rawJSONWebKey) Key() (interface{}, error) {
switch key.Kty {
case "EC":
return key.ecPublicKey()
case "RSA":
return key.rsaPublicKey()
}
return nil, errors.New("Key not supported yet " + key.Kty)
}
func fromEcPublicKey(pub *ecdsa.PublicKey) (*rawJSONWebKey, error) {
if pub == nil || pub.X == nil || pub.Y == nil {
return nil, fmt.Errorf("square/go-jose: invalid EC key (nil, or X/Y missing)")
}
name, err := curveName(pub.Curve)
if err != nil {
return nil, err
}
size := curveSize(pub.Curve)
xBytes := pub.X.Bytes()
yBytes := pub.Y.Bytes()
if len(xBytes) > size || len(yBytes) > size {
return nil, fmt.Errorf("square/go-jose: invalid EC key (X/Y too large)")
}
key := &rawJSONWebKey{
Kty: "EC",
Crv: name,
X: newFixedSizeBuffer(xBytes, size),
Y: newFixedSizeBuffer(yBytes, size),
}
return key, nil
}
func newFixedSizeBuffer(data []byte, length int) *byteBuffer {
if len(data) > length {
panic("square/go-jose: invalid call to newFixedSizeBuffer (len(data) > length)")
}
pad := make([]byte, length-len(data))
return newBuffer(append(pad, data...))
}
func newBufferFromInt(num uint64) *byteBuffer {
data := make([]byte, 8)
binary.BigEndian.PutUint64(data, num)
return newBuffer(bytes.TrimLeft(data, "\x00"))
}
// Get JOSE name of curve
func curveName(crv elliptic.Curve) (string, error) {
switch crv {
case elliptic.P256():
return "P-256", nil
case elliptic.P384():
return "P-384", nil
case elliptic.P521():
return "P-521", nil
default:
return "", fmt.Errorf("square/go-jose: unsupported/unknown elliptic curve")
}
}
// Get size of curve in bytes
func curveSize(crv elliptic.Curve) int {
bits := crv.Params().BitSize
div := bits / 8
mod := bits % 8
if mod == 0 {
return div
}
return div + 1
}