mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-08 09:12:29 +08:00
style: 💄 restyle
This commit is contained in:
parent
d4cd0e8eff
commit
04cbe9d3d1
@ -6,140 +6,140 @@ import sqlInit from "../sql_init.js";
|
|||||||
import OpenIDError from "../../errors/mfa_error.js";
|
import OpenIDError from "../../errors/mfa_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;
|
||||||
|
|
||||||
const verificationSalt = utils.randomSecureToken(32);
|
const verificationSalt = utils.randomSecureToken(32);
|
||||||
const derivedKeySalt = utils.randomSecureToken(32);
|
const derivedKeySalt = utils.randomSecureToken(32);
|
||||||
|
|
||||||
const verificationHash = myScryptService.getSubjectIdentifierVerificationHash(
|
const verificationHash = myScryptService.getSubjectIdentifierVerificationHash(
|
||||||
subjectIdentifier,
|
subjectIdentifier,
|
||||||
verificationSalt
|
verificationSalt
|
||||||
);
|
);
|
||||||
if (verificationHash === undefined) {
|
if (verificationHash === undefined) {
|
||||||
throw new OpenIDError("Verification hash undefined!")
|
throw new OpenIDError("Verification hash undefined!")
|
||||||
}
|
}
|
||||||
|
|
||||||
const userIDEncryptedDataKey = setDataKey(
|
const userIDEncryptedDataKey = setDataKey(
|
||||||
subjectIdentifier,
|
subjectIdentifier,
|
||||||
utils.randomSecureToken(16),
|
utils.randomSecureToken(16),
|
||||||
verificationSalt
|
verificationSalt
|
||||||
);
|
);
|
||||||
|
|
||||||
if (userIDEncryptedDataKey === undefined || userIDEncryptedDataKey === null) {
|
if (userIDEncryptedDataKey === undefined || userIDEncryptedDataKey === null) {
|
||||||
console.log("USERID ENCRYPTED DATA KEY NULL");
|
console.log("USERID ENCRYPTED DATA KEY NULL");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
tmpID: 0,
|
tmpID: 0,
|
||||||
userIDVerificationHash: utils.toBase64(verificationHash),
|
userIDVerificationHash: utils.toBase64(verificationHash),
|
||||||
salt: verificationSalt,
|
salt: verificationSalt,
|
||||||
derivedKey: derivedKeySalt,
|
derivedKey: derivedKeySalt,
|
||||||
userIDEcnryptedDataKey: userIDEncryptedDataKey,
|
userIDEcnryptedDataKey: userIDEncryptedDataKey,
|
||||||
isSetup: "true",
|
isSetup: "true",
|
||||||
username: name,
|
username: name,
|
||||||
email: email
|
email: email
|
||||||
};
|
};
|
||||||
|
|
||||||
sql.upsert("user_data", "tmpID", data);
|
sql.upsert("user_data", "tmpID", data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSubjectIdentifierSaved() {
|
function isSubjectIdentifierSaved() {
|
||||||
const value = sql.getValue("SELECT userIDEcnryptedDataKey FROM user_data;");
|
const value = sql.getValue("SELECT userIDEcnryptedDataKey FROM user_data;");
|
||||||
if (value === undefined || value === null || value === "") return false;
|
if (value === undefined || value === null || value === "") return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isUserSaved() {
|
function isUserSaved() {
|
||||||
const isSaved = sql.getValue<string>("SELECT isSetup FROM user_data;");
|
const isSaved = sql.getValue<string>("SELECT isSetup FROM user_data;");
|
||||||
return isSaved === "true" ? true : false;
|
return isSaved === "true" ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyOpenIDSubjectIdentifier(subjectIdentifier: string) {
|
function verifyOpenIDSubjectIdentifier(subjectIdentifier: string) {
|
||||||
if (!sqlInit.isDbInitialized()) {
|
if (!sqlInit.isDbInitialized()) {
|
||||||
throw new OpenIDError("Database not initialized!");
|
throw new OpenIDError("Database not initialized!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUserSaved()) {
|
if (isUserSaved()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const salt = sql.getValue("SELECT salt FROM user_data;");
|
const salt = sql.getValue("SELECT salt FROM user_data;");
|
||||||
if (salt == undefined) {
|
if (salt == undefined) {
|
||||||
console.log("Salt undefined");
|
console.log("Salt undefined");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const givenHash = myScryptService
|
const givenHash = myScryptService
|
||||||
.getSubjectIdentifierVerificationHash(subjectIdentifier)
|
.getSubjectIdentifierVerificationHash(subjectIdentifier)
|
||||||
?.toString("base64");
|
?.toString("base64");
|
||||||
if (givenHash === undefined) {
|
if (givenHash === undefined) {
|
||||||
console.log("Sub id hash undefined!");
|
console.log("Sub id hash undefined!");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const savedHash = sql.getValue(
|
const savedHash = sql.getValue(
|
||||||
"SELECT userIDVerificationHash FROM user_data"
|
"SELECT userIDVerificationHash FROM user_data"
|
||||||
);
|
);
|
||||||
if (savedHash === undefined) {
|
if (savedHash === undefined) {
|
||||||
console.log("verification hash undefined");
|
console.log("verification hash undefined");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Matches: " + givenHash === savedHash);
|
console.log("Matches: " + givenHash === savedHash);
|
||||||
return givenHash === savedHash;
|
return givenHash === savedHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setDataKey(
|
function setDataKey(
|
||||||
subjectIdentifier: string,
|
subjectIdentifier: string,
|
||||||
plainTextDataKey: string | Buffer,
|
plainTextDataKey: string | Buffer,
|
||||||
salt: string
|
salt: string
|
||||||
) {
|
) {
|
||||||
const subjectIdentifierDerivedKey =
|
const subjectIdentifierDerivedKey =
|
||||||
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier, salt);
|
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier, salt);
|
||||||
|
|
||||||
if (subjectIdentifierDerivedKey === undefined) {
|
if (subjectIdentifierDerivedKey === undefined) {
|
||||||
console.log("SOMETHING WENT WRONG SAVING USER ID DERIVED KEY");
|
console.log("SOMETHING WENT WRONG SAVING USER ID DERIVED KEY");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const newEncryptedDataKey = dataEncryptionService.encrypt(
|
const newEncryptedDataKey = dataEncryptionService.encrypt(
|
||||||
subjectIdentifierDerivedKey,
|
subjectIdentifierDerivedKey,
|
||||||
plainTextDataKey
|
plainTextDataKey
|
||||||
);
|
);
|
||||||
|
|
||||||
return newEncryptedDataKey;
|
return newEncryptedDataKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataKey(subjectIdentifier: string) {
|
function getDataKey(subjectIdentifier: string) {
|
||||||
const subjectIdentifierDerivedKey =
|
const subjectIdentifierDerivedKey =
|
||||||
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier);
|
myScryptService.getSubjectIdentifierDerivedKey(subjectIdentifier);
|
||||||
|
|
||||||
const encryptedDataKey = sql.getValue(
|
const encryptedDataKey = sql.getValue(
|
||||||
"SELECT userIDEcnryptedDataKey FROM user_data"
|
"SELECT userIDEcnryptedDataKey FROM user_data"
|
||||||
);
|
);
|
||||||
|
|
||||||
if (encryptedDataKey === undefined || encryptedDataKey === null) {
|
if (encryptedDataKey === undefined || encryptedDataKey === null) {
|
||||||
console.log("Encrypted data key empty!");
|
console.log("Encrypted data key empty!");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subjectIdentifierDerivedKey === undefined) {
|
if (subjectIdentifierDerivedKey === undefined) {
|
||||||
console.log("SOMETHING WENT WRONG SAVING USER ID DERIVED KEY");
|
console.log("SOMETHING WENT WRONG SAVING USER ID DERIVED KEY");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const decryptedDataKey = dataEncryptionService.decrypt(
|
const decryptedDataKey = dataEncryptionService.decrypt(
|
||||||
subjectIdentifierDerivedKey,
|
subjectIdentifierDerivedKey,
|
||||||
encryptedDataKey.toString()
|
encryptedDataKey.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
return decryptedDataKey;
|
return decryptedDataKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
verifyOpenIDSubjectIdentifier,
|
verifyOpenIDSubjectIdentifier,
|
||||||
getDataKey,
|
getDataKey,
|
||||||
setDataKey,
|
setDataKey,
|
||||||
saveUser,
|
saveUser,
|
||||||
isSubjectIdentifierSaved,
|
isSubjectIdentifierSaved,
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
import sql from "../sql.js";
|
import sql from "../sql.js";
|
||||||
import optionService from "../options.js";
|
import optionService from "../options.js";
|
||||||
import myScryptService from "./my_scrypt.js";
|
import myScryptService from "./my_scrypt.js";
|
||||||
|
@ -23,6 +23,7 @@ function setRecoveryCodes(recoveryCodes: string) {
|
|||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRecoveryCodes() {
|
function getRecoveryCodes() {
|
||||||
if (!isRecoveryCodeSet()) {
|
if (!isRecoveryCodeSet()) {
|
||||||
return Array(8).fill("Keys not set")
|
return Array(8).fill("Keys not set")
|
||||||
@ -67,7 +68,7 @@ function verifyRecoveryCode(recoveryCodeGuess: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getUsedRecoveryCodes() {
|
function getUsedRecoveryCodes() {
|
||||||
if (!isRecoveryCodeSet()){
|
if (!isRecoveryCodeSet()) {
|
||||||
return Array(8).fill("Recovery code not set")
|
return Array(8).fill("Recovery code not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user