Skip to content

Commit

Permalink
Fixed problem with using existing databases.
Browse files Browse the repository at this point in the history
  • Loading branch information
chesspro13 committed Sep 14, 2024
1 parent 2e21a45 commit d928202
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/services/encryption/open_id_encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,11 @@ import utils from "../utils.js";
import dataEncryptionService from "./data_encryption.js";
import sql from "../sql.js";
import sqlInit from "../sql_init.js";
import OpenIDError from "../../errors/open_id_error.js";

function saveUser(subjectIdentifier: string, name: string, email: string) {
if (isUserSaved()) return false;

// Allows setup with existing instances of trilium
sql.execute(`
CREATE TABLE IF NOT EXISTS "user_data"
(
tmpID INT,
username TEXT,
email TEXT,
userIDEcnryptedDataKey TEXT,
userIDVerificationHash TEXT,
salt TEXT,
derivedKey TEXT,
isSetup TEXT DEFAULT "false",
UNIQUE (tmpID),
PRIMARY KEY (tmpID)
);`);

const verificationSalt = utils.randomSecureToken(32);
const derivedKeySalt = utils.randomSecureToken(32);

Expand All @@ -32,8 +17,7 @@ function saveUser(subjectIdentifier: string, name: string, email: string) {
verificationSalt
);
if (verificationHash === undefined) {
console.log("Verification hash undefined!");
return undefined;
throw new OpenIDError("Verification hash undefined!")
}

const userIDEncryptedDataKey = setDataKey(
Expand Down Expand Up @@ -75,13 +59,11 @@ function isUserSaved() {

function verifyOpenIDSubjectIdentifier(subjectIdentifier: string) {
if (!sqlInit.isDbInitialized()) {
console.log("Database not initialized!");
return undefined;
throw new OpenIDError("Database not initialized!");
}

if (!isUserSaved()) {
console.log("DATABASE NOT SETUP");
return undefined;
if (isUserSaved()) {
return false;
}

const salt = sql.getValue("SELECT salt FROM user_data;");
Expand Down Expand Up @@ -115,7 +97,6 @@ function setDataKey(
plainTextDataKey: string | Buffer,
salt: string
) {
console.log("Subject Identifier: " + subjectIdentifier);
const subjectIdentifierDerivedKey =
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier, salt);

Expand All @@ -132,7 +113,6 @@ function setDataKey(
}

function getDataKey(subjectIdentifier: string) {
console.log("Subject Identifier: " + subjectIdentifier);
const subjectIdentifierDerivedKey =
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier);

Expand Down
15 changes: 15 additions & 0 deletions src/services/sql_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ async function initDbConnection() {

sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');

sql.execute(`
CREATE TABLE IF NOT EXISTS "user_data"
(
tmpID INT,
username TEXT,
email TEXT,
userIDEcnryptedDataKey TEXT,
userIDVerificationHash TEXT,
salt TEXT,
derivedKey TEXT,
isSetup TEXT DEFAULT "false",
UNIQUE (tmpID),
PRIMARY KEY (tmpID)
);`)

dbReady.resolve();
}

Expand Down

0 comments on commit d928202

Please sign in to comment.