Repository navigation
Expand file tree
/
Copy pathentraPayload.js
More file actions
64 lines (57 loc) · 2.43 KB
/
Copy pathentraPayload.js
File metadata and controls
64 lines (57 loc) · 2.43 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
'use strict';
const { inspect } = require('node:util');
const SUPPORTED_TYPE = 'microsoft.mfa.otpDeliver.v1';
const CHANNEL = Object.freeze({ SMS: 1, VOICE: 2 });
const MODE = Object.freeze({ LIVE: 1, EVALUATION: 2 });
const CHANNEL_BY_NAME = Object.freeze({ sms: CHANNEL.SMS, voice: CHANNEL.VOICE });
const MODE_BY_NAME = Object.freeze({ live: MODE.LIVE, evaluation: MODE.EVALUATION });
function normalizeEnum(value, names, first, second) {
if (value === first || value === second) return value;
if (typeof value === 'string') return names[value.toLowerCase()] || null;
return null;
}
class EntraSendOtpPayload {
constructor({ type, tenantId, correlationId, channel, mode, ttlSeconds, encryptedDeliveryContext }) {
this.type = type;
this.tenantId = tenantId;
this.correlationId = correlationId;
this.channel = channel;
this.mode = mode;
this.ttlSeconds = ttlSeconds;
this.encryptedDeliveryContext = encryptedDeliveryContext;
Object.freeze(this);
}
get channelName() { return this.channel === CHANNEL.VOICE ? 'voice' : 'sms'; }
get isEvaluation() { return this.mode === MODE.EVALUATION; }
[inspect.custom]() { return '[EntraSendOtpPayload]'; }
}
function parseEntraPayload(value) {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
return { error: 'invalid envelope' };
}
if (value.type !== SUPPORTED_TYPE) {
return { error: 'unsupported envelope type' };
}
if (typeof value.encryptedDeliveryContext !== 'string'
|| !value.encryptedDeliveryContext.trim()) {
return { error: 'encryptedDeliveryContext is required' };
}
const channel = normalizeEnum(value.channel, CHANNEL_BY_NAME, CHANNEL.SMS, CHANNEL.VOICE);
if (!channel) return { error: 'unsupported channel' };
const mode = normalizeEnum(value.mode, MODE_BY_NAME, MODE.LIVE, MODE.EVALUATION);
if (!mode) return { error: 'unsupported mode' };
if (Object.hasOwn(value, 'ttlSeconds')) {
if (!Number.isInteger(value.ttlSeconds) || value.ttlSeconds > 2147483647) {
return { error: 'invalid ttlSeconds' };
}
if (value.ttlSeconds <= 0) return { error: 'ttlSeconds expired' };
}
return {
payload: new EntraSendOtpPayload({
...value,
channel,
mode,
}),
};
}
module.exports = { EntraSendOtpPayload, parseEntraPayload, CHANNEL, MODE, SUPPORTED_TYPE };