From 1e8bd8607f10980ece095ed3351645e4315f0716 Mon Sep 17 00:00:00 2001 From: Gerald Yeo Date: Sat, 19 Aug 2017 12:35:14 +0800 Subject: [PATCH 1/2] fix google authenticator pre and post encoding --- src/impl/authenticator/check.js | 2 +- src/impl/authenticator/decodeKey.js | 7 ++----- src/impl/authenticator/encodeKey.js | 8 +++----- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/impl/authenticator/check.js b/src/impl/authenticator/check.js index c35c630b..67522215 100644 --- a/src/impl/authenticator/check.js +++ b/src/impl/authenticator/check.js @@ -16,7 +16,7 @@ function check(token, secret, options) { createHmacSecret: hotpSecret, ...options }; - return totpCheck(token, decodeKey(secret, opt.keyEncoding), opt); + return totpCheck(token, decodeKey(secret), opt); } export default check; diff --git a/src/impl/authenticator/decodeKey.js b/src/impl/authenticator/decodeKey.js index bed915f5..db7bd44a 100644 --- a/src/impl/authenticator/decodeKey.js +++ b/src/impl/authenticator/decodeKey.js @@ -5,13 +5,10 @@ import base32 from 'thirty-two'; * * @module impl/authenticator/decodeKey * @param {string} encodedKey - your encoded secret that is used to generate the token - * @param {string} format - any format supported by node's `Buffer` * @return {string} Decoded string */ -function decodeKey(encodedKey, format) { - const fmt = format || 'ascii'; - return base32.decode(encodedKey) - .toString(fmt); +function decodeKey(encodedKey) { + return base32.decode(encodedKey); } export default decodeKey; diff --git a/src/impl/authenticator/encodeKey.js b/src/impl/authenticator/encodeKey.js index ba92d4b1..cc65cb34 100644 --- a/src/impl/authenticator/encodeKey.js +++ b/src/impl/authenticator/encodeKey.js @@ -5,14 +5,12 @@ import base32 from 'thirty-two'; * * @module impl/authenticator/encodeKey * @param {string} secret - your secret that is used to generate the token - * @param {string} format - any format supported by node's `Buffer` * @return {string} Base32 string */ -function encodeKey(secret, format) { - const fmt = format || 'ascii'; - +function encodeKey(secret) { return base32.encode(secret) - .toString(fmt); + .toString() + .replace(/=/g, ''); } export default encodeKey; From e128286c561f40aea565b9d4a32934e9ea01bf98 Mon Sep 17 00:00:00 2001 From: Gerald Yeo Date: Sat, 19 Aug 2017 12:48:08 +0800 Subject: [PATCH 2/2] fix tests --- src/impl/authenticator/decodeKey.js | 2 +- src/v2.js | 4 +++- tests/specs/impl/authenticator/decodeKey.spec.js | 11 +++++------ tests/specs/impl/authenticator/encodeKey.spec.js | 9 ++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/impl/authenticator/decodeKey.js b/src/impl/authenticator/decodeKey.js index db7bd44a..cdd93499 100644 --- a/src/impl/authenticator/decodeKey.js +++ b/src/impl/authenticator/decodeKey.js @@ -5,7 +5,7 @@ import base32 from 'thirty-two'; * * @module impl/authenticator/decodeKey * @param {string} encodedKey - your encoded secret that is used to generate the token - * @return {string} Decoded string + * @return {Object} A Buffer object containing the decoded string. */ function decodeKey(encodedKey) { return base32.decode(encodedKey); diff --git a/src/v2.js b/src/v2.js index 02f2ac54..d75e2e15 100644 --- a/src/v2.js +++ b/src/v2.js @@ -76,7 +76,9 @@ Goog.prototype.qrcode = authenticator.qrcode; Goog.prototype.generate = authenticator.generate; Goog.prototype.check = withOptions(totp, 'check'); Goog.prototype.encode = authenticator.encode; -Goog.prototype.decode = authenticator.decode; +Goog.prototype.decode = function(...args) { + return authenticator.decode(...args).toString(); +} /** * Default Exports diff --git a/tests/specs/impl/authenticator/decodeKey.spec.js b/tests/specs/impl/authenticator/decodeKey.spec.js index a7c66a56..94671589 100644 --- a/tests/specs/impl/authenticator/decodeKey.spec.js +++ b/tests/specs/impl/authenticator/decodeKey.spec.js @@ -3,9 +3,9 @@ import {spy, stub} from 'sinon'; import decodeKey from 'src/impl/authenticator/decodeKey'; const codec = [ - ['testing secret key', 'ORSXG5DJNZTSA43FMNZGK5BANNSXS==='], - ['the quick brown fox', 'ORUGKIDROVUWG2ZAMJZG653OEBTG66A='], - ['mvomjsunp qwerty', 'NV3G63LKON2W44BAOF3WK4TUPE======'], + ['testing secret key', 'ORSXG5DJNZTSA43FMNZGK5BANNSXS'], + ['the quick brown fox', 'ORUGKIDROVUWG2ZAMJZG653OEBTG66A'], + ['mvomjsunp qwerty', 'NV3G63LKON2W44BAOF3WK4TUPE'], ['abcd efgh ijkl mnop qrstu', 'MFRGGZBAMVTGO2BANFVGW3BANVXG64BAOFZHG5DV'] ]; @@ -16,17 +16,16 @@ describe('impl/authenticator/decodeKey', function () { decodeKey.__Rewire__('base32', {decode}); - decodeKey('test', 'hex'); + decodeKey('test'); decodeKey.__ResetDependency__('base32'); expect(decode.calledWith('test')); - expect(toString.calledWith('hex')); }); it('should return expected values', function () { codec.forEach((entry) => { - expect(decodeKey(entry[1])).to.be.equal(entry[0]); + expect(decodeKey(entry[1]).toString()).to.be.equal(entry[0]); }); }); }); diff --git a/tests/specs/impl/authenticator/encodeKey.spec.js b/tests/specs/impl/authenticator/encodeKey.spec.js index 0e769a11..79754f55 100644 --- a/tests/specs/impl/authenticator/encodeKey.spec.js +++ b/tests/specs/impl/authenticator/encodeKey.spec.js @@ -3,9 +3,9 @@ import {spy, stub} from 'sinon'; import encodeKey from 'src/impl/authenticator/encodeKey'; const codec = [ - ['testing secret key', 'ORSXG5DJNZTSA43FMNZGK5BANNSXS==='], - ['the quick brown fox', 'ORUGKIDROVUWG2ZAMJZG653OEBTG66A='], - ['mvomjsunp qwerty', 'NV3G63LKON2W44BAOF3WK4TUPE======'], + ['testing secret key', 'ORSXG5DJNZTSA43FMNZGK5BANNSXS'], + ['the quick brown fox', 'ORUGKIDROVUWG2ZAMJZG653OEBTG66A'], + ['mvomjsunp qwerty', 'NV3G63LKON2W44BAOF3WK4TUPE'], ['abcd efgh ijkl mnop qrstu', 'MFRGGZBAMVTGO2BANFVGW3BANVXG64BAOFZHG5DV'] ]; @@ -16,12 +16,11 @@ describe('impl/authenticator/encodeKey', function () { encodeKey.__Rewire__('base32', {encode}); - encodeKey('test', 'hex'); + encodeKey('test'); encodeKey.__ResetDependency__('base32'); expect(encode.calledWith('test')); - expect(toString.calledWith('hex')); }); codec.forEach((entry, idx) => {