Skip to content

fix(api): return 401 for invalid or revoked API tokens - #1314

Merged
slothful-vassal merged 3 commits into
open-wanderer:devfrom
snowyukitty:fix/api-token-authentication-status
Oct 4, 2026
Merged

slothful-vassal merged 3 commits into
open-wanderer:devfrom
snowyukitty:fix/api-token-authentication-status

Conversation

@snowyukitty

Copy link
Copy Markdown
Contributor

Fixes #1309.

Unknown or revoked Wanderer API tokens currently turn PocketBase's credential rejection into HTTP 500. Clients such as Gadgetbridge then see a server failure instead of an authentication failure.

Return JSON HTTP 401 with message: "invalid_token" and WWW-Authenticate: Bearer error="invalid_token" for that specific rejection. Unrelated backend 400/404/500 responses and connection failures retain their server-error handling. This change is limited to invalid/revoked Wanderer API tokens.

Tested on Linux with Node 24 and the locked dependencies:

  • npm run test:unit -- --run: 278 passed, 11 existing engine tests skipped by their environment guard; nine new hook cases use the real PocketBase SDK and loopback HTTP.
  • npm run check: zero errors or warnings.
  • npm run build: successful.
  • Built-server HTTP comparison against a disposable PocketBase SQLite app running the unchanged AuthToken handler: unknown and actually revoked tokens return 500 on the unpatched dev base and JSON 401 on this patch. Valid-token and cookie authentication, public access, missing-token upload rejection, backend 500, generic backend 404, and connection failure were also checked.

To reproduce, request /api/v1/api-token with Authorization: Bearer wanderer_key_bogus. The response is now JSON 401 rather than 500.

@slothful-vassal

Copy link
Copy Markdown
Contributor

Thanks a lot for the fix and the thorough tests!

I pushed one small follow-up commit on top: /auth/token now answers 401 for unknown/revoked and expired tokens, and the hook checks e.status === 401 instead of matching the error message. Two reasons:

  • Expired tokens (expiration is user-settable) still came back as 400 → 500, which is the same problem for clients like Gadgetbridge.
  • Matching on "Invalid or revoked API token." would silently break if the backend message were ever reworded; the trailing period is even added by PocketBase, not our code.

A missing endpoint (404) or any other backend failure still results in a 500, as before. I extended your hook tests with the expired case and added a Go test for the handler's status codes.

The bearer prefix/case handling from the issue's optional point is left for a separate change.

slothful-vassal
slothful-vassal previously approved these changes Oct 4, 2026
@slothful-vassal
slothful-vassal merged commit 64f94bb into open-wanderer:dev Oct 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An invalid or revoked API token returns 500 instead of 401

2 participants