From 515901977a1f81aad993b54904a0e1d730e7d864 Mon Sep 17 00:00:00 2001 From: Gerald Yeo Date: Fri, 24 Jan 2020 00:21:50 +0800 Subject: [PATCH] fix: convert to ascii before thirty-two encode --- .github/workflows/main.yml | 1 + .prettierignore | 1 - README.md | 2 +- .../otplib-plugin-thirty-two/src/index.ts | 2 +- tests/extras/issue-201.test.js | 28 +++++++++++++++++++ 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/extras/issue-201.test.js diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 922b39fd..0464f391 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,6 +25,7 @@ jobs: run: | npm run setup npm run test + run: npm run test:extras -- --type=issue env: PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true diff --git a/.prettierignore b/.prettierignore index 01799400..6d8fe53f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,7 +1,6 @@ .cache builds externals -extras node_modules package-lock.json public diff --git a/README.md b/README.md index 4dc0fa85..66740b4c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ It implements both [HOTP][rfc-4226-wiki] - [RFC 4226][rfc-4226] and [TOTP][rfc-6238-wiki] - [RFC 6238][rfc-6238], and are tested against the test vectors provided in their respective RFC specifications. -These datasets can be found in the `packages/tests-data` folder. +These datasets can be found in the `tests/data` folder. - [RFC 4226 Dataset][rfc-4226-dataset] - [RFC 6238 Dataset][rfc-6238-dataset] diff --git a/packages/otplib-plugin-thirty-two/src/index.ts b/packages/otplib-plugin-thirty-two/src/index.ts index 322328d7..a7c97baa 100644 --- a/packages/otplib-plugin-thirty-two/src/index.ts +++ b/packages/otplib-plugin-thirty-two/src/index.ts @@ -27,7 +27,7 @@ export const keyEncoder: KeyEncoder = ( encoding: KeyEncodings ): Base32SecretKey => { return thirtyTwo - .encode(Buffer.from(secret, encoding).toString()) + .encode(Buffer.from(secret, encoding).toString('ascii')) .toString() .replace(/=/g, ''); }; diff --git a/tests/extras/issue-201.test.js b/tests/extras/issue-201.test.js new file mode 100644 index 00000000..b45e67e4 --- /dev/null +++ b/tests/extras/issue-201.test.js @@ -0,0 +1,28 @@ +import { authenticatorGenerateSecret, KeyEncodings } from '@otplib/core'; +import { createRandomBytes } from '@otplib/plugin-crypto'; +import * as thirtyTwo from '@otplib/plugin-thirty-two'; +import * as base32EncDec from '@otplib/plugin-base32-enc-dec'; + +[ + { + name: 'thirty-two', + mod: thirtyTwo + }, + { + name: 'base32-enc-dec', + mod: base32EncDec + } +].forEach(({ name, mod }) => { + const options = { + createRandomBytes, + encoding: KeyEncodings.HEX, + keyEncoder: mod.keyEncoder, + keyDecoder: mod.keyDecoder + }; + + test(`[${name}] generated secret should have consistent length`, async () => { + for (let i = 0; i < 20; i++) { + expect(authenticatorGenerateSecret(10, options)).toHaveLength(16); + } + }); +});