Skip to content

Commit f6028f8

Browse files
authored
Merge pull request #51 from philipgough/oidc-check
Handle correct response code for token expired error
2 parents 5509cb3 + b3f5e32 commit f6028f8

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

authentication/authentication_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package authentication
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"testing"
8+
"time"
79

10+
"github.com/coreos/go-oidc/v3/oidc"
811
"github.com/go-kit/log"
912
grpc_middleware_auth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
1013
"github.com/mitchellh/mapstructure"
@@ -109,3 +112,63 @@ func TestNewAuthentication(t *testing.T) {
109112
}
110113
})
111114
}
115+
116+
func TestTokenExpiredErrorHandling(t *testing.T) {
117+
// Test the error handling logic for TokenExpiredError
118+
t.Run("TokenExpiredError is correctly identified", func(t *testing.T) {
119+
// Create a TokenExpiredError
120+
expiredErr := &oidc.TokenExpiredError{
121+
Expiry: time.Now().Add(-time.Hour), // Expired an hour ago
122+
}
123+
124+
// Test direct error
125+
var tokenExpiredErr *oidc.TokenExpiredError
126+
if !errors.As(expiredErr, &tokenExpiredErr) {
127+
t.Error("errors.As should identify TokenExpiredError")
128+
}
129+
130+
// Test wrapped error
131+
wrappedErr := &wrappedError{
132+
msg: "verification failed",
133+
err: expiredErr,
134+
}
135+
136+
if !errors.As(wrappedErr, &tokenExpiredErr) {
137+
t.Error("errors.As should identify wrapped TokenExpiredError")
138+
}
139+
})
140+
141+
t.Run("Other errors are not identified as TokenExpiredError", func(t *testing.T) {
142+
// Test with a generic error
143+
genericErr := errors.New("generic verification error")
144+
145+
var tokenExpiredErr *oidc.TokenExpiredError
146+
if errors.As(genericErr, &tokenExpiredErr) {
147+
t.Error("errors.As should not identify generic error as TokenExpiredError")
148+
}
149+
150+
// Test with wrapped generic error
151+
wrappedGenericErr := &wrappedError{
152+
msg: "verification failed",
153+
err: genericErr,
154+
}
155+
156+
if errors.As(wrappedGenericErr, &tokenExpiredErr) {
157+
t.Error("errors.As should not identify wrapped generic error as TokenExpiredError")
158+
}
159+
})
160+
}
161+
162+
// Helper type to wrap errors for testing
163+
type wrappedError struct {
164+
msg string
165+
err error
166+
}
167+
168+
func (e *wrappedError) Error() string {
169+
return e.msg
170+
}
171+
172+
func (e *wrappedError) Unwrap() error {
173+
return e.err
174+
}

authentication/oidc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"encoding/pem"
8+
"errors"
89
"fmt"
910
"net"
1011
"net/http"
@@ -390,6 +391,11 @@ func (a oidcAuthenticator) checkAuth(ctx context.Context, token string) (context
390391
// We log it to allow the possibility of debugging this.
391392
level.Debug(a.logger).Log("msg", msg, "err", err)
392393

394+
var tokenExpiredErr *oidc.TokenExpiredError
395+
if errors.As(err, &tokenExpiredErr) {
396+
return ctx, "token is expired", http.StatusForbidden, codes.Unauthenticated
397+
}
398+
393399
// The original HTTP implementation returned StatusInternalServerError.
394400
// For gRPC we return Unknown, as we can't really
395401
// be sure the problem is internal and not deserving Unauthenticated or InvalidArgument.

0 commit comments

Comments
 (0)