Skip to content

Commit

Permalink
feat: export token expiration error
Browse files Browse the repository at this point in the history
try to match style

follow naming convention

tests

revert import change

match format

naming convention

test

errors.As instead of errors.Is
  • Loading branch information
crenshaw-dev authored and ericchiang committed Jun 27, 2022
1 parent 26c5037 commit 2cafe18
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 13 additions & 1 deletion oidc/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const (
issuerGoogleAccountsNoScheme = "accounts.google.com"
)

// TokenExpiredError indicates that Verify failed because the token was expired. This
// error does NOT indicate that the token is not also invalid for other reasons. Other
// checks might have failed if the expiration check had not failed.
type TokenExpiredError struct {
// Expiry is the time when the token expired.
Expiry time.Time
}

func (e *TokenExpiredError) Error() string {
return fmt.Sprintf("oidc: token is expired (Token Expiry: %v)", e.Expiry)
}

// KeySet is a set of publc JSON Web Keys that can be used to validate the signature
// of JSON web tokens. This is expected to be backed by a remote key set through
// provider metadata discovery or an in-memory set of keys delivered out-of-band.
Expand Down Expand Up @@ -260,7 +272,7 @@ func (v *IDTokenVerifier) Verify(ctx context.Context, rawIDToken string) (*IDTok
nowTime := now()

if t.Expiry.Before(nowTime) {
return nil, fmt.Errorf("oidc: token is expired (Token Expiry: %v)", t.Expiry)
return nil, &TokenExpiredError{Expiry: t.Expiry}
}

// If nbf claim is provided in token, ensure that it is indeed in the past.
Expand Down
17 changes: 11 additions & 6 deletions oidc/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oidc
import (
"context"
"crypto"
"errors"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -72,8 +73,8 @@ func TestVerify(t *testing.T) {
config: Config{
SkipClientIDCheck: true,
},
signKey: newRSAKey(t),
wantErr: true,
signKey: newRSAKey(t),
wantErrAs: &TokenExpiredError{},
},
{
name: "unexpired token",
Expand Down Expand Up @@ -530,8 +531,9 @@ type verificationTest struct {
// testing invalid signatures.
verificationKey *signingKey

config Config
wantErr bool
config Config
wantErr bool
wantErrAs error
}

func (v verificationTest) runGetToken(t *testing.T) (*IDToken, error) {
Expand All @@ -557,10 +559,13 @@ func (v verificationTest) runGetToken(t *testing.T) (*IDToken, error) {

func (v verificationTest) run(t *testing.T) {
_, err := v.runGetToken(t)
if err != nil && !v.wantErr {
if err != nil && !v.wantErr && v.wantErrAs == nil {
t.Errorf("%v", err)
}
if err == nil && v.wantErr {
if err == nil && (v.wantErr || v.wantErrAs != nil) {
t.Errorf("expected error")
}
if v.wantErrAs != nil && !errors.As(err, &v.wantErrAs) {
t.Errorf("expected error %q but got %q", v.wantErrAs, err)
}
}

0 comments on commit 2cafe18

Please sign in to comment.