Skip to content

Commit 2286142

Browse files
committed
Merge branch 'main' into token-ver-req
2 parents 067f241 + 1250a31 commit 2286142

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
// The HTTP request is provided in case verifying the token involves checking it.
@@ -92,7 +95,9 @@ func verify(req *http.Request, verifier TokenVerifier, opts *RequireBearerTokenO
9295
if errors.Is(err, ErrInvalidToken) {
9396
return nil, err.Error(), http.StatusUnauthorized
9497
}
95-
// TODO: the TS SDK distinguishes another error, OAuthError, and returns a 400.
98+
if errors.Is(err, ErrOAuth) {
99+
return nil, err.Error(), http.StatusBadRequest
100+
}
96101
// Investigate how that works.
97102
// See typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
98103
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)