2016-11-17 23:21:26 +00:00
|
|
|
package oidc
|
|
|
|
|
|
|
|
import (
|
2017-03-08 18:33:36 +00:00
|
|
|
"context"
|
2019-06-20 16:27:47 +00:00
|
|
|
"errors"
|
2016-11-17 23:21:26 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pquerna/cachecontrol"
|
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// keysExpiryDelta is the allowed clock skew between a client and the OpenID Connect
|
|
|
|
// server.
|
|
|
|
//
|
|
|
|
// When keys expire, they are valid for this amount of time after.
|
|
|
|
//
|
|
|
|
// If the keys have not expired, and an ID Token claims it was signed by a key not in
|
|
|
|
// the cache, if and only if the keys expire in this amount of time, the keys will be
|
|
|
|
// updated.
|
|
|
|
const keysExpiryDelta = 30 * time.Second
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// NewRemoteKeySet returns a KeySet that can validate JSON web tokens by using HTTP
|
|
|
|
// GETs to fetch JSON web token sets hosted at a remote URL. This is automatically
|
|
|
|
// used by NewProvider using the URLs returned by OpenID Connect discovery, but is
|
|
|
|
// exposed for providers that don't support discovery or to prevent round trips to the
|
|
|
|
// discovery URL.
|
|
|
|
//
|
|
|
|
// The returned KeySet is a long lived verifier that caches keys based on cache-control
|
|
|
|
// headers. Reuse a common remote key set instead of creating new ones as needed.
|
|
|
|
//
|
|
|
|
// The behavior of the returned KeySet is undefined once the context is canceled.
|
|
|
|
func NewRemoteKeySet(ctx context.Context, jwksURL string) KeySet {
|
|
|
|
return newRemoteKeySet(ctx, jwksURL, time.Now)
|
|
|
|
}
|
|
|
|
|
2016-11-17 23:21:26 +00:00
|
|
|
func newRemoteKeySet(ctx context.Context, jwksURL string, now func() time.Time) *remoteKeySet {
|
|
|
|
if now == nil {
|
|
|
|
now = time.Now
|
|
|
|
}
|
|
|
|
return &remoteKeySet{jwksURL: jwksURL, ctx: ctx, now: now}
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteKeySet struct {
|
|
|
|
jwksURL string
|
|
|
|
ctx context.Context
|
|
|
|
now func() time.Time
|
|
|
|
|
|
|
|
// guard all other fields
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// inflight suppresses parallel execution of updateKeys and allows
|
2016-12-01 21:16:14 +00:00
|
|
|
// multiple goroutines to wait for its result.
|
2019-06-20 16:27:47 +00:00
|
|
|
inflight *inflight
|
2016-11-17 23:21:26 +00:00
|
|
|
|
|
|
|
// A set of cached keys and their expiry.
|
|
|
|
cachedKeys []jose.JSONWebKey
|
|
|
|
expiry time.Time
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// inflight is used to wait on some in-flight request from multiple goroutines.
|
2016-12-01 21:16:14 +00:00
|
|
|
type inflight struct {
|
2019-06-20 16:27:47 +00:00
|
|
|
doneCh chan struct{}
|
|
|
|
|
|
|
|
keys []jose.JSONWebKey
|
2016-12-01 21:16:14 +00:00
|
|
|
err error
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
func newInflight() *inflight {
|
|
|
|
return &inflight{doneCh: make(chan struct{})}
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// wait returns a channel that multiple goroutines can receive on. Once it returns
|
|
|
|
// a value, the inflight request is done and result() can be inspected.
|
|
|
|
func (i *inflight) wait() <-chan struct{} {
|
|
|
|
return i.doneCh
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// done can only be called by a single goroutine. It records the result of the
|
|
|
|
// inflight request and signals other goroutines that the result is safe to
|
|
|
|
// inspect.
|
|
|
|
func (i *inflight) done(keys []jose.JSONWebKey, err error) {
|
|
|
|
i.keys = keys
|
2016-12-01 21:16:14 +00:00
|
|
|
i.err = err
|
2019-06-20 16:27:47 +00:00
|
|
|
close(i.doneCh)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// result cannot be called until the wait() channel has returned a value.
|
|
|
|
func (i *inflight) result() ([]jose.JSONWebKey, error) {
|
|
|
|
return i.keys, i.err
|
|
|
|
}
|
2016-11-17 23:21:26 +00:00
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
func (r *remoteKeySet) VerifySignature(ctx context.Context, jwt string) ([]byte, error) {
|
|
|
|
jws, err := jose.ParseSigned(jwt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oidc: malformed jwt: %v", err)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
2019-06-20 16:27:47 +00:00
|
|
|
return r.verify(ctx, jws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteKeySet) verify(ctx context.Context, jws *jose.JSONWebSignature) ([]byte, error) {
|
|
|
|
// We don't support JWTs signed with multiple signatures.
|
|
|
|
keyID := ""
|
|
|
|
for _, sig := range jws.Signatures {
|
|
|
|
keyID = sig.Header.KeyID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, expiry := r.keysFromCache()
|
2016-11-17 23:21:26 +00:00
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
// Don't check expiry yet. This optimizes for when the provider is unavailable.
|
2016-11-17 23:21:26 +00:00
|
|
|
for _, key := range keys {
|
2019-06-20 16:27:47 +00:00
|
|
|
if keyID == "" || key.KeyID == keyID {
|
|
|
|
if payload, err := jws.Verify(&key); err == nil {
|
|
|
|
return payload, nil
|
|
|
|
}
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
if !r.now().Add(keysExpiryDelta).After(expiry) {
|
|
|
|
// Keys haven't expired, don't refresh.
|
|
|
|
return nil, errors.New("failed to verify id token signature")
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
keys, err := r.keysFromRemote(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fetching keys %v", err)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if keyID == "" || key.KeyID == keyID {
|
|
|
|
if payload, err := jws.Verify(&key); err == nil {
|
|
|
|
return payload, nil
|
|
|
|
}
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
2019-06-20 16:27:47 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("failed to verify id token signature")
|
|
|
|
}
|
2016-11-17 23:21:26 +00:00
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
func (r *remoteKeySet) keysFromCache() (keys []jose.JSONWebKey, expiry time.Time) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
return r.cachedKeys, r.expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
// keysFromRemote syncs the key set from the remote set, records the values in the
|
|
|
|
// cache, and returns the key set.
|
|
|
|
func (r *remoteKeySet) keysFromRemote(ctx context.Context) ([]jose.JSONWebKey, error) {
|
|
|
|
// Need to lock to inspect the inflight request field.
|
|
|
|
r.mu.Lock()
|
|
|
|
// If there's not a current inflight request, create one.
|
|
|
|
if r.inflight == nil {
|
|
|
|
r.inflight = newInflight()
|
|
|
|
|
|
|
|
// This goroutine has exclusive ownership over the current inflight
|
|
|
|
// request. It releases the resource by nil'ing the inflight field
|
|
|
|
// once the goroutine is done.
|
|
|
|
go func() {
|
|
|
|
// Sync keys and finish inflight when that's done.
|
|
|
|
keys, expiry, err := r.updateKeys()
|
|
|
|
|
|
|
|
r.inflight.done(keys, err)
|
|
|
|
|
|
|
|
// Lock to update the keys and indicate that there is no longer an
|
|
|
|
// inflight request.
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
r.cachedKeys = keys
|
|
|
|
r.expiry = expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free inflight so a different request can run.
|
|
|
|
r.inflight = nil
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
inflight := r.inflight
|
|
|
|
r.mu.Unlock()
|
2016-11-17 23:21:26 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2019-06-20 16:27:47 +00:00
|
|
|
case <-inflight.wait():
|
|
|
|
return inflight.result()
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
func (r *remoteKeySet) updateKeys() ([]jose.JSONWebKey, time.Time, error) {
|
2016-11-17 23:21:26 +00:00
|
|
|
req, err := http.NewRequest("GET", r.jwksURL, nil)
|
|
|
|
if err != nil {
|
2019-06-20 16:27:47 +00:00
|
|
|
return nil, time.Time{}, fmt.Errorf("oidc: can't create request: %v", err)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:27:47 +00:00
|
|
|
resp, err := doRequest(r.ctx, req)
|
2016-11-17 23:21:26 +00:00
|
|
|
if err != nil {
|
2019-06-20 16:27:47 +00:00
|
|
|
return nil, time.Time{}, fmt.Errorf("oidc: get keys failed %v", err)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-06-20 16:27:47 +00:00
|
|
|
return nil, time.Time{}, fmt.Errorf("unable to read response body: %v", err)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
2019-06-20 16:27:47 +00:00
|
|
|
|
2016-11-17 23:21:26 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2019-06-20 16:27:47 +00:00
|
|
|
return nil, time.Time{}, fmt.Errorf("oidc: get keys failed: %s %s", resp.Status, body)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var keySet jose.JSONWebKeySet
|
2019-06-20 16:27:47 +00:00
|
|
|
err = unmarshalResp(resp, body, &keySet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, time.Time{}, fmt.Errorf("oidc: failed to decode keys: %v %s", err, body)
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the server doesn't provide cache control headers, assume the
|
|
|
|
// keys expire immediately.
|
|
|
|
expiry := r.now()
|
|
|
|
|
|
|
|
_, e, err := cachecontrol.CachableResponse(req, resp, cachecontrol.Options{})
|
|
|
|
if err == nil && e.After(expiry) {
|
|
|
|
expiry = e
|
|
|
|
}
|
2019-06-20 16:27:47 +00:00
|
|
|
return keySet.Keys, expiry, nil
|
2016-11-17 23:21:26 +00:00
|
|
|
}
|