-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
130 lines (99 loc) · 3.09 KB
/
client.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
const crypto = require('crypto');
const got = require('got');
const KeyGenerator = require('./createKeyPair');
const Encrypter = require('./encrypter');
let keyPair = {};
const hasher = crypto.createHash('sha256');
if (require.main === module) {
const Readline = require('readline');
const readline = Readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question1 = () => {
return new Promise((resolve, reject) => {
const msg =
'Would You like to create an asymmetic keypair? (default: Yes) ';
readline.question(msg, (answer) => {
const sanitizedString = answer.toLowerCase();
if (sanitizedString === '') {
resolve(KeyGenerator.createKeyPair());
return;
}
if (sanitizedString !== 'yes' && sanitizedString !== 'no') {
return reject(new Error("String must be 'yes' or 'no'."));
}
resolve(KeyGenerator.createKeyPair());
});
});
};
const question2 = () => {
return new Promise((resolve, reject) => {
const msg = 'Enter password to submit publicKey. ';
readline.question(msg, (answer) => {
const sanitizedString = answer.toLowerCase();
if (sanitizedString === '') {
resolve(new Error('You cannot enter an empty string.'));
return;
}
resolve(sanitizedString.trim());
});
});
};
const question3 = () => {
return new Promise((resolve, reject) => {
const msg = 'Type message to send to sever for verification. ';
readline.question(msg, (answer) => {
const sanitizedString = answer.trim();
if (sanitizedString === '') {
resolve(new Error('You cannot enter an empty string.'));
return;
}
hasher.update(sanitizedString);
const hashedData = hasher.digest('hex');
const signedMsg = Encrypter.encryptWithPrivateKey(keyPair.privateKey, hashedData);
const data = {
originalMsg: sanitizedString,
signedMsg
}
resolve(data);
});
});
};
async function run() {
keyPair = await question1();
const enteredPassword = await question2();
try {
const signUpResponse = await got.post('http://localhost:8080/signUp', {
headers: {
password: enteredPassword
},
json: {
publicKey: keyPair.publicKey
}
}).json();
console.log(signUpResponse);
const dataPackage = await question3();
const verifyResponse = await got.post('http://localhost:8080/verify', {
json: dataPackage
}).json();
const successResponse = {
...verifyResponse,
data: {
originalMsg: verifyResponse.data.originalMsg,
signedMsg: Buffer.from(verifyResponse.data.signedMsg).toString()
}
}
readline.close();
} catch (error) {
if (error.response.statusCode === 401) {
throw new Error('Unauthorized');
}
throw error;
}
}
run().catch((err) => {
console.error(err.message);
process.exit(1);
});
}