mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-12 20:02:28 +08:00
Fixed problem with using existing databases.
This commit is contained in:
parent
2e21a45fc1
commit
d92820280d
@ -4,26 +4,11 @@ import utils from "../utils.js";
|
|||||||
import dataEncryptionService from "./data_encryption.js";
|
import dataEncryptionService from "./data_encryption.js";
|
||||||
import sql from "../sql.js";
|
import sql from "../sql.js";
|
||||||
import sqlInit from "../sql_init.js";
|
import sqlInit from "../sql_init.js";
|
||||||
|
import OpenIDError from "../../errors/open_id_error.js";
|
||||||
|
|
||||||
function saveUser(subjectIdentifier: string, name: string, email: string) {
|
function saveUser(subjectIdentifier: string, name: string, email: string) {
|
||||||
if (isUserSaved()) return false;
|
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 verificationSalt = utils.randomSecureToken(32);
|
||||||
const derivedKeySalt = utils.randomSecureToken(32);
|
const derivedKeySalt = utils.randomSecureToken(32);
|
||||||
|
|
||||||
@ -32,8 +17,7 @@ function saveUser(subjectIdentifier: string, name: string, email: string) {
|
|||||||
verificationSalt
|
verificationSalt
|
||||||
);
|
);
|
||||||
if (verificationHash === undefined) {
|
if (verificationHash === undefined) {
|
||||||
console.log("Verification hash undefined!");
|
throw new OpenIDError("Verification hash undefined!")
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const userIDEncryptedDataKey = setDataKey(
|
const userIDEncryptedDataKey = setDataKey(
|
||||||
@ -75,13 +59,11 @@ function isUserSaved() {
|
|||||||
|
|
||||||
function verifyOpenIDSubjectIdentifier(subjectIdentifier: string) {
|
function verifyOpenIDSubjectIdentifier(subjectIdentifier: string) {
|
||||||
if (!sqlInit.isDbInitialized()) {
|
if (!sqlInit.isDbInitialized()) {
|
||||||
console.log("Database not initialized!");
|
throw new OpenIDError("Database not initialized!");
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isUserSaved()) {
|
if (isUserSaved()) {
|
||||||
console.log("DATABASE NOT SETUP");
|
return false;
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const salt = sql.getValue("SELECT salt FROM user_data;");
|
const salt = sql.getValue("SELECT salt FROM user_data;");
|
||||||
@ -115,7 +97,6 @@ function setDataKey(
|
|||||||
plainTextDataKey: string | Buffer,
|
plainTextDataKey: string | Buffer,
|
||||||
salt: string
|
salt: string
|
||||||
) {
|
) {
|
||||||
console.log("Subject Identifier: " + subjectIdentifier);
|
|
||||||
const subjectIdentifierDerivedKey =
|
const subjectIdentifierDerivedKey =
|
||||||
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier, salt);
|
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier, salt);
|
||||||
|
|
||||||
@ -132,7 +113,6 @@ function setDataKey(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getDataKey(subjectIdentifier: string) {
|
function getDataKey(subjectIdentifier: string) {
|
||||||
console.log("Subject Identifier: " + subjectIdentifier);
|
|
||||||
const subjectIdentifierDerivedKey =
|
const subjectIdentifierDerivedKey =
|
||||||
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier);
|
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier);
|
||||||
|
|
||||||
|
@ -48,6 +48,21 @@ async function initDbConnection() {
|
|||||||
|
|
||||||
sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
|
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();
|
dbReady.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user