Skip to content

AINATIVEM-42 AES-256-GCM token encryption#8

Merged
youssef-saber-3 merged 2 commits into
masterfrom
AINATIVEM-44
May 14, 2026
Merged

AINATIVEM-42 AES-256-GCM token encryption#8
youssef-saber-3 merged 2 commits into
masterfrom
AINATIVEM-44

Conversation

@youssef-saber-3
Copy link
Copy Markdown
Contributor

@youssef-saber-3 youssef-saber-3 commented May 14, 2026

Summary

  • Generates src/crypto/encrypt.ts in the scaffolded app: AES-256-GCM encrypt/decrypt using Node.js built-in node:crypto
  • Tokens are encrypted before writing to DB and decrypted on read in tokenRepository
  • access_token and refresh_token columns widened from VARCHAR(768) to TEXT (ciphertext is larger than plaintext)
  • ENCRYPTION_KEY= added to generated .env.example with a # generate with: openssl rand -hex 32 hint
  • docker compose updocker-compose up in CLI next steps
  • USER node added to generated Dockerfile; SQLite volume directory pre-created with correct ownership

Test plan

  • npm test passes
  • Generated postgres app starts, tokens stored as AES-256-GCM ciphertext
  • Generated mysql app starts, tokens stored as AES-256-GCM ciphertext
  • Generated sqlite app starts, tokens stored as AES-256-GCM ciphertext

- generate src/crypto/encrypt.ts with encrypt/decrypt using node:crypto
- wire CryptoStep into NodeProjectBuilder, add ENCRYPTION_KEY to .env.example
- widen access_token/refresh_token columns to TEXT (varchar(768) too small for ciphertext)
- encrypt on write, decrypt on read in tokenRepository for all db drivers
- add RUN mkdir -p /app/data && chown in Dockerfile for sqlite volume permissions
- replace docker compose with docker-compose in CLI next steps
- add USER node to generated Dockerfile
@youssef-saber-3 youssef-saber-3 changed the title AINATIVEM-44 oauth, database generator, token encryption AINATIVEM-42 AES-256-GCM token encryption May 14, 2026
@youssef-saber-3 youssef-saber-3 marked this pull request as ready for review May 14, 2026 12:27
Copilot AI review requested due to automatic review settings May 14, 2026 12:27
@youssef-saber-3 youssef-saber-3 merged commit a9c8373 into master May 14, 2026
3 checks passed
@youssef-saber-3 youssef-saber-3 deleted the AINATIVEM-44 branch May 14, 2026 12:31
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds AES-256-GCM token encryption to the generated Node scaffold and updates related database, environment, Docker, CLI, and test coverage.

Changes:

  • Adds generated src/crypto/encrypt.ts and wires token encryption/decryption into tokenRepository.
  • Widens token columns to TEXT for Postgres/MySQL and adds ENCRYPTION_KEY to generated env examples.
  • Updates Docker/CLI behavior and adds tests for crypto generation, schema changes, and Dockerfile updates.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/generators/node/projectBuilder.ts Adds crypto generation step and ENCRYPTION_KEY env example entry.
src/generators/node/projectBuilder.test.ts Tests env example and crypto file generation via builder.
src/generators/node/index.ts Includes crypto generation in the main Node generator pipeline.
src/generators/node/database.ts Encrypts/decrypts tokens, widens token columns, and updates Dockerfile user/data ownership.
src/generators/node/database.test.ts Adds coverage for token column type changes, crypto imports, and Dockerfile updates.
src/generators/node/crypto.ts Adds generator for AES-256-GCM encrypt/decrypt helper.
src/generators/node/crypto.test.ts Adds tests for generated crypto helper content and round-trip behavior.
src/cli.ts Updates next-step command to docker-compose up.
src/cli.test.ts Updates CLI next-step expectation.
Comments suppressed due to low confidence (1)

src/generators/node/database.ts:400

  • These update values re-encrypt the same access and refresh tokens that were already encrypted for the insert values above, so each upsert performs duplicate crypto work and sends two different ciphertexts for each token in one statement. Compute the encrypted tokens once and reuse them for both insert and update fields.
						accessToken: encrypt(token.access_token),
						refreshToken: encrypt(token.refresh_token),

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +16 to +18
const buf = Buffer.from(key, 'hex');
if (buf.length !== 32) throw new Error('ENCRYPTION_KEY must be a 64-char hex string (32 bytes)');
return buf;
Comment on lines +30 to +31
const [ivB64, authTagB64, dataB64] = ciphertext.split('.');
if (!ivB64 || !authTagB64 || !dataB64) throw new Error('Invalid ciphertext format');
Comment on lines +199 to +200
ENCRYPTION_KEY=
# generate with: openssl rand -hex 32
await writeFile(
runner,
`
import { encrypt, decrypt } from '${join(tmpDir, 'src/crypto/encrypt.ts')}';
Comment thread src/cli.ts
@@ -16,7 +16,7 @@ export function nextStepLines(options: NextStepOptions): string[] {
`cd ${options.nameOrPath}`,
'cp .env.example .env',
'# fill in PIPEDRIVE_CLIENT_ID and PIPEDRIVE_CLIENT_SECRET',
Comment on lines +53 to +54
const { execSync } = await import('node:child_process');
const out = execSync(`./node_modules/.bin/tsx ${runner}`, {
Comment on lines +320 to +321
accessToken: encrypt(token.access_token),
refreshToken: encrypt(token.refresh_token),
Comment on lines +199 to +200
ENCRYPTION_KEY=
# generate with: openssl rand -hex 32
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.

3 participants