/
app.js
226 lines (198 loc) · 5.64 KB
/
app.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Dependencies
const fs = require("fs");
const crypto = require("crypto");
const fetch = require("node-fetch");
const bodyParser = require("body-parser");
// Globals
const API_TOKEN = process.env.API_TOKEN;
const API_SECRET = process.env.API_SECRET;
const API_URL = process.env.API_URL || "https://api.veriff.me/v1";
const WEBHOOK_PORT = process.env.WEBHOOK_PORT;
const IMAGE_DIR = process.env.IMAGE_DIR || "documents";
if (!API_TOKEN) throw "API_TOKEN environment variable is required";
if (!API_SECRET) throw "API_SECRET environment variable is required";
// Initialize
(async () => {
try {
const files = fs
.readdirSync("./" + IMAGE_DIR)
.filter((file) => file.match(/.*\.(jpg|jpeg|png)/gi));
const session = await start();
const verificationId = session.verification.id;
console.log("Started verification with an id of", verificationId);
const uploads = files.map(async (file) => {
await upload(verificationId, file);
console.log("Uploaded file", file);
});
Promise.all(uploads).then(async () => {
await end(verificationId);
console.log("Ended verification");
console.log("Getting media for the verification session");
const media = await getMedia(verificationId);
console.log(JSON.stringify(media, null, 4));
});
} catch (err) {
console.log(err);
}
})();
async function start() {
try {
const payload = {
verification: {
person: {
firstName: "Nican Onio",
lastName: "Xander [EXAMPLE]",
idNumber: "001-1505561-1",
},
document: {
number: "VL0199336",
type: "PASSPORT",
country: "DO",
},
lang: "en",
features: ["selfid"],
timestamp: timestamp(),
},
};
const headers = {
"x-auth-client": API_TOKEN,
"x-hmac-signature": generateSignature(payload, API_SECRET),
"content-type": "application/json",
};
const options = {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
};
const response = await fetch(API_URL + "/sessions", options);
return await response.json();
} catch (err) {
console.log("verify method error", err);
}
}
async function upload(verificationId, file) {
try {
const payload = {
image: {
context: file.split(".")[0],
content: readImage(IMAGE_DIR + "/" + file),
timestamp: timestamp(),
},
};
const headers = {
"x-auth-client": API_TOKEN,
"x-hmac-signature": generateSignature(payload, API_SECRET),
"content-type": "application/json",
};
const options = {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
};
const response = await fetch(
API_URL + "/sessions/" + verificationId + "/media",
options
);
const json = await response.json();
return response;
} catch (err) {
console.log("upload method error", err);
}
}
async function getMedia(verificationId) {
try {
const headers = {
"x-auth-client": API_TOKEN,
"x-hmac-signature": generateSignature(verificationId, API_SECRET),
};
const options = { method: "GET", headers: headers };
const response = await fetch(
API_URL + "/sessions/" + verificationId + "/media",
options
);
return await response.json();
} catch (err) {
console.log("getMedia method error", err);
}
}
async function end(verificationId) {
try {
const payload = {
verification: {
frontState: "done",
status: "submitted",
timestamp: timestamp(),
},
};
const headers = {
"x-auth-client": API_TOKEN,
"x-hmac-signature": generateSignature(payload, API_SECRET),
"content-type": "application/json",
};
const options = {
method: "PATCH",
headers: headers,
body: JSON.stringify(payload),
};
const response = await fetch(
API_URL + "/sessions/" + verificationId,
options
);
return await response.json();
} catch (err) {
console.log("done method error", err);
}
}
function timestamp() {
return new Date().toISOString();
}
function readImage(file) {
const bitmap = fs.readFileSync(file);
return Buffer.from(bitmap).toString("base64");
}
function generateSignature(payload, secret) {
if (payload.constructor === Object) {
payload = JSON.stringify(payload);
}
if (payload.constructor !== Buffer) {
payload = Buffer.from(payload, "utf8");
}
const signature = crypto.createHmac("sha256", secret);
signature.update(payload);
return signature.digest("hex");
}
function isSignatureValid(data) {
const { signature, secret } = data;
let { payload } = data;
if (data.payload.constructor === Object) {
payload = JSON.stringify(data.payload);
}
if (payload.constructor !== Buffer) {
payload = new Buffer.from(payload, "utf8");
}
const hash = crypto.createHmac("sha256", secret);
hash.update(payload);
const digest = hash.digest("hex");
return digest === signature.toLowerCase();
}
// Receive a web hook locally
if (WEBHOOK_PORT) {
const express = require("express");
const app = express();
app.use(bodyParser.json());
let server = require("http").Server(app);
app.post("/verification/", (req, res) => {
const signature = req.get("x-hmac-signature");
const secret = API_SECRET;
const payload = req.body;
console.log("Received a webhook");
console.log(
"Validated signature:",
isSignatureValid({ signature, secret, payload })
);
console.log("Payload", JSON.stringify(payload, null, 4));
res.json({ status: "success" });
process.exit();
});
server.listen(WEBHOOK_PORT);
}