@@ -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"
@@ -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+ }
0 commit comments