Skip to content

Commit 36340e6

Browse files
committed
chore: account for forbidden as well as unauthorized response codes
1 parent 9995a0c commit 36340e6

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

coderd/coderdtest/oidctest/idp.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,9 @@ type ExternalAuthConfigOptions struct {
12551255
// ValidatePayload is the payload that is used when the user calls the
12561256
// equivalent of "userinfo" for oauth2. This is not standardized, so is
12571257
// different for each provider type.
1258-
ValidatePayload func(email string) (interface{}, error)
1258+
//
1259+
// The int,error payload can control the response if set.
1260+
ValidatePayload func(email string) (interface{}, int, error)
12591261

12601262
// routes is more advanced usage. This allows the caller to
12611263
// completely customize the response. It captures all routes under the /external-auth-validate/*
@@ -1293,10 +1295,19 @@ func (f *FakeIDP) ExternalAuthConfig(t testing.TB, id string, custom *ExternalAu
12931295
var payload interface{} = "OK"
12941296
if custom.ValidatePayload != nil {
12951297
var err error
1296-
payload, err = custom.ValidatePayload(email)
1298+
var code int
1299+
payload, code, err = custom.ValidatePayload(email)
1300+
if code == 0 && err == nil {
1301+
code = http.StatusOK
1302+
}
1303+
if code == 0 && err != nil {
1304+
code = http.StatusUnauthorized
1305+
}
12971306
if err != nil {
1298-
http.Error(rw, fmt.Sprintf("failed validation via custom method: %s", err.Error()), http.StatusBadRequest)
1307+
http.Error(rw, fmt.Sprintf("failed validation via custom method: %s", err.Error()), code)
1308+
return
12991309
}
1310+
rw.WriteHeader(code)
13001311
}
13011312
_ = json.NewEncoder(rw).Encode(payload)
13021313
default:

coderd/externalauth/externalauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (c *Config) ValidateToken(ctx context.Context, link *oauth2.Token) (bool, *
218218
return false, nil, err
219219
}
220220
defer res.Body.Close()
221-
if res.StatusCode == http.StatusUnauthorized {
221+
if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden {
222222
// The token is no longer valid!
223223
return false, nil, nil
224224
}

coderd/externalauth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ func TestExternalAuthByID(t *testing.T) {
7979
client := coderdtest.New(t, &coderdtest.Options{
8080
ExternalAuthConfigs: []*externalauth.Config{
8181
fake.ExternalAuthConfig(t, providerID, &oidctest.ExternalAuthConfigOptions{
82-
ValidatePayload: func(_ string) (interface{}, error) {
82+
ValidatePayload: func(_ string) (interface{}, int, error) {
8383
return github.User{
8484
Login: github.String("kyle"),
8585
AvatarURL: github.String("https://avatars.githubusercontent.com/u/12345678?v=4"),
86-
}, nil
86+
}, 0, nil
8787
},
8888
}, func(cfg *externalauth.Config) {
8989
cfg.Type = codersdk.EnhancedExternalAuthProviderGitHub.String()
@@ -108,11 +108,11 @@ func TestExternalAuthByID(t *testing.T) {
108108

109109
// routes includes a route for /install that returns a list of installations
110110
routes := (&oidctest.ExternalAuthConfigOptions{
111-
ValidatePayload: func(_ string) (interface{}, error) {
111+
ValidatePayload: func(_ string) (interface{}, int, error) {
112112
return github.User{
113113
Login: github.String("kyle"),
114114
AvatarURL: github.String("https://avatars.githubusercontent.com/u/12345678?v=4"),
115-
}, nil
115+
}, 0, nil
116116
},
117117
}).AddRoute("/installs", func(_ string, rw http.ResponseWriter, r *http.Request) {
118118
httpapi.Write(r.Context(), rw, http.StatusOK, struct {

coderd/workspacebuilds_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,13 @@ func TestWorkspaceDeleteSuspendedUser(t *testing.T) {
724724
IncludeProvisionerDaemon: true,
725725
ExternalAuthConfigs: []*externalauth.Config{
726726
fake.ExternalAuthConfig(t, providerID, &oidctest.ExternalAuthConfigOptions{
727-
ValidatePayload: func(email string) (interface{}, error) {
727+
ValidatePayload: func(email string) (interface{}, int, error) {
728728
validateCalls++
729729
if userSuspended {
730730
// Simulate the user being suspended from the IDP too.
731-
return "", fmt.Errorf("user is suspended")
731+
return "", http.StatusForbidden, fmt.Errorf("user is suspended")
732732
}
733-
return "OK", nil
733+
return "OK", 0, nil
734734
},
735735
}),
736736
},
@@ -782,7 +782,7 @@ func TestWorkspaceDeleteSuspendedUser(t *testing.T) {
782782
Transition: codersdk.WorkspaceTransitionDelete,
783783
})
784784
require.NoError(t, err)
785-
build = coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build.ID)
785+
build = coderdtest.AwaitWorkspaceBuildJobCompleted(t, owner, build.ID)
786786
require.Equal(t, 2, validateCalls)
787787
require.Equal(t, codersdk.WorkspaceStatusDeleted, build.Status)
788788
}

0 commit comments

Comments
 (0)