Skip to content

Commit

Permalink
feat: ✨ Store cryptographically strong random secret for each note
Browse files Browse the repository at this point in the history
  • Loading branch information
mcndt committed Nov 20, 2022
1 parent 836d491 commit de5395b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "EncryptedNote" ADD COLUMN "secret_token" TEXT;
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ model EncryptedNote {
hmac String?
iv String?
crypto_version String @default("v1")
secret_token String?
}

model event {
Expand Down
5 changes: 5 additions & 0 deletions server/src/controllers/note/note.post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ValidationError,
Matches,
} from "class-validator";
import { generateToken } from "../../crypto/GenerateToken";

/**
* Request body for creating a note
Expand Down Expand Up @@ -78,12 +79,15 @@ export async function postNoteController(

// Create note object
const EXPIRE_WINDOW_DAYS = 30;
const secret_token = generateToken();

const note = {
ciphertext: notePostRequest.ciphertext as string,
hmac: notePostRequest.hmac as string,
iv: notePostRequest.iv as string,
expire_time: addDays(new Date(), EXPIRE_WINDOW_DAYS),
crypto_version: notePostRequest.crypto_version,
secret_token: secret_token,
} as EncryptedNote;

// Store note object
Expand All @@ -97,6 +101,7 @@ export async function postNoteController(
res.json({
view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}`,
expire_time: savedNote.expire_time,
secret_token: savedNote.secret_token,
});
})
.catch(async (err) => {
Expand Down
9 changes: 9 additions & 0 deletions server/src/crypto/GenerateToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import crypto from "crypto";

/**
* Generates a 256 bit token using the nodeJS crypto module.
* @returns base 64-encoded token.
*/
export function generateToken(): string {
return crypto.randomBytes(32).toString("base64");
}

0 comments on commit de5395b

Please sign in to comment.