forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify-send-authenticate-text-msg.js
More file actions
35 lines (31 loc) · 1 KB
/
Copy pathnotify-send-authenticate-text-msg.js
File metadata and controls
35 lines (31 loc) · 1 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
import crypto from 'crypto'
import {t} from '@lingui/macro'
const {CIPHER_KEY, NOTIFICATION_AUTHENTICATE_TEXT_ID} = process.env
export const sendAuthTextMsg = ({notifyClient, i18n}) => async ({user}) => {
const templateId = NOTIFICATION_AUTHENTICATE_TEXT_ID
const {iv, tag, phoneNumber: encryptedData} = user.phoneDetails
const decipher = crypto.createDecipheriv(
'aes-256-ccm',
String(CIPHER_KEY),
Buffer.from(iv, 'hex'),
{authTagLength: 16},
)
decipher.setAuthTag(Buffer.from(tag, 'hex'))
let phoneNumber = decipher.update(encryptedData, 'hex', 'utf8')
phoneNumber += decipher.final('utf8')
try {
await notifyClient.sendSms(templateId, phoneNumber, {
personalisation: {
tfa_code: user.tfaCode,
},
})
return true
} catch (err) {
console.error(
`Error occurred when sending authentication code via text for ${user._key}: ${err}`,
)
throw new Error(
i18n._(t`Unable to send authentication text message. Please try again.`),
)
}
}