Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cace7d179d | ||
|
|
550764d43c |
@@ -33,9 +33,9 @@ require (
|
|||||||
github.com/rogpeppe/go-internal v1.15.0 // indirect
|
github.com/rogpeppe/go-internal v1.15.0 // indirect
|
||||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
||||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||||
golang.org/x/sync v0.18.0 // indirect
|
golang.org/x/sync v0.21.0 // indirect
|
||||||
golang.org/x/sys v0.41.0 // indirect
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
golang.org/x/text v0.31.0 // indirect
|
golang.org/x/text v0.39.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.8 // indirect
|
google.golang.org/protobuf v1.36.8 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -113,12 +113,12 @@ go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
|||||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||||
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
|
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
|
||||||
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
|
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
|
||||||
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
|
||||||
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
|
golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus=
|
||||||
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
|
golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM=
|
||||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||||
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package oidc_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"git.d-ma.be/mathias/tapir/internal/web/oidc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testConfig(issuer string) oidc.Config {
|
||||||
|
return oidc.Config{
|
||||||
|
Issuer: issuer,
|
||||||
|
ClientID: testClientID,
|
||||||
|
ClientSecret: "test-client-secret",
|
||||||
|
RedirectURL: "http://tapir.test/auth/callback",
|
||||||
|
SessionSecret: "test-session-secret-please-change",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #59: a discovery 503 at start-up is retried instead of failing New.
|
||||||
|
func TestNewRetriesDiscoveryAfter503UntilIssuerAnswers(t *testing.T) {
|
||||||
|
f := newFakeIssuer(t)
|
||||||
|
f.unavailableFor = 2
|
||||||
|
|
||||||
|
auth, err := oidc.New(context.Background(), testConfig(f.server.URL),
|
||||||
|
oidc.WithDiscoveryRetry(5, time.Millisecond))
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, auth)
|
||||||
|
require.Equal(t, int32(3), f.discoveryHits.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewReturnsDiscoveryErrorAfterAttemptsExhausted(t *testing.T) {
|
||||||
|
f := newFakeIssuer(t)
|
||||||
|
f.unavailableFor = 1000
|
||||||
|
|
||||||
|
_, err := oidc.New(context.Background(), testConfig(f.server.URL),
|
||||||
|
oidc.WithDiscoveryRetry(3, time.Millisecond))
|
||||||
|
|
||||||
|
require.ErrorContains(t, err, "503")
|
||||||
|
require.Equal(t, int32(3), f.discoveryHits.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewStopsDiscoveryBackoffWhenContextCancelled(t *testing.T) {
|
||||||
|
f := newFakeIssuer(t)
|
||||||
|
f.unavailableFor = 1000
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
_, err := oidc.New(ctx, testConfig(f.server.URL),
|
||||||
|
oidc.WithDiscoveryRetry(5, time.Hour))
|
||||||
|
|
||||||
|
require.True(t, errors.Is(err, context.DeadlineExceeded), "err = %v", err)
|
||||||
|
require.Less(t, time.Since(start), 5*time.Second)
|
||||||
|
require.Equal(t, int32(1), f.discoveryHits.Load())
|
||||||
|
}
|
||||||
+70
-20
@@ -57,6 +57,10 @@ const (
|
|||||||
pendingTTL = 10 * time.Minute
|
pendingTTL = 10 * time.Minute
|
||||||
sessionCookie = "tapir_session"
|
sessionCookie = "tapir_session"
|
||||||
loginPath = "/auth/login"
|
loginPath = "/auth/login"
|
||||||
|
|
||||||
|
// Discovery retry (#59): 5 tries, 2+4+8+16 s back-off ≈ 30 s, then fail.
|
||||||
|
defaultDiscoveryAttempts = 5
|
||||||
|
defaultDiscoveryDelay = 2 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// DexAuth implements web.Auth against Dex.
|
// DexAuth implements web.Auth against Dex.
|
||||||
@@ -70,6 +74,9 @@ type DexAuth struct {
|
|||||||
sessionTTL time.Duration
|
sessionTTL time.Duration
|
||||||
now func() time.Time
|
now func() time.Time
|
||||||
insecure bool // tests only: drop the Secure attribute so cookies flow over http
|
insecure bool // tests only: drop the Secure attribute so cookies flow over http
|
||||||
|
|
||||||
|
discoveryAttempts int
|
||||||
|
discoveryDelay time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ web.Auth = (*DexAuth)(nil)
|
var _ web.Auth = (*DexAuth)(nil)
|
||||||
@@ -96,14 +103,55 @@ func WithSessionTTL(ttl time.Duration) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithDiscoveryRetry overrides the discovery attempt count and the initial
|
||||||
|
// back-off delay (doubled after each failed attempt).
|
||||||
|
func WithDiscoveryRetry(attempts int, delay time.Duration) Option {
|
||||||
|
return func(d *DexAuth) {
|
||||||
|
if attempts > 0 {
|
||||||
|
d.discoveryAttempts = attempts
|
||||||
|
}
|
||||||
|
if delay > 0 {
|
||||||
|
d.discoveryDelay = delay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithInsecureCookies drops the Secure cookie attribute. TEST ONLY — it lets
|
// WithInsecureCookies drops the Secure cookie attribute. TEST ONLY — it lets
|
||||||
// the session cookie travel over plain http; never use it in production.
|
// the session cookie travel over plain http; never use it in production.
|
||||||
func WithInsecureCookies() Option {
|
func WithInsecureCookies() Option {
|
||||||
return func(d *DexAuth) { d.insecure = true }
|
return func(d *DexAuth) { d.insecure = true }
|
||||||
}
|
}
|
||||||
|
|
||||||
// New discovers the issuer and returns a configured DexAuth. ctx bounds the
|
// discover runs OIDC discovery up to attempts times, doubling delay between
|
||||||
// discovery request only.
|
// tries (#59: an issuer briefly unavailable behind its proxy answered 503 and
|
||||||
|
// serve exited). A cancelled ctx ends the back-off immediately with ctx's error.
|
||||||
|
func discover(ctx context.Context, issuer string, attempts int, delay time.Duration) (*oidc.Provider, error) {
|
||||||
|
var lastErr error
|
||||||
|
for i := range attempts {
|
||||||
|
if i > 0 {
|
||||||
|
t := time.NewTimer(delay)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Stop()
|
||||||
|
return nil, fmt.Errorf("after %d attempts (last: %v): %w", i, lastErr, ctx.Err())
|
||||||
|
case <-t.C:
|
||||||
|
}
|
||||||
|
delay *= 2
|
||||||
|
}
|
||||||
|
p, err := oidc.NewProvider(ctx, issuer)
|
||||||
|
if err == nil {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil, fmt.Errorf("after %d attempts: %w", i+1, ctx.Err())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("after %d attempts: %w", attempts, lastErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New discovers the issuer and returns a configured DexAuth. ctx bounds discovery,
|
||||||
|
// including its retry back-off.
|
||||||
func New(ctx context.Context, cfg Config, opts ...Option) (*DexAuth, error) {
|
func New(ctx context.Context, cfg Config, opts ...Option) (*DexAuth, error) {
|
||||||
for name, val := range map[string]string{
|
for name, val := range map[string]string{
|
||||||
"issuer": cfg.Issuer,
|
"issuer": cfg.Issuer,
|
||||||
@@ -117,29 +165,31 @@ func New(ctx context.Context, cfg Config, opts ...Option) (*DexAuth, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
provider, err := oidc.NewProvider(ctx, cfg.Issuer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("oidc: discover issuer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := &DexAuth{
|
d := &DexAuth{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
verifier: provider.Verifier(&oidc.Config{ClientID: cfg.ClientID}),
|
pending: newPendingStore(),
|
||||||
oauth: &oauth2.Config{
|
secret: []byte(cfg.SessionSecret),
|
||||||
ClientID: cfg.ClientID,
|
sessionTTL: defaultSessionTTL,
|
||||||
ClientSecret: cfg.ClientSecret,
|
now: time.Now,
|
||||||
Endpoint: provider.Endpoint(),
|
discoveryAttempts: defaultDiscoveryAttempts,
|
||||||
RedirectURL: cfg.RedirectURL,
|
discoveryDelay: defaultDiscoveryDelay,
|
||||||
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
|
|
||||||
},
|
|
||||||
pending: newPendingStore(),
|
|
||||||
secret: []byte(cfg.SessionSecret),
|
|
||||||
sessionTTL: defaultSessionTTL,
|
|
||||||
now: time.Now,
|
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(d)
|
o(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provider, err := discover(ctx, cfg.Issuer, d.discoveryAttempts, d.discoveryDelay)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("oidc: discover issuer: %w", err)
|
||||||
|
}
|
||||||
|
d.verifier = provider.Verifier(&oidc.Config{ClientID: cfg.ClientID})
|
||||||
|
d.oauth = &oauth2.Config{
|
||||||
|
ClientID: cfg.ClientID,
|
||||||
|
ClientSecret: cfg.ClientSecret,
|
||||||
|
Endpoint: provider.Endpoint(),
|
||||||
|
RedirectURL: cfg.RedirectURL,
|
||||||
|
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
|
||||||
|
}
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -34,6 +35,11 @@ type fakeIssuer struct {
|
|||||||
sub string
|
sub string
|
||||||
email string
|
email string
|
||||||
nonce string
|
nonce string
|
||||||
|
|
||||||
|
// discoveryHits counts requests to the discovery document; the first
|
||||||
|
// unavailableFor of them answer 503 (issuer briefly down behind its proxy).
|
||||||
|
discoveryHits atomic.Int32
|
||||||
|
unavailableFor int32
|
||||||
}
|
}
|
||||||
|
|
||||||
const testKID = "test-key"
|
const testKID = "test-key"
|
||||||
@@ -47,6 +53,10 @@ func newFakeIssuer(t *testing.T) *fakeIssuer {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
|
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
if f.discoveryHits.Add(1) <= f.unavailableFor {
|
||||||
|
http.Error(w, "no available server", http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
|
}
|
||||||
writeJSON(t, w, map[string]any{
|
writeJSON(t, w, map[string]any{
|
||||||
"issuer": f.server.URL,
|
"issuer": f.server.URL,
|
||||||
"authorization_endpoint": f.server.URL + "/authorize",
|
"authorization_endpoint": f.server.URL + "/authorize",
|
||||||
|
|||||||
Reference in New Issue
Block a user