-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (108 loc) · 2.81 KB
/
app.js
File metadata and controls
153 lines (108 loc) · 2.81 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
console.log("Starting the app");
// for local storage
var storage = require("node-persist");
// init storage
storage.initSync();
// for encryption
var crypto = require('crypto-js');
// for accepting command line inputs
var argv = require('yargs')
.command('create','create user account',function(yargs){
yargs.options({
name: {
demand: true,
alias : 'n',
description : 'Enter account name',
type :'string'
},
userName :{
demand:true,
alias : 'u',
description: 'Choose username name',
type : 'string'
},
password :{
demand:true,
alias : 'p',
description: 'your last name',
type : 'string'
},
masterPassword:{
demand:true,
alias:'m',
description:'This is the master password you have to provide',
type:'string'
}
}).help()
})
.command('get','Get user account',function(yargs){
yargs.options({
name : {
demand : true,
alias : 'n',
description : 'Name of account ',
type : 'string'
},
masterPassword:{
demand:true,
alias:'m',
description:'This is the master password you have to provide',
type:'string'
}
}).help()
}).help().argv
function getAccounts(masterPassword){
var encryptedAccounts = storage.getItemSync('accounts');
var accounts = [];
if(typeof encryptedAccounts !== 'undefined'){
var bytes = crypto.AES.decrypt(encryptedAccounts,masterPassword);
accounts = JSON.parse(bytes.toString(crypto.enc.Utf8));
}
return accounts;
}
function saveAccounts(accounts,masterPassword){
var arrayString = JSON.stringify(accounts);
var enctyptedAccounts = crypto.AES.encrypt(arrayString,masterPassword);
storage.setItemSync('accounts',enctyptedAccounts.toString());
return accounts;
}
function createAccount(account,masterPassword){
var accounts = getAccounts(masterPassword);
accounts.push(account);
saveAccounts(accounts,masterPassword);
return account;
}
function getAccount(accountName,masterPassword){
var accounts = getAccounts(masterPassword);
var matched;
accounts.forEach(function(data){
if(data.name === accountName){
matched = data;
}
});
return matched;
}
var command = argv._[0];
if(command === 'create'){
try{
var account = {
name:argv.n,
userName:argv.u,
password:argv.p
}
var newAccount = createAccount(account,argv.masterPassword);
}catch(e){
console.log('Unable to create acccount dute to error:'+e.message);
}
}else if(command === 'get'){
try{
var account = getAccount(argv.name,argv.masterPassword);
if(typeof account != 'undefined'){
console.log(account);
}else{
console.log('Account not found');
}
}catch(e){
console.log('Unable to get account duo to error:'+e.message);
}
}