Skip to content

Commit

Permalink
Merge pull request #3 from FinotiLucas/develop
Browse files Browse the repository at this point in the history
Update package and fix QR code generation issues
  • Loading branch information
brunolipe-a authored Jan 19, 2025
2 parents c5dc5c6 + 71ea765 commit 4e164ea
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/lock.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
ignoreUnless: {{ STALE_BOT }}
ignoreUnless: { { STALE_BOT } }
---
# Configuration for Lock Threads - https://github.com/dessant/lock-threads-app

Expand Down
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
ignoreUnless: {{ STALE_BOT }}
ignoreUnless: { { STALE_BOT } }
---
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 60
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
auto-install-peers=true
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Introduction

Two Factor Authentication using `node-2fa` integration for AdonisJS.
Two Factor Authentication using `2fa-node` integration for AdonisJS.

## Documentation

Expand All @@ -17,12 +17,9 @@ See documentation at [adonis-2fa.netlify.app](https://adonis-2fa.netlify.app)
AdonisJS 2FA is open-sourced software licensed under the [MIT license](LICENSE.md).

[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/nulix-dev/adonis-2fa/test.yml?style=for-the-badge
[gh-workflow-url]: https://github.com/nulix-dev/adonis-2fa/actions/workflows/test.yml "Github action"

[gh-workflow-url]: https://github.com/nulix-dev/adonis-2fa/actions/workflows/test.yml 'Github action'
[npm-image]: https://img.shields.io/npm/v/@nulix/adonis-2fa/latest.svg?style=for-the-badge&logo=npm
[npm-url]: https://www.npmjs.com/package/@nulix/adonis-2fa/v/latest "npm"

[npm-url]: https://www.npmjs.com/package/@nulix/adonis-2fa/v/latest 'npm'
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript

[license-url]: LICENSE.md
[license-image]: https://img.shields.io/github/license/nulix-dev/adonis-2fa?style=for-the-badge
2 changes: 1 addition & 1 deletion docs/bin/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function defineRoutes(app: ApplicationService) {
return response.redirect(redirectsCollection[request.url()])
}

for (let collection of collections) {
for (const collection of collections) {
await collection.refresh()
const entry = collection.findByPermalink(request.url())
if (entry) {
Expand Down
Binary file removed docs/content/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/content/docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ summary: Adonis-2FA is a library for managing Two Factor Authentication in your

# Introduction

Adonis-2FA is a library for managing Two Factor Authentication in your AdonisJS project build on top of [node-2fa](https://github.com/jeremyscalpello/node-2fa).
Adonis-2FA is a library for managing Two Factor Authentication in your AdonisJS project build on top of [2fa-node](https://github.com/FinotiLucas/2fa-node).

The package it self does not store any secret or data on your behalf. It only give you access the methods to implement a two factor authentication flow and create recovery codes. You can store that information inside a database and use the [auth](https://docs.adonisjs.com/guides/auth) package to login the user within your application.

Expand Down
13 changes: 13 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'

/** @type {import('eslint').Linter.Config[]} */
export default [
{ files: ['**/*.{js,mjs,cjs,ts}'] },
{
ignores: ['**/bin/*', '**/docs/*', '**/tests/*', 'tsconfig.json'],
},

pluginJs.configs.recommended,
...tseslint.configs.recommended,
]
70 changes: 37 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"clean": "del-cli build",
"copy:templates": "copyfiles \"stubs/**/*.stub\" build",
"typecheck": "tsc --noEmit",
"lint": "eslint . --ext=.ts",
"lint": "eslint --fix",
"format": "prettier --write .",
"test": "node --import=./tsnode.esm.js --enable-source-maps bin/test.ts",
"prec8:test": "npm run lint",
Expand All @@ -38,35 +38,49 @@
"version": "npm run build",
"prepublishOnly": "npm run build"
},
"keywords": [
"adonisjs",
"2fa",
"adonis"
],
"author": "brunolipe-a, nulix",
"license": "MIT",
"devDependencies": {
"@adonisjs/assembler": "^7.0.0",
"@adonisjs/core": "^6.2.0",
"@adonisjs/eslint-config": "^1.2.1",
"@adonisjs/prettier-config": "^1.2.1",
"@adonisjs/tsconfig": "^1.2.1",
"@japa/assert": "^2.1.0",
"@japa/runner": "^3.1.1",
"@swc/core": "^1.3.102",
"@types/node": "^20.10.7",
"c8": "^9.0.0",
"@adonisjs/assembler": "^7.8.2",
"@adonisjs/core": "^6.17.1",
"@adonisjs/eslint-config": "^1.3.0",
"@adonisjs/prettier-config": "^1.4.0",
"@adonisjs/tsconfig": "^1.4.0",
"@eslint/js": "^9.18.0",
"@japa/assert": "^4.0.1",
"@japa/runner": "^4.1.0",
"@swc/core": "^1.10.7",
"@types/node": "^22.10.7",
"c8": "^10.1.3",
"copyfiles": "^2.4.1",
"del-cli": "^5.0.0",
"eslint": "^8.38.0",
"np": "^9.2.0",
"prettier": "^3.1.1",
"del-cli": "^6.0.0",
"eslint": "^9.18.0",
"np": "^10.1.0",
"prettier": "^3.4.2",
"prettier-edgejs": "^0.2.34",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.7.3",
"typescript-eslint": "^8.20.0"
},
"dependencies": {
"2fa-node": "^0.0.5"
},
"peerDependencies": {
"@adonisjs/core": "^6.2.0"
},
"author": "brunolipe-a, nulix",
"license": "MIT",
"keywords": [
"adonisjs",
"2fa",
"adonis"
],
"types": "module",
"eslintConfig": {
"extends": "@adonisjs/eslint-config/package",
"ignorePatterns": [
"docs/"
]
},
"prettier": "@adonisjs/prettier-config",
"publishConfig": {
"access": "public",
"tag": "latest"
Expand All @@ -87,15 +101,5 @@
"src/**",
"!src/types.ts"
]
},
"eslintConfig": {
"extends": "@adonisjs/eslint-config/package",
"ignorePatterns": [
"docs/"
]
},
"prettier": "@adonisjs/prettier-config",
"dependencies": {
"node-2fa": "^2.0.3"
}
}
1 change: 1 addition & 0 deletions providers/two_factor_auth_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class TwoFactorAuthProvider {
/**
* Resolve config from the provider
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const config = await configProvider.resolve<any>(this.app, twoFactorConfigProvider)
if (!config) {
throw new RuntimeException(
Expand Down
10 changes: 6 additions & 4 deletions src/two_factor_auth_manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as twoFactor from 'node-2fa'
import * as twoFactor from '2fa-node'

import { ResolvedTwoFactorAuthConfig, TwoFactorSecret } from './types.js'
import { randomInt } from 'node:crypto'
Expand All @@ -9,10 +9,11 @@ export class TwoFactorAuthManager {
/**
* Generate a `Secret` to the given user information
*/
generateSecret(userInfo: string): TwoFactorSecret {
return twoFactor.generateSecret({
async generateSecret(userInfo: string): Promise<TwoFactorSecret> {
return await twoFactor.generateSecret({
name: this.config.issuer,
account: userInfo,
counter: undefined,
})
}

Expand All @@ -29,13 +30,14 @@ export class TwoFactorAuthManager {
*/
verifyToken(secret: string = '', token: string, recoveryCodes: string[] = []) {
const verifyResult = twoFactor.verifyToken(secret, token)

if (!verifyResult) {
const isSecretInRecoveryCodes = recoveryCodes.includes(token)

return isSecretInRecoveryCodes
}

return verifyResult.delta === 0 // Valida token atual, não permitindo token já expirado ou token futuro
return verifyResult
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/two_factor_auth_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test.group('TwoFactorAuthManager', () => {
const email = '[email protected]'
const manager = new TwoFactorAuthManager({ issuer })

const twoFactorSecret = manager.generateSecret(email)
const twoFactorSecret = await manager.generateSecret(email)

assert.properties(twoFactorSecret, ['secret', 'uri', 'qr'])
assert.equal(twoFactorSecret.secret.length, 32)
Expand Down Expand Up @@ -46,7 +46,7 @@ test.group('TwoFactorAuthManager', () => {
test('it should be able to verify a valid secret and OTP', async ({ assert }) => {
const manager = new TwoFactorAuthManager({ issuer })

const { secret } = manager.generateSecret('any')
const { secret } = await manager.generateSecret('any')

const token = manager.generateToken(secret)!

Expand All @@ -58,7 +58,7 @@ test.group('TwoFactorAuthManager', () => {
test('it not should be able to verify a valid secret and invalid OTP', async ({ assert }) => {
const manager = new TwoFactorAuthManager({ issuer })

const { secret } = manager.generateSecret('any')
const { secret } = await manager.generateSecret('any')

const isValid = manager.verifyToken(secret, 'something')

Expand All @@ -68,7 +68,7 @@ test.group('TwoFactorAuthManager', () => {
test('it not should be able to verify a invalid secret and valid OTP', async ({ assert }) => {
const manager = new TwoFactorAuthManager({ issuer })

const { secret } = manager.generateSecret('any')
const { secret } = await manager.generateSecret('any')

const token = manager.generateToken(secret)!

Expand Down
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": "@adonisjs/tsconfig/tsconfig.package.json",
"compilerOptions": {
"target": "esnext",
"moduleResolution": "nodenext",
"rootDir": "./",
"outDir": "./build",
"outDir": "./build"
},
"exclude": [
"docs"
]
"exclude": ["docs"]
}

0 comments on commit 4e164ea

Please sign in to comment.