forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve token OIDC logging (supabase#1606)
## What kind of change does this PR introduce? * Currently, when the "Unacceptable audience in id_token" error is returned, it doesn't log the audience claim from the id token, which makes it hard to debug. The audience claim from the id token is now logged as well when this error is returned. * Adds a basic test for the generic id token oidc `getProvider()` method, since we currently have 0 coverage for this file * The test also uncovered a possible nil pointer panic in the case of the generic OIDC provider being returned since in the generic case, the `oauthConfig` will be nil. Rather than returning the `oauthConfig`, we only need to return the `skipNonceCheck` property since we only check for that.
- Loading branch information
1 parent
24cf102
commit d324df7
Showing
2 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/supabase/auth/internal/conf" | ||
) | ||
|
||
type TokenOIDCTestSuite struct { | ||
suite.Suite | ||
API *API | ||
Config *conf.GlobalConfiguration | ||
} | ||
|
||
func TestTokenOIDC(t *testing.T) { | ||
api, config, err := setupAPIForTest() | ||
require.NoError(t, err) | ||
|
||
ts := &TokenOIDCTestSuite{ | ||
API: api, | ||
Config: config, | ||
} | ||
defer api.db.Close() | ||
|
||
suite.Run(t, ts) | ||
} | ||
|
||
func SetupTestOIDCProvider(ts *TokenOIDCTestSuite) *httptest.Server { | ||
var server *httptest.Server | ||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/.well-known/openid-configuration": | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"issuer":"` + server.URL + `","authorization_endpoint":"` + server.URL + `/authorize","token_endpoint":"` + server.URL + `/token","jwks_uri":"` + server.URL + `/jwks"}`)) | ||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
})) | ||
return server | ||
} | ||
|
||
func (ts *TokenOIDCTestSuite) TestGetProvider() { | ||
server := SetupTestOIDCProvider(ts) | ||
defer server.Close() | ||
|
||
params := &IdTokenGrantParams{ | ||
IdToken: "test-id-token", | ||
AccessToken: "test-access-token", | ||
Nonce: "test-nonce", | ||
Provider: server.URL, | ||
ClientID: "test-client-id", | ||
Issuer: server.URL, | ||
} | ||
|
||
ts.Config.External.AllowedIdTokenIssuers = []string{server.URL} | ||
|
||
req := httptest.NewRequest(http.MethodPost, "http://localhost", nil) | ||
oidcProvider, skipNonceCheck, providerType, acceptableClientIds, err := params.getProvider(context.Background(), ts.Config, req) | ||
require.NoError(ts.T(), err) | ||
require.NotNil(ts.T(), oidcProvider) | ||
require.False(ts.T(), skipNonceCheck) | ||
require.Equal(ts.T(), params.Provider, providerType) | ||
require.NotEmpty(ts.T(), acceptableClientIds) | ||
} |