-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.js
133 lines (109 loc) · 4.35 KB
/
welcome.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const ipc = require('electron').ipcRenderer;
const fs = require("fs");
const passwordValidator = require('password-validator');
const passwordStrength = require('check-password-strength')
const { Confirm } = require('./utils/confirm-dialog');
var cryp = require("./utils/cryptography");
const handleData = require("./utils/handleData");
const submitBtn = document.querySelector('#pswd-submit');
const titleHead = document.querySelector('#title');
let masterKey = "";
let isMasterPassword = false;
let schema = new passwordValidator();
const dataStorePath = __dirname+"/data/data.json";
// password validator schema
schema
.is().min(6) // Minimum length 8
.is().max(100) // Maximum length 100
.has().uppercase(1) // Must have uppercase letters
.has().lowercase(1) // Must have lowercase letters
.has().digits(1)
.has().symbols(1) // Must have at least 2 digits/
.has().not().spaces();
// Flash message
$("#message-cross").on("click", function () {
$("#message").parent()[0].style.display = "none";
});
$('#master-key').focus();
// submit
function submitBtnHandler() {
enteredPass = document.querySelector("#master-key").value;
if (isMasterPassword) {
if (enteredPass === masterKey) {
ipc.send('load-page', 'file://' + __dirname + '/index.html');
} else {
// show error msg
$("#message").parent()[0].style.display = "flex";
$("#message").text("Wrong Password !!");
}
}
else {
if (schema.validate(enteredPass)) {
Confirm(
'Alert',
'Are you sure you want to set this as master password?\nThere is no way back if you forget it !!',
'Yes, I\'m Sure',
'Cancel',
function(isConfirm){
if(isConfirm){
handleData.loadJsonData(function (jsonData) {
let data = jsonData;
data.masterKey = cryp.encrypt(enteredPass);
// console.log(jsonData);
fs.writeFile(dataStorePath, JSON.stringify(jsonData, null, 2), function (err) {
if (err) {
console.log(err);
}
ipc.send('load-page', 'file://' + __dirname + '/index.html');
});
});
}
else{
return;
}
}
);
} else {
// show error message
$("#message").parent()[0].style.display = "flex";
$("#message").text("Password must be 6-100 characters long and contain at least 1 upper case letter, 1 number, 1 special character");
}
}
}
handleData.loadJsonData(function(jsonData){
if (jsonData.masterKey) {
isMasterPassword = true;
masterKey = cryp.decrypt(jsonData.masterKey);
titleHead.innerHTML = "Enter your Master Password"
} else {
titleHead.innerHTML = "Create your Master Password"
}
});
// submit button click
submitBtn.addEventListener('click', submitBtnHandler);
function keyPressHandler(e) {
if (e.keyCode === 13) {
e.preventDefault();
submitBtnHandler();
}
}
$('.unmask').on('click', function () {
if ($(this).prev('input').attr('type') == 'password')
$(this).prev('input').prop('type', 'text');
else
$(this).prev('input').prop('type', 'password');
return false;
});
$('.password').on('keyup', function (e) {
// remove the error message only if enter key is not pressed
e.keyCode !== 13 ? $("#message").parent()[0].style.display = "none" : null;
var p = $('#master-key');
if (p.val().length == 0) {
$('#strong span').removeClass().html("");
}
else {
let pwdStrength = passwordStrength(p.val()).value.toLowerCase();
$('#strong span').removeClass();
$('#strong span').addClass(pwdStrength).html(pwdStrength);
}
});