Skip to content

Commit

Permalink
Revert "fix: encrypt user supplied tokens for Notion"
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu authored Apr 10, 2022
1 parent 8da05e0 commit 8939858
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 126 deletions.
5 changes: 5 additions & 0 deletions server/lib/User/getNotionData.ts
Original file line number Diff line number Diff line change
@@ -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();
}
6 changes: 3 additions & 3 deletions server/lib/jobs/ConversionJob.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 3 additions & 8 deletions server/lib/misc/TokenHandler.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 0 additions & 8 deletions server/lib/misc/hashToken.ts

This file was deleted.

7 changes: 0 additions & 7 deletions server/lib/misc/unHashToken.ts

This file was deleted.

This file was deleted.

57 changes: 0 additions & 57 deletions server/migrations/20220410101019_hash-all-notion-tokens.js

This file was deleted.

28 changes: 2 additions & 26 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"notion2anki"
],
"author": "Alexander Alemayhu",
"version": "0.10.2",
"version": "0.9.7",
"engines": {
"node": ">=12.0.0"
},
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions server/routes/notion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -19,8 +20,8 @@ const ConfigureNotionAPI = async (
res: express.Response
): Promise<NotionAPIWrapper> => {
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) => {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8939858

Please sign in to comment.