feat(auth): per-request backend JWT + E2E round-trip (#2770)#2877
Draft
piyalbasu wants to merge 8 commits into
Draft
feat(auth): per-request backend JWT + E2E round-trip (#2770)#2877piyalbasu wants to merge 8 commits into
piyalbasu wants to merge 8 commits into
Conversation
Contributor
|
PR Preview build is ready: https://github.com/stellar/freighter/releases/tag/untagged-772481c2a5d2d2e7b910 (SDF collaborators only — install instructions in the release description) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
Builds the client side of Freighter's stateless backend auth: a function that mints a fresh, short-lived signed token for each request to
freighter-backend-v2, signed with the anonymous auth key derived in #2769 — so the backend can verify who's calling without the wallet ever doing a signing prompt or revealing its Stellar address. Includes a tiny request wrapper that retries once if a token is rejected, and a runnable script to prove the whole round-trip works against a local backend.This is the piece that lets you test the auth middleware end-to-end (#2770). It's still scoped narrowly: it does not yet wire auth into the real contacts requests — that's a later ticket. Nothing user-facing changes yet.
Implementation details (for agents/reviewers)
What changed (all new, under
@shared/api/helpers/+ one script):buildAuthJwt.ts— mints a compact EdDSA JWS per request. Header{alg:"EdDSA",typ:"JWT"}; claimssub(lowercase-hex auth pubkey = userId),iss("freighter-extension"),iat,exp(iat+15s),bodyHash(hex SHA-256 of raw body viacrypto.subtle; empty-bytes hash for no body),methodAndPath("<METHOD> <path>", method upper-cased, path includes query — bound to the server'sr.URL.RequestURI()). Signsb64url(header).b64url(payload)withKeypair.sign(); url-safe base64, no padding.nowis injectable for deterministic tests.authedFetch.ts— attachesAuthorization: Bearer <jwt>, builds a fresh JWT per call, and on401rebuilds a fresh JWT and retries exactly once.Content-Type: application/jsondefault for non-GET (caller can override);baseUrltrailing slash normalized so the fetched URL can't diverge from the signedmethodAndPath.scripts/auth-e2e.ts— standalone round-trip check. Derives a keypair from a test mnemonic ([Extension] Derive auth keypair from seed for backend authentication #2769 vector → known userId) and hitsGET /api/v1/auth/whoami, asserting: valid→200+matching userId; tampered bodyHash→401; flipped-signature→401; expired→401. Per-case PASS/FAIL, non-zero exit on any failure.@shared/api/helpers/__tests__/buildAuthJwt.test.ts(7),authedFetch.test.ts(6) — unit tests.Why a script (not a jest integration test): chosen deliberately for a manual end-to-end check against a locally-run backend.
whoamiis GET-only, so the four failure cases are mapped onto it: tampered body = a JWT carrying a non-emptybodyHashwhile the GET body is empty; wrong key = a flipped signature byte; expired = a backdatednow.How to run the E2E round-trip:
Runs via
npx tsxby design — no committed dependency (version pinned in the script's usage comment).Verification:
yarn jest @shared/api→ 10 suites / 76 tests pass underjest-fixed-jsdom(real WebCrypto). The script runs end-to-end up to the network call without a backend (derives the expected userId, then reports four FAILs + a friendly hint, exit 1). Reviewed per-task + a whole-branch review.Notes:
iatis whole-seconds and Ed25519 is deterministic, so within-second retries are byte-identical — that assertion would be flaky and the property is behaviorally moot. The "fresh per request" guarantee is structural (buildAuthJwtis rebuilt inside the retry).translation.jsonkeys in the diff are the huskyi18next-scannerhook backfilling a pre-existingmastergap — unrelated to this feature.Follow-ups (out of scope): wire
authedFetchinto the real background contacts path; optionally promote the script to a gated/CI integration test;freighter-mobilebuilds the equivalent against the same contract.