Skip to content
Prev Previous commit
Next Next commit
Add test case for invalid tokens
  • Loading branch information
kylecarbs committed Jul 31, 2022
commit 424579edeb27fa781adec1dffdf4af55fb388bb3
14 changes: 0 additions & 14 deletions coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func (api *API) userAuthMethods(rw http.ResponseWriter, _ *http.Request) {
}

func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
if api.GithubOAuth2Config == nil {
httpapi.Write(rw, http.StatusPreconditionRequired, codersdk.Response{
Message: "GitHub authentication is not enabled!",
})
return
}

state := httpmw.OAuth2(r)

oauthClient := oauth2.NewClient(r.Context(), oauth2.StaticTokenSource(state.Token))
Expand Down Expand Up @@ -227,13 +220,6 @@ type OIDCConfig struct {
}

func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
if api.OIDCConfig == nil {
httpapi.Write(rw, http.StatusPreconditionRequired, codersdk.Response{
Message: "OpenID Connect authentication is not enabled!",
})
return
}

state := httpmw.OAuth2(r)

// See the example here: https://github.com/coreos/go-oidc
Expand Down
40 changes: 40 additions & 0 deletions coderd/userauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,46 @@ func TestUserOIDC(t *testing.T) {
}
})
}

t.Run("Disabled", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
resp := oidcCallback(t, client)
require.Equal(t, http.StatusPreconditionRequired, resp.StatusCode)
})

t.Run("NoIDToken", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{
OIDCConfig: &coderd.OIDCConfig{
OAuth2Config: &oauth2Config{},
},
})
resp := oidcCallback(t, client)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
})

t.Run("BadVerify", func(t *testing.T) {
t.Parallel()
verifier := oidc.NewVerifier("", &oidc.StaticKeySet{
PublicKeys: []crypto.PublicKey{},
}, &oidc.Config{})

client := coderdtest.New(t, &coderdtest.Options{
OIDCConfig: &coderd.OIDCConfig{
OAuth2Config: &oauth2Config{
token: (&oauth2.Token{
AccessToken: "token",
}).WithExtra(map[string]interface{}{
"id_token": "invalid",
}),
},
Verifier: verifier,
},
})
resp := oidcCallback(t, client)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
})
}

// createOIDCConfig generates a new OIDCConfig that returns a static token
Expand Down