Skip to content

Commit 1ed27ab

Browse files
committed
Remove providerid from places it is not needed
1 parent f50e346 commit 1ed27ab

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

coderd/coderdtest/oidctest/idp.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ type FakeIDP struct {
5050
// clientID to be used by coderd
5151
clientID string
5252
clientSecret string
53-
logger slog.Logger
53+
// externalProviderID is optional to match the provider in coderd for
54+
// redirectURLs.
55+
externalProviderID string
56+
logger slog.Logger
5457
// externalAuthValidate will be called when the user tries to validate their
5558
// external auth. The fake IDP will reject any invalid tokens, so this just
5659
// controls the response payload after a successfully authed token.
57-
externalAuthValidate map[string]func(email string, rw http.ResponseWriter, r *http.Request)
60+
externalAuthValidate func(email string, rw http.ResponseWriter, r *http.Request)
5861

5962
// These maps are used to control the state of the IDP.
6063
// That is the various access tokens, refresh tokens, states, etc.
@@ -197,7 +200,6 @@ func NewFakeIDP(t testing.TB, opts ...FakeIDPOpt) *FakeIDP {
197200
hookOnRefresh: func(_ string) error { return nil },
198201
hookUserInfo: func(email string) (jwt.MapClaims, error) { return jwt.MapClaims{}, nil },
199202
hookValidRedirectURL: func(redirectURL string) error { return nil },
200-
externalAuthValidate: make(map[string]func(email string, rw http.ResponseWriter, r *http.Request)),
201203
}
202204

203205
for _, opt := range opts {
@@ -356,8 +358,8 @@ func (f *FakeIDP) LoginWithClient(t testing.TB, client *codersdk.Client, idToken
356358

357359
// ExternalLogin does the oauth2 flow for external auth providers. This requires
358360
// an authenticated coder client.
359-
func (f *FakeIDP) ExternalLogin(t testing.TB, client *codersdk.Client, providerID string, opts ...func(r *http.Request)) *http.Response {
360-
coderOauthURL, err := client.URL.Parse(fmt.Sprintf("/external-auth/%s/callback", providerID))
361+
func (f *FakeIDP) ExternalLogin(t testing.TB, client *codersdk.Client, opts ...func(r *http.Request)) *http.Response {
362+
coderOauthURL, err := client.URL.Parse(fmt.Sprintf("/external-auth/%s/callback", f.externalProviderID))
361363
require.NoError(t, err)
362364
f.SetRedirect(t, coderOauthURL.String())
363365

@@ -713,7 +715,7 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
713715
_ = json.NewEncoder(rw).Encode(claims)
714716
}))
715717

716-
mux.Mount("/external-auth-validate/{provider-id}", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
718+
mux.Mount("/external-auth-validate/", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
717719
token, err := f.authenticateBearerTokenRequest(t, r)
718720
f.logger.Info(r.Context(), "http call idp external auth validate",
719721
slog.Error(err),
@@ -731,14 +733,13 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
731733
return
732734
}
733735

734-
id := chi.URLParam(r, "provider-id")
735-
handle, ok := f.externalAuthValidate[id]
736-
if !ok {
737-
t.Errorf("missing external auth validate handler for %s", id)
738-
http.Error(rw, fmt.Sprintf("missing external auth validate handler for %s", id), http.StatusBadRequest)
736+
if f.externalAuthValidate == nil {
737+
t.Errorf("missing external auth validate handler")
738+
http.Error(rw, "missing external auth validate handler", http.StatusBadRequest)
739739
return
740740
}
741-
handle(email, rw, r)
741+
742+
f.externalAuthValidate(email, rw, r)
742743
}))
743744

744745
mux.Handle(keysPath, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
@@ -850,7 +851,7 @@ type ExternalAuthConfigOptions struct {
850851
ValidatePayload func(email string) interface{}
851852

852853
// routes is more advanced usage. This allows the caller to
853-
// completely customize the response. It captures all routes under the /external-auth-validate/{provider-id}/*
854+
// completely customize the response. It captures all routes under the /external-auth-validate/*
854855
// so the caller can do whatever they want and even add routes.
855856
routes map[string]func(email string, rw http.ResponseWriter, r *http.Request)
856857
}
@@ -875,8 +876,8 @@ func (f *FakeIDP) ExternalAuthConfig(t testing.TB, id string, custom *ExternalAu
875876
if custom == nil {
876877
custom = &ExternalAuthConfigOptions{}
877878
}
878-
879-
f.externalAuthValidate[id] = func(email string, rw http.ResponseWriter, r *http.Request) {
879+
f.externalProviderID = id
880+
f.externalAuthValidate = func(email string, rw http.ResponseWriter, r *http.Request) {
880881
newPath := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/external-auth-validate/%s", id))
881882
switch newPath {
882883
// /user is ALWAYS supported under the `/` path too.

coderd/externalauth_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestExternalAuthByID(t *testing.T) {
6363
})
6464

6565
coderdtest.CreateFirstUser(t, client)
66-
fake.ExternalLogin(t, client, providerID)
66+
fake.ExternalLogin(t, client)
6767

6868
auth, err := client.ExternalAuthByID(context.Background(), providerID)
6969
require.NoError(t, err)
@@ -90,7 +90,7 @@ func TestExternalAuthByID(t *testing.T) {
9090

9191
coderdtest.CreateFirstUser(t, client)
9292
// Login to external auth provider
93-
fake.ExternalLogin(t, client, providerID)
93+
fake.ExternalLogin(t, client)
9494

9595
auth, err := client.ExternalAuthByID(context.Background(), providerID)
9696
require.NoError(t, err)
@@ -133,7 +133,7 @@ func TestExternalAuthByID(t *testing.T) {
133133
})
134134

135135
coderdtest.CreateFirstUser(t, client)
136-
fake.ExternalLogin(t, client, providerID)
136+
fake.ExternalLogin(t, client)
137137

138138
auth, err := client.ExternalAuthByID(context.Background(), providerID)
139139
require.NoError(t, err)

0 commit comments

Comments
 (0)