diff --git a/server/lib/User/getNotionData.ts b/server/lib/User/getNotionData.ts new file mode 100644 index 000000000..ecda47588 --- /dev/null +++ b/server/lib/User/getNotionData.ts @@ -0,0 +1,5 @@ +import { Knex } from "knex"; + +export default async function getNotionData(DB: Knex, owner: string) { + return DB("notion_tokens").where({ owner }).returning(["token"]).first(); +} diff --git a/server/lib/jobs/ConversionJob.ts b/server/lib/jobs/ConversionJob.ts index 28bfc5404..16da89bd5 100644 --- a/server/lib/jobs/ConversionJob.ts +++ b/server/lib/jobs/ConversionJob.ts @@ -1,8 +1,8 @@ import { Knex } from "knex"; import performConversion from "../../routes/notion/convert/helpers/performConversion"; -import TokenHandler from "../misc/TokenHandler"; import NotionAPIWrapper from "../notion/NotionAPIWrapper"; +import getNotionData from "../User/getNotionData"; export default class ConversionJob { db: Knex; @@ -58,8 +58,8 @@ export default class ConversionJob { console.log("jobs", jobs); jobs.forEach(async (job) => { try { - const token = await TokenHandler.GetNotionToken(job.owner); - const api = new NotionAPIWrapper(token!); + const data = await getNotionData(DB, job.owner); + const api = new NotionAPIWrapper(data.token); await performConversion(api, job.object_id, job.owner, null, null); } catch (error) { await new ConversionJob(DB).completed(job.object_id, job.owner); diff --git a/server/lib/misc/TokenHandler.ts b/server/lib/misc/TokenHandler.ts index 7ad17a44f..6f6dc2d19 100644 --- a/server/lib/misc/TokenHandler.ts +++ b/server/lib/misc/TokenHandler.ts @@ -1,10 +1,9 @@ import crypto from "crypto"; import jwt from "jsonwebtoken"; +import { Knex } from "knex"; import DB from "../storage/db"; -import hashToken from "./hashToken"; -import unHashToken from "./unHashToken"; interface User { owner: string; @@ -22,7 +21,7 @@ class TokenHandler { workspace_icon: data.workspace_icon, workspace_id: data.workspace_id, notion_owner: data.owner, - token: hashToken(data.access_token), + token: data.access_token, owner: user, }) .onConflict("owner") @@ -63,11 +62,7 @@ class TokenHandler { if (!owner) { return null; } - const row = await DB("notion_tokens") - .where({ owner }) - .returning("token") - .first(); - return unHashToken(row.token); + return DB("notion_tokens").where({ owner }).returning("token").first(); } static async GetPatreonToken(owner: number) { diff --git a/server/lib/misc/hashToken.ts b/server/lib/misc/hashToken.ts deleted file mode 100644 index d7b967b0b..000000000 --- a/server/lib/misc/hashToken.ts +++ /dev/null @@ -1,8 +0,0 @@ -import CryptoJS from "crypto-js"; - -export default function hashToken(token: string): string { - return CryptoJS.AES.encrypt( - token, - process.env.THE_HASHING_SECRET! - ).toString(); -} diff --git a/server/lib/misc/unHashToken.ts b/server/lib/misc/unHashToken.ts deleted file mode 100644 index ca324f6fb..000000000 --- a/server/lib/misc/unHashToken.ts +++ /dev/null @@ -1,7 +0,0 @@ -import CryptoJS from "crypto-js"; - -export default function unHashToken(hashed: string): string { - return CryptoJS.AES.decrypt(hashed, process.env.THE_HASHING_SECRET!).toString( - CryptoJS.enc.Utf8 - ); -} diff --git a/server/migrations/20220410100114_add-encrypted-field-notion-tokens.js b/server/migrations/20220410100114_add-encrypted-field-notion-tokens.js deleted file mode 100644 index 03b9d8cd6..000000000 --- a/server/migrations/20220410100114_add-encrypted-field-notion-tokens.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports.up = (knex) => { - return knex.schema.table("notion_tokens", (table) => { - table.boolean("encrypted").defaultTo(false); - }); -}; - -module.exports.down = (knex) => { - return knex.schema.table("notion_tokens", (table) => { - table.dropColumn("encrypted"); - }); -}; diff --git a/server/migrations/20220410101019_hash-all-notion-tokens.js b/server/migrations/20220410101019_hash-all-notion-tokens.js deleted file mode 100644 index cc6cb8425..000000000 --- a/server/migrations/20220410101019_hash-all-notion-tokens.js +++ /dev/null @@ -1,57 +0,0 @@ -const CryptoJS = require("crypto-js"); - -function unHashToken(token) { - return CryptoJS.AES.decrypt(hashed, process.env.THE_HASHING_SECRET).toString( - CryptoJS.enc.Utf8 - ); -} - -function hashToken(token) { - return CryptoJS.AES.encrypt(token, process.env.THE_HASHING_SECRET).toString(); -} - -module.exports.up = (knex) => { - return knex - .select() - .from("notion_tokens") - .then((users) => { - return knex.transaction((trx) => { - return knex.schema - .table("notion_tokens", () => - Promise.all( - users.map((row) => { - return knex("notion_tokens") - .update({ token: hashToken(row.token), encrypted: true }) - .where({ owner: row.owner, encrypted: false }) - .transacting(trx); - }) - ) - ) - .then(trx.commit) - .catch(trx.rollback); - }); - }); -}; - -module.exports.down = (knex) => { - return knex - .select() - .from("notion_tokens") - .then((users) => { - return knex.transaction((trx) => { - return knex.schema - .table("notion_tokens", () => - Promise.all( - users.map((row) => { - return knex("notion_tokens") - .update({ token: unHashToken(row.token), encrypted: false }) - .where({ owner: row.owner, encrypted: true }) - .transacting(trx); - }) - ) - ) - .then(trx.commit) - .catch(trx.rollback); - }); - }); -}; diff --git a/server/package-lock.json b/server/package-lock.json index 0425a3748..1c43e3f41 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -1,12 +1,12 @@ { "name": "notion2anki-server", - "version": "0.10.2", + "version": "0.9.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "notion2anki-server", - "version": "0.10.2", + "version": "0.9.7", "license": "MIT", "dependencies": { "@notionhq/client": "^0.4.12", @@ -22,7 +22,6 @@ "bcryptjs": "^2.4.3", "cheerio": "1.0.0-rc.3", "cookie-parser": "^1.4.5", - "crypto-js": "^4.1.1", "express": "^4.17.1", "find-remove": "^2.0.3", "json-markup": "^1.1.3", @@ -45,7 +44,6 @@ "@types/base-64": "^1.0.0", "@types/cheerio": "^0.22.28", "@types/cookie-parser": "^1.4.2", - "@types/crypto-js": "^4.1.1", "@types/dotenv": "^8.2.0", "@types/express": "^4.17.11", "@types/find-remove": "^2.0.0", @@ -1485,12 +1483,6 @@ "@types/express": "*" } }, - "node_modules/@types/crypto-js": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.1.1.tgz", - "integrity": "sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==", - "dev": true - }, "node_modules/@types/dotenv": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz", @@ -3121,11 +3113,6 @@ "node": ">= 8" } }, - "node_modules/crypto-js": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz", - "integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==" - }, "node_modules/crypto-random-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz", @@ -10345,12 +10332,6 @@ "@types/express": "*" } }, - "@types/crypto-js": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.1.1.tgz", - "integrity": "sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==", - "dev": true - }, "@types/dotenv": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz", @@ -11856,11 +11837,6 @@ "which": "^2.0.1" } }, - "crypto-js": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz", - "integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==" - }, "crypto-random-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz", diff --git a/server/package.json b/server/package.json index dede24a80..f0c6b5e41 100644 --- a/server/package.json +++ b/server/package.json @@ -7,7 +7,7 @@ "notion2anki" ], "author": "Alexander Alemayhu", - "version": "0.10.2", + "version": "0.9.7", "engines": { "node": ">=12.0.0" }, @@ -51,7 +51,6 @@ "bcryptjs": "^2.4.3", "cheerio": "1.0.0-rc.3", "cookie-parser": "^1.4.5", - "crypto-js": "^4.1.1", "express": "^4.17.1", "find-remove": "^2.0.3", "json-markup": "^1.1.3", @@ -74,7 +73,6 @@ "@types/base-64": "^1.0.0", "@types/cheerio": "^0.22.28", "@types/cookie-parser": "^1.4.2", - "@types/crypto-js": "^4.1.1", "@types/dotenv": "^8.2.0", "@types/express": "^4.17.11", "@types/find-remove": "^2.0.0", diff --git a/server/routes/notion/index.ts b/server/routes/notion/index.ts index 26574da9a..5f063031d 100644 --- a/server/routes/notion/index.ts +++ b/server/routes/notion/index.ts @@ -10,6 +10,7 @@ import getPage from "./getPage"; import getBlocks from "./getBlocks"; import getBlock from "./getBlock"; import getDatabase from "./getDatabase"; +import getNotionData from "../../lib/User/getNotionData"; import { queryDatabase } from "./queryDatabase"; const router = express.Router(); @@ -19,8 +20,8 @@ const ConfigureNotionAPI = async ( res: express.Response ): Promise => { console.debug("Configuring Notion API for " + req.originalUrl); - const token = await TokenHandler.GetNotionToken(res.locals.owner); - return new NotionAPIWrapper(token!); + const data = await getNotionData(DB, res.locals.owner); + return new NotionAPIWrapper(data.token); }; router.get("/connect", RequireAuthentication, async (req, res) => { diff --git a/server/routes/users/index.ts b/server/routes/users/index.ts index 04b29caae..3fe81f267 100644 --- a/server/routes/users/index.ts +++ b/server/routes/users/index.ts @@ -192,7 +192,7 @@ router.post("/register", async (req, res, next) => { } }); -const distDir = path.join(__dirname, "../../../web/build"); +const distDir = path.join(__dirname, "../../web/build"); router.get("/r/:id", async (req, res, next) => { try { const reset_token = req.params.id;