From c1efa9f9968b7855e00a8b2111616255a8e9da33 Mon Sep 17 00:00:00 2001 From: vdbulcke Date: Tue, 15 Mar 2022 09:19:52 +0100 Subject: [PATCH] feat: Add Issuer override option --- mockoidc.go | 39 +++++++++++++++++++++++++++++++++++++++ mockoidc_test.go | 16 ++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/mockoidc.go b/mockoidc.go index 990923f..c6f8d85 100644 --- a/mockoidc.go +++ b/mockoidc.go @@ -23,6 +23,8 @@ type MockOIDC struct { ClientID string ClientSecret string + IssuerBaseUrl string + AccessTTL time.Duration RefreshTTL time.Duration @@ -225,7 +227,14 @@ func (m *MockOIDC) Issuer() string { if m.Server == nil { return "" } + + // Generate the Issue from IssuerBaseUrl and the root path /oidc + if m.IssuerBaseUrl != "" { + return m.IssuerBaseUrl + IssuerBase + } + return m.Addr() + IssuerBase + } // DiscoveryEndpoint returns the full `/.well-known/openid-configuration` URL @@ -233,6 +242,12 @@ func (m *MockOIDC) DiscoveryEndpoint() string { if m.Server == nil { return "" } + + // Generate the Issue from IssuerBaseUrl + if m.IssuerBaseUrl != "" { + return m.IssuerBaseUrl + DiscoveryEndpoint + } + return m.Addr() + DiscoveryEndpoint } @@ -241,6 +256,12 @@ func (m *MockOIDC) AuthorizationEndpoint() string { if m.Server == nil { return "" } + + // Generate the Issue from IssuerBaseUrl + if m.IssuerBaseUrl != "" { + return m.IssuerBaseUrl + AuthorizationEndpoint + } + return m.Addr() + AuthorizationEndpoint } @@ -249,6 +270,12 @@ func (m *MockOIDC) TokenEndpoint() string { if m.Server == nil { return "" } + + // Generate the Issue from IssuerBaseUrl + if m.IssuerBaseUrl != "" { + return m.IssuerBaseUrl + TokenEndpoint + } + return m.Addr() + TokenEndpoint } @@ -257,6 +284,12 @@ func (m *MockOIDC) UserinfoEndpoint() string { if m.Server == nil { return "" } + + // Generate the Issue from IssuerBaseUrl + if m.IssuerBaseUrl != "" { + return m.IssuerBaseUrl + UserinfoEndpoint + } + return m.Addr() + UserinfoEndpoint } @@ -265,6 +298,12 @@ func (m *MockOIDC) JWKSEndpoint() string { if m.Server == nil { return "" } + + // Generate the Issue from IssuerBaseUrl + if m.IssuerBaseUrl != "" { + return m.IssuerBaseUrl + JWKSEndpoint + } + return m.Addr() + JWKSEndpoint } diff --git a/mockoidc_test.go b/mockoidc_test.go index 6dfd599..1c097ad 100644 --- a/mockoidc_test.go +++ b/mockoidc_test.go @@ -243,6 +243,22 @@ func TestMockOIDC_Config(t *testing.T) { assert.Equal(t, m.CodeChallengeMethodsSupported, cfg.CodeChallengeMethodsSupported) } +func TestMockOIDC_ConfigBaseIssuer(t *testing.T) { + m, err := mockoidc.Run() + assert.NoError(t, err) + defer m.Shutdown() + + m.IssuerBaseUrl = "https://idp.oidc.proxy.com" + + cfg := m.Config() + assert.Equal(t, m.ClientID, cfg.ClientID) + assert.Equal(t, m.ClientSecret, cfg.ClientSecret) + assert.Equal(t, m.Issuer(), cfg.Issuer) + assert.Equal(t, m.AccessTTL, cfg.AccessTTL) + assert.Equal(t, m.RefreshTTL, cfg.RefreshTTL) + assert.Equal(t, m.CodeChallengeMethodsSupported, cfg.CodeChallengeMethodsSupported) +} + func TestMockOIDC_QueueError(t *testing.T) { m, err := mockoidc.Run() assert.NoError(t, err)