-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2fa): implementing backend code for TOTP
- Loading branch information
1 parent
a67be7f
commit 5b5c9ab
Showing
20 changed files
with
786 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
infra/migrations/1725497855500_alter-table-users-add-mfa-totp-secret.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
exports.up = (pgm) => { | ||
pgm.addColumns('users', { | ||
totp_secret: { | ||
type: 'varchar(128)', | ||
notNull: false, | ||
}, | ||
}); | ||
}; | ||
|
||
exports.down = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import crypto from 'crypto'; | ||
import * as OTPAuth from 'otpauth'; | ||
|
||
const defaultTOTPConfigurations = { | ||
issuer: 'TabNews', | ||
algorithm: 'SHA1', | ||
digits: 6, | ||
}; | ||
|
||
function createSecret() { | ||
return new OTPAuth.Secret({ size: 20 }).base32; | ||
} | ||
|
||
function createTotp(secret, username) { | ||
if (!secret) { | ||
secret = createSecret(); | ||
} | ||
return new OTPAuth.TOTP({ ...defaultTOTPConfigurations, secret, label: username }); | ||
} | ||
|
||
const cryptoConfigurations = { | ||
algorithm: process.env.TOTP_ENCRYPTION_METHOD, | ||
key: crypto.createHash('sha512').update(process.env.TOTP_SECRET_KEY).digest('hex').substring(0, 32), | ||
encryptionIV: crypto.createHash('sha512').update(process.env.TOTP_SECRET_IV).digest('hex').substring(0, 16), | ||
}; | ||
|
||
function encryptData(data) { | ||
const cipher = crypto.createCipheriv( | ||
cryptoConfigurations.algorithm, | ||
cryptoConfigurations.key, | ||
cryptoConfigurations.encryptionIV, | ||
); | ||
return Buffer.from(cipher.update(data, 'utf8', 'hex') + cipher.final('hex')).toString('base64'); | ||
} | ||
|
||
function decryptData(encryptedData) { | ||
const buff = Buffer.from(encryptedData, 'base64'); | ||
const decipher = crypto.createDecipheriv( | ||
cryptoConfigurations.algorithm, | ||
cryptoConfigurations.key, | ||
cryptoConfigurations.encryptionIV, | ||
); | ||
return decipher.update(buff.toString('utf8'), 'hex', 'utf8') + decipher.final('utf8'); | ||
} | ||
|
||
function validateUserTotp(userEncryptedSecret, token) { | ||
const userSecret = decryptData(userEncryptedSecret); | ||
const userTOTP = createTotp(userSecret); | ||
return userTOTP.validate({ token }) !== null; | ||
} | ||
|
||
function validateTotp(secret, token) { | ||
const totp = createTotp(secret); | ||
return totp.validate({ token }) !== null; | ||
} | ||
|
||
export default Object.freeze({ | ||
createTotp, | ||
createSecret, | ||
decryptData, | ||
encryptData, | ||
validateUserTotp, | ||
validateTotp, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import nextConnect from 'next-connect'; | ||
|
||
import { ServiceError } from 'errors'; | ||
import authentication from 'models/authentication.js'; | ||
import authorization from 'models/authorization.js'; | ||
import cacheControl from 'models/cache-control'; | ||
import controller from 'models/controller.js'; | ||
import otp from 'models/otp'; | ||
|
||
export default nextConnect({ | ||
attachParams: true, | ||
onNoMatch: controller.onNoMatchHandler, | ||
onError: controller.onErrorHandler, | ||
}) | ||
.use(controller.injectRequestMetadata) | ||
.use(controller.logRequest) | ||
.get( | ||
cacheControl.noCache, | ||
authentication.injectAnonymousOrUser, | ||
authorization.canRequest('read:session'), | ||
getHandler, | ||
); | ||
|
||
function getHandler(request, response) { | ||
const username = request.context.user.username; | ||
const totp = otp.createTotp(null, username); | ||
|
||
try { | ||
response.status(200).json({ totp: totp.toString() }); | ||
} catch (err) { | ||
throw new ServiceError({ | ||
message: 'Não foi possível gerar um TOTP no momento.', | ||
action: 'Tente novamente mais tarde.', | ||
stack: new Error().stack, | ||
errorLocationCode: 'CONTROLLER:MFA:TOTP:GET_HANDLER:GENERATE_TOTP', | ||
key: 'totp', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.