Skip to content

feat: merchant API key auth for POST /payments and GET /payments#60

Merged
Manuel1234477 merged 2 commits into
StellarGateLabs:mainfrom
Nexha-dev:feat/api-key-auth
Jun 23, 2026
Merged

feat: merchant API key auth for POST /payments and GET /payments#60
Manuel1234477 merged 2 commits into
StellarGateLabs:mainfrom
Nexha-dev:feat/api-key-auth

Conversation

@Nexha-dev

Copy link
Copy Markdown
Contributor

Summary

Closes #9

Every write and list endpoint was unauthenticated. This PR adds Bearer token auth backed by hashed API keys, with merchant-scoped payment visibility.

Changes

src/db.rs

  • Add merchants table creation to migrate() (id, api_key_hash, created_at)
  • create_merchant(id, raw_key) — hashes key with SHA-256, stores digest only
  • find_merchant_by_key(raw_key) — hashes input and looks up matching merchant id

src/api/mod.rs

  • POST /merchants — provisions a merchant, returns one-time api_key
  • auth_middleware — validates Authorization: Bearer <key>, injects AuthenticatedMerchant extension
  • Route layering: auth applied only to POST /payments and GET /payments; GET /payments/:id and webhook routes stay public

src/api/payments.rs

  • create and list handlers extract merchant id from AuthenticatedMerchant extension (no free-text field)
  • list scopes DB query to authenticated merchant

tests/api_tests.rs

  • test_unauthenticated_create_returns_401
  • test_unauthenticated_list_returns_401
  • test_invalid_api_key_returns_401
  • test_merchant_list_scoped_to_own_payments
  • Existing tests updated to provision a merchant and pass the Bearer token

Acceptance criteria

  • Unauthenticated POST /payments → 401
  • Unauthenticated GET /payments → 401
  • A merchant only sees its own payments in GET /payments
  • GET /payments/:id remains public (status polling without a key)

Nexha-dev and others added 2 commits June 23, 2026 12:55
- Add merchants table (id, api_key_hash) to migrate()
- POST /merchants provisions a merchant and returns a one-time raw key
- auth_middleware validates Bearer token via SHA-256 hash lookup
- Derive merchant_id from the authenticated key; removed free-text field
- Scope GET /payments list to authenticated merchant only
- GET /payments/:id remains public (poll by payment id without key)
- Add tests: 401 on missing/invalid key, merchant list isolation,
  idempotency key scoping per merchant

Closes StellarGateLabs#9
@Manuel1234477 Manuel1234477 merged commit 7223529 into StellarGateLabs:main Jun 23, 2026
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.

Merchant API-key authentication and scoping

2 participants