CoreID/app/controllers/auth/MFA.controller.js

77 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2020-04-22 21:56:39 +00:00
const { Controller } = require('libflitter')
class MFAController extends Controller {
static get services() {
2020-05-04 01:16:54 +00:00
return [...super.services, 'Vue', 'configs', 'models']
2020-04-22 21:56:39 +00:00
}
async setup(req, res, next) {
if ( req.user.mfa_enabled ) {
// Already set up!
return this.Vue.auth_message(res, {
message: req.T('auth.already_mfa'),
next_destination: '/dash/profile',
2020-04-22 21:56:39 +00:00
button_text: 'Okay',
})
}
// Display the token setup page
return res.page('auth:mfa:setup', {
...this.Vue.data()
})
}
async challenge(req, res, next) {
if ( !req.user.mfa_enabled ) {
return this.Vue.auth_message(res, {
message: req.T('auth.mfa_prompt'),
2020-04-22 21:56:39 +00:00
next_destination: '/auth/mfa/setup',
button_text: 'Setup MFA',
})
}
if ( !req.trap.has_trap('mfa_challenge') ) {
2020-04-22 21:56:39 +00:00
return res.redirect(req.session.auth.flow)
}
// Display the MFA challenge page
return res.page('auth:mfa:challenge', {
...this.Vue.data()
})
}
2020-05-04 01:16:54 +00:00
async get_disable(req, res, next) {
return this.Vue.confirm(res, {
message: req.T('auth.mfa_disable_prompt'),
2020-05-04 01:16:54 +00:00
yes: '/auth/mfa/disable/process',
no: '/dash/profile',
})
}
async do_disable(req, res, next) {
return res.page('auth:mfa:disable', {
...this.Vue.data(),
...this.Vue.session(req),
})
}
2020-05-30 22:21:47 +00:00
async get_recovery(req, res, next) {
if (
!req.user.mfa_enabled
|| !Array.isArray(req.user.mfa_token.recovery_codes)
|| req.user.mfa_token.recovery_codes.length < 1
) return this.Vue.auth_message(res, {
message: req.T('auth.mfa_no_recovery'),
2020-05-30 22:21:47 +00:00
next_destination: '/auth/mfa/challenge',
button_text: 'Go Back',
})
return res.page('auth:mfa:recovery', {
...this.Vue.data(),
...this.Vue.session(req),
})
}
2020-04-22 21:56:39 +00:00
}
module.exports = exports = MFAController