@@ -2,9 +2,12 @@ package authentication
22
33import (
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"
@@ -108,3 +111,63 @@ func TestNewAuthentication(t *testing.T) {
108111 }
109112 })
110113}
114+
115+ func TestTokenExpiredErrorHandling (t * testing.T ) {
116+ // Test the error handling logic for TokenExpiredError
117+ t .Run ("TokenExpiredError is correctly identified" , func (t * testing.T ) {
118+ // Create a TokenExpiredError
119+ expiredErr := & oidc.TokenExpiredError {
120+ Expiry : time .Now ().Add (- time .Hour ), // Expired an hour ago
121+ }
122+
123+ // Test direct error
124+ var tokenExpiredErr * oidc.TokenExpiredError
125+ if ! errors .As (expiredErr , & tokenExpiredErr ) {
126+ t .Error ("errors.As should identify TokenExpiredError" )
127+ }
128+
129+ // Test wrapped error
130+ wrappedErr := & wrappedError {
131+ msg : "verification failed" ,
132+ err : expiredErr ,
133+ }
134+
135+ if ! errors .As (wrappedErr , & tokenExpiredErr ) {
136+ t .Error ("errors.As should identify wrapped TokenExpiredError" )
137+ }
138+ })
139+
140+ t .Run ("Other errors are not identified as TokenExpiredError" , func (t * testing.T ) {
141+ // Test with a generic error
142+ genericErr := errors .New ("generic verification error" )
143+
144+ var tokenExpiredErr * oidc.TokenExpiredError
145+ if errors .As (genericErr , & tokenExpiredErr ) {
146+ t .Error ("errors.As should not identify generic error as TokenExpiredError" )
147+ }
148+
149+ // Test with wrapped generic error
150+ wrappedGenericErr := & wrappedError {
151+ msg : "verification failed" ,
152+ err : genericErr ,
153+ }
154+
155+ if errors .As (wrappedGenericErr , & tokenExpiredErr ) {
156+ t .Error ("errors.As should not identify wrapped generic error as TokenExpiredError" )
157+ }
158+ })
159+ }
160+
161+ // Helper type to wrap errors for testing
162+ type wrappedError struct {
163+ msg string
164+ err error
165+ }
166+
167+ func (e * wrappedError ) Error () string {
168+ return e .msg
169+ }
170+
171+ func (e * wrappedError ) Unwrap () error {
172+ return e .err
173+ }
0 commit comments