π Secure Wallet Endpoints with JWT Authentication π οΈ
π Description
Secure wallet-related API endpoints in the Stellar wallet service by applying JWT authentication middleware. This will ensure that only users with valid JWTs, issued after successful WebAuthn authentication, can access sensitive wallet operations such as account creation and management. The implementation will protect the /wallet/* endpoints to enhance the security of the service.
π― Objective
Apply the JWT middleware from src/auth/jwt.js to secure the /wallet/* endpoints in services/stellar-wallet/src/routes/wallet.js, with a unit test to verify access control.
π Structure
- Directory:
services/stellar-wallet
- Files:
src/routes/wallet.js (updated)
tests/routes/wallet.test.js (updated)
- Expected structure:
services/stellar-wallet
βββ src
β βββ index.js
β βββ stellar
β β βββ client.js
β β βββ keys.js
β β βββ fund.js
β βββ db
β β βββ kyc.js
β βββ routes
β β βββ kyc.js
β β βββ kyc-verify.js
β β βββ kyc-status.js
β β βββ auth-register.js
β β βββ auth-verify.js
β β βββ auth-login.js
β β βββ wallet.js
β βββ kyc
β β βββ validate.js
β βββ soroban
β β βββ client.js
β β βββ kyc-contract.rs
β β βββ deploy.js
β βββ auth
β βββ webauthn.js
β βββ jwt.js
βββ config
β βββ db.sqlite
βββ tests
β βββ stellar
β β βββ client.test.js
β β βββ keys.test.js
β β βββ fund.test.js
β βββ db
β β βββ kyc.test.js
β βββ routes
β β βββ kyc.test.js
β β βββ kyc-verify.test.js
β β βββ kyc-status.test.js
β β βββ auth-register.test.js
β β βββ auth-verify.test.js
β β βββ auth-login.test.js
β β βββ wallet.test.js
β βββ kyc
β β βββ validate.test.js
β βββ soroban
β β βββ client.test.js
β β βββ deploy.test.js
β βββ auth
β βββ jwt.test.js
βββ package.json
βββ .env.example
βββ .eslintrc.json
βββ .eslintignore
βββ .prettierrc.json
βββ .prettierignore
βββ .gitignore
β
Requirements
- Create a branch named
feat/secure-wallet-endpoints for this task.
- Update
src/routes/wallet.js to apply the jwtMiddleware from src/auth/jwt.js (Issue 20) to the POST /wallet/create endpoint.
- Ensure the middleware checks for a valid JWT in the
Authorization: Bearer <token> header.
- Return HTTP 401 with a JSON error message (e.g.,
{ error: "Unauthorized" }) if the JWT is missing or invalid.
- Return HTTP 403 with a JSON error message (e.g.,
{ error: "Forbidden" }) if the user_id in the JWT does not match the user_id in the request body.
- Update
src/index.js to apply jwtMiddleware to all /wallet/* routes for consistency.
- Update the unit test in
tests/routes/wallet.test.js to verify:
- Requests with a valid JWT and matching
user_id succeed (HTTP 201 for POST /wallet/create).
- Requests without a JWT return HTTP 401.
- Requests with a valid JWT but mismatched
user_id return HTTP 403.
- Mock the JWT middleware and SQLite database in the unit test to avoid external dependencies.
- Ensure the code adheres to ESLint and Prettier rules (from Issue 3).
- Commit changes to the
feat/secure-wallet-endpoints branch with a message like feat: secure wallet endpoints.
- Verify that the CI pipeline (from Issue 1) passes, with linting and test jobs succeeding.
π Expected Outcomes
src/routes/wallet.js applies jwtMiddleware to the POST /wallet/create endpoint.
/wallet/* routes are protected by jwtMiddleware in src/index.js.
- Valid JWTs with matching
user_id allow access to the POST /wallet/create endpoint.
- Missing or invalid JWTs return HTTP 401 with a JSON error message.
- Mismatched
user_id returns HTTP 403 with a JSON error message.
- Updated unit test in
tests/routes/wallet.test.js confirms access control behavior.
- Code passes ESLint and Prettier checks.
- Changes are committed to the
feat/secure-wallet-endpoints branch with a descriptive lowercase commit message.
- CI pipeline runs successfully, with linting passing for updated
src/routes/wallet.js and tests/routes/wallet.test.js, and the unit test passing.
π References
π Notes
- The
jwtMiddleware should extract and validate the JWT from the Authorization: Bearer <token> header.
- The
user_id in the JWT payload must match the user_id in the request body to prevent unauthorized access.
- Mocking the JWT middleware and SQLite database in tests ensures reliable CI execution.
- Ensure error messages are consistent with other endpoints (e.g., JSON format
{ error: "message" }).
- Commit messages must be in lowercase and start with
feat, change, fix, chore, or refactor.
- The CI pipeline should validate the updated code, ensuring ESLint passes and the unit test executes successfully.
π Secure Wallet Endpoints with JWT Authentication π οΈ
π Description
Secure wallet-related API endpoints in the Stellar wallet service by applying JWT authentication middleware. This will ensure that only users with valid JWTs, issued after successful WebAuthn authentication, can access sensitive wallet operations such as account creation and management. The implementation will protect the
/wallet/*endpoints to enhance the security of the service.π― Objective
Apply the JWT middleware from
src/auth/jwt.jsto secure the/wallet/*endpoints inservices/stellar-wallet/src/routes/wallet.js, with a unit test to verify access control.π Structure
services/stellar-walletsrc/routes/wallet.js(updated)tests/routes/wallet.test.js(updated)β Requirements
feat/secure-wallet-endpointsfor this task.src/routes/wallet.jsto apply thejwtMiddlewarefromsrc/auth/jwt.js(Issue 20) to thePOST /wallet/createendpoint.Authorization: Bearer <token>header.{ error: "Unauthorized" }) if the JWT is missing or invalid.{ error: "Forbidden" }) if theuser_idin the JWT does not match theuser_idin the request body.src/index.jsto applyjwtMiddlewareto all/wallet/*routes for consistency.tests/routes/wallet.test.jsto verify:user_idsucceed (HTTP 201 forPOST /wallet/create).user_idreturn HTTP 403.feat/secure-wallet-endpointsbranch with a message likefeat: secure wallet endpoints.π Expected Outcomes
src/routes/wallet.jsappliesjwtMiddlewareto thePOST /wallet/createendpoint./wallet/*routes are protected byjwtMiddlewareinsrc/index.js.user_idallow access to thePOST /wallet/createendpoint.user_idreturns HTTP 403 with a JSON error message.tests/routes/wallet.test.jsconfirms access control behavior.feat/secure-wallet-endpointsbranch with a descriptive lowercase commit message.src/routes/wallet.jsandtests/routes/wallet.test.js, and the unit test passing.π References
π Notes
jwtMiddlewareshould extract and validate the JWT from theAuthorization: Bearer <token>header.user_idin the JWT payload must match theuser_idin the request body to prevent unauthorized access.{ error: "message" }).feat,change,fix,chore, orrefactor.