wip(backend): Added MFA support
This commit is contained in:
parent
36726dbdc3
commit
a5ee0a0854
13 changed files with 380 additions and 14 deletions
30
sites/backend/src/utils/mfa.mjs
Normal file
30
sites/backend/src/utils/mfa.mjs
Normal file
|
@ -0,0 +1,30 @@
|
|||
import qrcode from 'qrcode'
|
||||
import { authenticator } from '@otplib/preset-default'
|
||||
|
||||
const dark = '#AAAAAA'
|
||||
const light = '#EEEEEE'
|
||||
/*
|
||||
* Exporting this closure that makes sure we have access to the
|
||||
* instantiated config
|
||||
*/
|
||||
export const mfa = ({ service }) => ({
|
||||
mfa: {
|
||||
enroll: async (user) => {
|
||||
const secret = authenticator.generateSecret()
|
||||
const otpauth = authenticator.keyuri(user, service, secret)
|
||||
let svg
|
||||
try {
|
||||
svg = await qrcode.toString(otpauth, {
|
||||
type: 'svg',
|
||||
color: { dark, light },
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
svg = svg.replace(dark, 'currentColor').replace(light, 'none')
|
||||
|
||||
return { secret, otpauth, qrcode: svg }
|
||||
},
|
||||
verify: (token, secret) => authenticator.check(token, secret),
|
||||
},
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue