2020-04-22 21:56:39 +00:00
|
|
|
const { Service } = require('flitter-di')
|
|
|
|
const speakeasy = require('speakeasy')
|
|
|
|
const qrcode = require('qrcode')
|
|
|
|
|
|
|
|
class MFAService extends Service {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'configs']
|
|
|
|
}
|
|
|
|
|
|
|
|
secret(user) {
|
|
|
|
return speakeasy.generateSecret({
|
|
|
|
length: this.configs.get('auth.mfa.secret_length') ?? 20,
|
2020-10-19 04:27:23 +00:00
|
|
|
name: `${this.configs.get('app.name')} (${user.uid.toLowerCase()})`,
|
2020-04-22 21:56:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async qr_code(secret) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
qrcode.toDataURL(secret.otpauth_url, (err, image_data) => {
|
|
|
|
if ( err ) reject(err)
|
|
|
|
else resolve(image_data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = MFAService
|