Skip to content

Commit 71f941c

Browse files
authored
Merge pull request #49 from philipgough/oidc-check
Handle correct response code for token expired error
2 parents c2feae4 + d8fe3ef commit 71f941c

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

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"
@@ -357,6 +358,11 @@ func (a oidcAuthenticator) checkAuth(ctx context.Context, token string) (context
357358
// We log it to allow the possibility of debugging this.
358359
level.Debug(a.logger).Log("msg", msg, "err", err)
359360

361+
var tokenExpiredErr *oidc.TokenExpiredError
362+
if errors.As(err, &tokenExpiredErr) {
363+
return ctx, "token is expired", http.StatusForbidden, codes.Unauthenticated
364+
}
365+
360366
// The original HTTP implementation returned StatusInternalServerError.
361367
// For gRPC we return Unknown, as we can't really
362368
// be sure the problem is internal and not deserving Unauthenticated or InvalidArgument.

0 commit comments

Comments
 (0)