Skip to content

Commit 1250a31

Browse files
authored
auth: add OAuth error handling in TokenVerifier and tests (#399)
Add ErrOAuth error type and handling to match TypeScript SDK behavior. OAuth protocol errors now return HTTP 400 instead of 500, providing better error classification for authentication issues. Changes - Add ErrOAuth variable for OAuth-specific protocol errors - Update verify function to return 400 for OAuth errors - Add test case for OAuth error handling Fixes compatibility with TypeScript SDK error handling patterns.
1 parent 1c20560 commit 1250a31

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

auth/auth.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type TokenInfo struct {
2424
// The error that a TokenVerifier should return if the token cannot be verified.
2525
var ErrInvalidToken = errors.New("invalid token")
2626

27+
// The error that a TokenVerifier should return for OAuth-specific protocol errors.
28+
var ErrOAuth = errors.New("oauth error")
29+
2730
// A TokenVerifier checks the validity of a bearer token, and extracts information
2831
// from it. If verification fails, it should return an error that unwraps to ErrInvalidToken.
2932
type TokenVerifier func(ctx context.Context, token string) (*TokenInfo, error)
@@ -88,7 +91,9 @@ func verify(ctx context.Context, verifier TokenVerifier, opts *RequireBearerToke
8891
if errors.Is(err, ErrInvalidToken) {
8992
return nil, err.Error(), http.StatusUnauthorized
9093
}
91-
// TODO: the TS SDK distinguishes another error, OAuthError, and returns a 400.
94+
if errors.Is(err, ErrOAuth) {
95+
return nil, err.Error(), http.StatusBadRequest
96+
}
9297
// Investigate how that works.
9398
// See typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
9499
return nil, err.Error(), http.StatusInternalServerError

auth/auth_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func TestVerify(t *testing.T) {
1919
return &TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
2020
case "invalid":
2121
return nil, ErrInvalidToken
22+
case "oauth":
23+
return nil, ErrOAuth
2224
case "noexp":
2325
return &TokenInfo{}, nil
2426
case "expired":
@@ -47,6 +49,10 @@ func TestVerify(t *testing.T) {
4749
"invalid", nil, "bearer invalid",
4850
"invalid token", 401,
4951
},
52+
{
53+
"oauth error", nil, "Bearer oauth",
54+
"oauth error", 400,
55+
},
5056
{
5157
"no expiration", nil, "Bearer noexp",
5258
"token missing expiration", 401,

0 commit comments

Comments
 (0)