Feature/auth middleware#13
Merged
robertocarlous merged 7 commits intoNeurowealth:mainfrom Mar 6, 2026
Merged
Conversation
Contributor
|
Hello @Joaco2603 Pls pull main branch and sync with your branch and run ci to ensure it pass, so i can merge,thank you |
Contributor
Author
|
Hi @robertocarlous I just synced with the main version and everything is up to date. The integration looks good, so we're ready for the merge. Please keep an eye out for any further modifications. |
Contributor
|
Nice implementation |
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.
Secure Authentication System for NeuroWealth Backend
Closes #8
Summary
Implemented a full authentication system protecting all user-specific and financial endpoints.
Authentication is based on Stellar keypair ownership (challenge-response with signature verification) rather than passwords, issuing a 24-hour JWT backed by a database session on success.
What was built / changed
New files
src/types/express.d.tsreq.userIdandreq.stellarPubKeyto the ExpressRequestinterfacesrc/utils/stellar/stellar-verification.tsStellarVerificationclass — owns the in-memory nonce store,purgeExpiredNonces(),verifyStellarSignature(), andresolveNetwork(). Exports a singleton and_nonceStoreForTestssrc/jobs/sessionCleanup.tsconnectDb()—scheduleSessionCleanup()— daily cron that deletes expired sessions from the database; also runs once at startupsrc/controllers/auth-controller.tschallenge,verify,logoutHTTP handlers — delegates all Stellar logic toStellarVerificationsrc/controllers/__tests__/auth.test.tsAUTH.mdModified files
src/middleware/authenticate.tsreq.userIdandreq.stellarPubKeysrc/routes/auth.tsPOST /challenge,POST /verify,POST /logout(logout requires JWT)src/config/env.tsconfig.jwt.session_ttl_hours,config.jwt.nonce_ttl_ms,config.jwt.interval_mssrc/config/index.tsconfigalongsideJwtAdaptersrc/db/index.tsconnectDb()— pings Prisma, logs a clear error and callsprocess.exit(1)if the database is unreachablesrc/index.tsauthRouter, appliesAuthMiddleware.validateJwtto all four financial route prefixes, callsconnectDb()before accepting traffic, starts session cleanup crontsconfig.json"ts-node": { "files": true }and explicit"files": ["src/types/express.d.ts"]so ts-node picks up the Express augmentation.env.exampleJWT_SEED,JWT_SESSION_TTL_HOURS,JWT_NONCE_TTL_MS,JWT_CLEANUP_INTERVAL_MS,WALLET_ENCRYPTION_KEY,DB_NAME,DB_PASSWORDreadme.mdAuth flow
Protected routes
AuthMiddleware.validateJwtis applied to:POST /api/portfolioPOST /api/transactionsPOST /api/depositPOST /api/withdrawThe middleware validates the JWT signature, then checks that the session exists in the database and has not expired. A missing or stale row always returns 401.
Security properties
Keypair.verify()on raw UTF-8 nonce bytesTesting
19 unit tests, 0 failures (
npm test -- auth):Challenge
stellarPubKeynonceandexpiresAtVerify
AuthMiddleware
next(), attachesreq.userIdandreq.stellarPubKeyLogout
Refactoring decisions
StellarVerificationextracted from controllerThe controller had inline helper functions (
purgeExpiredNonces,verifyStellarSignature,resolveNetwork) and owned the nonce store directly. These are Stellar domain concerns, not HTTP concerns. They were moved tosrc/utils/stellar/stellar-verification.tsas a class that owns the Map and exposes a clean interface. The controller imports and delegates to the singleton.connectDb()extracted fromindex.tsStartup DB connectivity logic was moved into
src/db/index.tsalongside the Prisma singleton.index.tsnow calls a singleawait connectDb()line — startup concerns belong to the DB layer.express.d.tsaugmentation fixts-nodedoes not automatically load.d.tsfiles reachable only via theincludeglob — it only follows explicitimportchains. Fixed by:export {}inexpress.d.ts(makes it a module augmentation, not an ambient declaration that would shadow all Express types)"ts-node": { "files": true }+ explicit"files": ["src/types/express.d.ts"]intsconfig.jsonHow to test locally