Skip to content

Commit

Permalink
feat: config code to docs folder and typo errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolipe-a committed Jan 27, 2024
1 parent d4b46e7 commit 55720dc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function configure(command: ConfigureCommand) {
}

const shouldMakeController = await command.prompt.confirm(
'Do you want to create controller and routes for a basic 2FA flow?',
'Do you want to create controller and routes for a basic 2FA flow (API)?',
{
default: false,
hint: 'You should be using @adonisjs/auth',
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@
"html"
],
"exclude": [
"tests/**"
"tests/**",
"docs/**"
]
},
"eslintConfig": {
"extends": "@adonisjs/eslint-config/package"
"extends": "@adonisjs/eslint-config/package",
"ignorePatterns": [
"docs/"
]
},
"prettier": "@adonisjs/prettier-config",
"dependencies": {
Expand Down
30 changes: 25 additions & 5 deletions src/two_factor_auth_manager.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as twoFactor from 'node-2fa'

import stringHelpers from '@adonisjs/core/helpers/string'
import { ResolvedTwoFactorAuthConfig } from './types.js'
import { ResolvedTwoFactorAuthConfig, TwoFactorSecret } from './types.js'
import { randomInt } from 'node:crypto'

export class TwoFactorAuthManager {
constructor(private config: ResolvedTwoFactorAuthConfig) {}

generateSecret(account: string) {
generateSecret(account: string): TwoFactorSecret {
return twoFactor.generateSecret({
name: this.config.issuer,
account,
})
}

generateRecoveryCodes() {
return Array.from({ length: 16 }, () => stringHelpers.generateRandom(10).toUpperCase())
generateRecoveryCodes(codeLength = 16) {
return Array.from({ length: codeLength }, () => this.generateRecoveryCode(10))
}

verifyToken(secret: string = '', token: string, recoveryCodes: string[] = []) {
Expand All @@ -31,4 +31,24 @@ export class TwoFactorAuthManager {
generateToken(secret: string) {
return twoFactor.generateToken(secret)
}

private generateRandomChar() {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
const randomIndex = randomInt(0, charset.length)
return charset[randomIndex]
}

private generateRecoveryCode(length: number) {
let recoveryCode = ''

for (let i = 0; i < length; i++) {
recoveryCode += this.generateRandomChar()
}

// Inserir um espaço no meio
const middleIndex = Math.floor(length / 2)
recoveryCode = `${recoveryCode.substring(0, middleIndex)} ${recoveryCode.substring(middleIndex)}`

return recoveryCode
}
}
2 changes: 1 addition & 1 deletion stubs/make/controller/two_factor_controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { HttpContext } from '@adonisjs/core/http'

import { verifyOtpValidator } from '#validators/verify_otp'

export default class TwoFactorsController {
export default class TwoFactorAuthController {
async generate({ auth }: HttpContext) {
const user = auth.user!

Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"compilerOptions": {
"rootDir": "./",
"outDir": "./build",
}
},
"exclude": [
"docs"
]
}

0 comments on commit 55720dc

Please sign in to comment.