Skip to content

Commit

Permalink
feat(oidc): return defined error when discovery failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ay4toh5i committed Sep 16, 2024
1 parent b555396 commit aadfd30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Discover(ctx context.Context, issuer string, httpClient *http.Client, wellK
discoveryConfig := new(oidc.DiscoveryConfiguration)
err = httphelper.HttpRequest(httpClient, req, &discoveryConfig)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %w", oidc.ErrDiscoveryFailed, err)
}
if logger, ok := logging.FromContext(ctx); ok {
logger.Debug("discover", "config", discoveryConfig)
Expand Down
20 changes: 16 additions & 4 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/oidc/v3/pkg/oidc"
)

func TestDiscover(t *testing.T) {
Expand All @@ -22,7 +23,7 @@ func TestDiscover(t *testing.T) {
name string
args args
wantFields *wantFields
wantErr bool
wantErr error
}{
{
name: "spotify", // https://github.com/zitadel/oidc/issues/406
Expand All @@ -32,16 +33,27 @@ func TestDiscover(t *testing.T) {
wantFields: &wantFields{
UILocalesSupported: true,
},
wantErr: false,
wantErr: nil,
},
{
name: "discovery failed",
args: args{
issuer: "https://example.com",
},
wantFields: &wantFields{
UILocalesSupported: true,
},
wantErr: oidc.ErrDiscoveryFailed,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Discover(context.Background(), tt.args.issuer, http.DefaultClient, tt.args.wellKnownUrl...)
if tt.wantErr {
assert.Error(t, err)
if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
return
}

require.NoError(t, err)
if tt.wantFields == nil {
return
Expand Down
1 change: 1 addition & 0 deletions pkg/oidc/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type IDClaims interface {

var (
ErrParse = errors.New("parsing of request failed")
ErrDiscoveryFailed = errors.New("OpenID Provider Configuration Discovery is failed")
ErrIssuerInvalid = errors.New("issuer does not match")
ErrSubjectMissing = errors.New("subject missing")
ErrAudience = errors.New("audience is not valid")
Expand Down

0 comments on commit aadfd30

Please sign in to comment.