Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type TokenInfo struct {
// The error that a TokenVerifier should return if the token cannot be verified.
var ErrInvalidToken = errors.New("invalid token")

// The error that a TokenVerifier should return for OAuth-specific protocol errors.
var ErrOAuth = errors.New("oauth error")

// A TokenVerifier checks the validity of a bearer token, and extracts information
// from it. If verification fails, it should return an error that unwraps to ErrInvalidToken.
type TokenVerifier func(ctx context.Context, token string) (*TokenInfo, error)
Expand Down Expand Up @@ -88,7 +91,9 @@ func verify(ctx context.Context, verifier TokenVerifier, opts *RequireBearerToke
if errors.Is(err, ErrInvalidToken) {
return nil, err.Error(), http.StatusUnauthorized
}
// TODO: the TS SDK distinguishes another error, OAuthError, and returns a 400.
if errors.Is(err, ErrOAuth) {
return nil, err.Error(), http.StatusBadRequest
}
// Investigate how that works.
// See typescript-sdk/src/server/auth/middleware/bearerAuth.ts.
return nil, err.Error(), http.StatusInternalServerError
Expand Down
6 changes: 6 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestVerify(t *testing.T) {
return &TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
case "invalid":
return nil, ErrInvalidToken
case "oauth":
return nil, ErrOAuth
case "noexp":
return &TokenInfo{}, nil
case "expired":
Expand Down Expand Up @@ -47,6 +49,10 @@ func TestVerify(t *testing.T) {
"invalid", nil, "bearer invalid",
"invalid token", 401,
},
{
"oauth error", nil, "Bearer oauth",
"oauth error", 400,
},
{
"no expiration", nil, "Bearer noexp",
"token missing expiration", 401,
Expand Down