-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathusersArgon.js
More file actions
204 lines (178 loc) · 7.17 KB
/
usersArgon.js
File metadata and controls
204 lines (178 loc) · 7.17 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const assert = require('assert');
const db = require('./db');
const crypto = require('crypto');
const argon2i = require('argon2-ffi').argon2i
const UIControls = require('./UIControlGroups');
const userUIControls = new UIControls();
let msg = '';
let collection = '';
let TimeNow;
const PASSWORD_LENGTH = 256;
const SALT_LENGTH = 128;
const ITERATIONS = 10000;
const DIGEST = 'sha256'; // hashing algorithm
const BYTE_TO_STRING_ENCODING = 'base64';
// verify syntax for moving function inside class then I can call from
// micro-services.
class User {
constructor(obj) {
for ( let key in obj ) {
this [key] = obj [key];
}
}
static failResponse (err, res) {
const response = { success : 'FAIL',
message : err };
console.log(JSON.stringify(response));
res.status(403).send(JSON.stringify(response));
}
static successResponse(err, res) {
const response = { success : 'OK',
message : err };
console.log(JSON.stringify(response));
res.writeHead(200, {'Content-Type': 'JSON'});
res.end(JSON.stringify(response));
}
static formatDateTime(date) {
var d = new Date(date),
year = d.getFullYear(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
hour = '' + d.getHours(),
min = '' + d.getMinutes(),
sec = '' + d.getSeconds();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
if (hour.length < 2) hour = '0' + hour;
if (min.length < 2) min = '0' + min;
if (sec.length < 2) sec = '0' + sec;
let dat = [year, month, day].join('-');
let time = [ hour, min, sec ].join(':');
let dateTime = dat + ' ' + time
return dateTime;
}
successHTMLResponse(err, res, loginInfo) {
const response = { success : 'OK',
message : err };
console.log(JSON.stringify(response));
userUIControls.LoadPage(loginInfo).then( function(data) {
msg = 'authenticate response: \n' + data;
console.log(msg);
res.writeHead(200, {'Content-Type': 'HTML'});
res.end(data);
}, function(error) {
console.log(error);
});
}
hashPassword(password) {
return new Promise(function(resolve, reject) {
var salt = crypto.randomBytes(SALT_LENGTH); // .toString(BYTE_TO_STRING_ENCODING);
msg = "\n salt: <" + salt.toString(BYTE_TO_STRING_ENCODING) + "> \n saltLength: <" + SALT_LENGTH + "> \n encoding: <" + BYTE_TO_STRING_ENCODING + ">";
msg += "\n password : <" + password + ">";
console.log(msg);
const pwd = new Buffer.from(password)
// const options = {
// timeCost: 4,
// memoryCost: 1 << 14,
// parallelism: 2,
// hashLength: 256
// }
//argon2i.hash( password, salt, options).then((hash) => {
argon2i.hash( pwd, salt).then((hash) => {
console.log(hash);
resolve(hash);
}, (err) => {
console.log(err)
reject(err)
})
})
}
isPasswordCorrect( loginInfo, passwordAttempt ) {
return new Promise(function(resolve, reject) {
const savedHash = loginInfo[0].hash;
const pwd = new Buffer.from(passwordAttempt)
argon2i.verify(savedHash, pwd).then(
(correct) => { resolve(correct)},
(error) => { reject(error) }
)
});
}
getUserByName(userName) {
let query;
query = '{ "login" : "' + userName + '" }';
collection = 'users'
//console.log(JSON.parse(query));
//console.log(collection);
return new Promise(function(resolve, reject) {
db().then(() => {
db.Article.find(JSON.parse(query), collection).then((docs, err) => {
assert.equal(err, null);
msg = 'getUserByName: Retrieving Data from Mongodb collection: ' + collection + '\n';
msg += 'getUserByName: User details retrieved for: ' + userName + '\n';
//console.log(msg);
//console.log(docs);
if (docs) {
resolve(docs);
} else {
reject(Error("login info not found"));
}
});
});
});
}
authenticate(login, password) {
TimeNow = new Date();
msg = '\n Authentication Request recieved at: ' + TimeNow + '\n';
console.log(msg);
let that = this;
return new Promise( function(resolve, reject){
that.getUserByName(login).then( function(gubnResponse) {
msg = '\n response length: <' + gubnResponse.length + '>';
//console.log(msg);
console.log(gubnResponse);
if ( gubnResponse.length > 0 ) {
that.isPasswordCorrect( gubnResponse, password ).then( function(ipcResponse ) {
//console.log(ipcResponse);
if (ipcResponse) {
resolve(gubnResponse)
} else {
reject('isPasswordCorrect: User not authenticated invalid password')
}
}, function(error) {
console.log(error);
reject('isPasswordCorrect: User not authenicated');
});
} else {
reject('getUserByName: User does not exist')
}
}, function(error){
console.log(error);
reject('getUserByName: User not authenticated');
});
});
}
saveUser( hash, userName, role ) {
return new Promise(function(resolve, reject){
let userRecord = '{ "login" : "' + userName;
userRecord += '", "role" : "' + role;
userRecord += '", "hash" : "' + hash + '" }';
msg += 'Creating User Record : \n' + userRecord + '\n';
console.log(msg);
collection='users';
db().then(() => {
db.Article.create(JSON.parse(userRecord), collection).then((userDocs, err) => {
assert.equal(err, null);
if (err) {
reject(Error("save: Unable to save login details to groups document"));
} else {
msg = 'save: Record inserted into Mongodb collection: ' + collection + '\n';
console.log(msg);
console.log("Inserted count: " + userDocs.insertedCount);
resolve(userDocs.insertedCount);
}
})
});
})
}
}
module.exports = User;