feat: secure Aadhaar tokenization and step-up detokenization - #114
Open
DUKartik wants to merge 22 commits into
Open
feat: secure Aadhaar tokenization and step-up detokenization#114DUKartik wants to merge 22 commits into
DUKartik wants to merge 22 commits into
Conversation
…scaffolding - Add isolated microservices/aadhaar-vault/ workspace (Fastify 4, TypeScript 5, strict mode + noUncheckedIndexedAccess) with its own package.json, tsconfig, vitest config, Dockerfile, and docker-compose.yml. - Wire a Pino logger whose redact paths cover Authorization / cookie headers and any aadhaar-shaped field; a defensive stdout/stderr scrubber strips any 12-digit sequence before it reaches the wire. - Stand up the /health, /health/live, /health/ready routes mandated by architecture §6, plus a JSON 404 fallback so PII never leaks via Fastify's default HTML error page. - Boot smoke test covers live + ready + JSON 404 + Authorization-header redaction. tsc --noEmit is clean and all 5 vitest cases pass in 1.85 s. - Zero changes outside microservices/aadhaar-vault/. The root package.json, legacy backend/, and frontend/ trees remain untouched — verified before commit.
… + /health/ready - application/ports/key-manager.ts: 4-method crypto port (getActiveDek / wrapDek / unwrapDek / generateDek) - infrastructure/key-providers/local-dev-key-manager.ts: HKDF + AES-256-GCM (zero-cost dev impl) - infrastructure/key-providers/index.ts: factory with prod-guard (refuses to boot LocalDev in production) - util/dek-zero.ts: handling for '00000000...' sentinel DEK return path - routes/health.routes.ts: extended /health/ready to assert keyProvider.isReady() - server.ts: thread keyManager (closure) into /health/ready via app.decorate - tests/key-manager.test.ts: 8 cases (round-trip, wrong key, missing key, key-rotation, primary-rotation, sentinel, paired DEKs)
…d 40/40 tests green
…to feature/aadhaar-vault
…P verifier (98 tests across 9 suites)
…fier (98 tests across 9 suites)
…token/detokenize)
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.
Pull Request: Feat --- Aadhaar Vault Microservice (Tokenization & Step-Up Auth)
🎯 Objective
This PR introduces a standalone Aadhaar Vault microservice for securely tokenizing and protecting sensitive student identity numbers such as Aadhaar.
It isolates plaintext PII from the main application and provides two capabilities:
🏗️ Architecture & Stack
The service follows Clean Architecture, keeping application logic independent from HTTP, database, and cryptographic infrastructure.
pg) + customMemoryPoolfor testsjose✨ Core Features
Tokenization
POST /v1/tokenize---vault:tokenizePlaintext Aadhaar is encrypted inside the vault and represented externally using an opaque token. The authenticated JWT subject is used as the trusted actor for audit attribution.
Step-Up Detokenization
Plaintext recovery requires an additional MFA-protected authorization
flow:
Key endpoints:
POST /v1/mfa/enroll--- creates a TOTP factor.POST /v1/detokenize/request--- creates a short-lived challenge.POST /v1/detokenize/step-up/:challengeId/approve--- verifies TOTPand approves the challenge.
POST /v1/detokenize--- consumes the approved challenge andreleases plaintext.
POST /v1/mfa/verify--- standalone factor verification; notrequired by the dedicated Step-Up console flow.
Consumed challenges cannot be reused, providing replay protection.
🔒 Security
safeZero()where applicable.sub.short-lived approved challenge.
LocalDevKeyManageris guarded against accidental production use.🖥️ Developer Consoles
Two separate developer interfaces are provided:
Tokenization Console ---
/console/index.htmlUsed for tokenization and architecture inspection.
Step-Up Console ---
/console/stepup.htmlGuides developers through:
Intermediate Factor IDs and Challenge IDs are propagated automatically.
🏥 Operations
GET /healthGET /health/readynpm run migrate🧪 Testing
The test suite covers tokenization, encryption, JWT/scopes, MFA/TOTP, Step-Up challenges, expiry, replay rejection, detokenization, auditing, key management, and Clean Architecture boundaries.
The end-to-end smoke flow verifies:
📋 Reviewer Guide
Recommended starting points:
src/server.ts--- dependency wiringsrc/auth/plugin.ts--- JWT authentication and scopessrc/application/commands/tokenize-aadhaar.ts--- tokenizationsrc/application/commands/detokenize-aadhaar.ts--- plaintextrecovery
STEP_UP_AUTH.md--- Step-Up designAADHAAR_VAULT_FREE_ARCHITECTURE.md--- architectureRUN.md--- local setup✅ Summary
This PR adds a standalone Aadhaar Vault with tokenization, envelope encryption, JWT authentication, TOTP MFA, Step-Up authorization, replay protection, audit logging, PostgreSQL persistence, developer consoles, and automated security/workflow tests.
The main application can operate on opaque identity tokens while plaintext Aadhaar remains isolated behind a dedicated security boundary.