-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchallenge-manager.js
52 lines (39 loc) · 1.15 KB
/
challenge-manager.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
const twilio = require('twilio');
class ChallengeManager {
constructor() {
this.client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
this.challenges = new Map();
}
async create(user, fields) {
console.log('identity: ', user.id);
const details = JSON.stringify({
message: 'Please approve the login request',
fields: fields
});
const challenge = await this.client.verify
.services(process.env.TWILIO_VERIFY_SERVICE_SID)
.entities(user.id)
.challenges.create({
factorSid: user.factor.sid,
details: details
});
this.challenges.set(challenge.sid, {
status: challenge.status,
socket: undefined
});
return challenge;
}
get(sid) {
return this.challenges.get(sid);
}
update(sid, status) {
const { socket } = this.challenges.get(sid);
this.challenges.set(sid, { socket, status });
socket && socket.send(JSON.stringify({ status: status }));
}
registerSocket(sid, socket) {
const { status } = this.challenges.get(sid);
this.challenges.set(sid, { status, socket });
}
}
module.exports = ChallengeManager;