Skip to content

Commit

Permalink
Merge pull request #11 from yeojz/hotfix/google-authenticator-codec
Browse files Browse the repository at this point in the history
google authenticator
  • Loading branch information
yeojz authored Aug 19, 2017
2 parents 2700bec + e128286 commit 2a7b0f4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/impl/authenticator/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 3 additions & 6 deletions src/impl/authenticator/decodeKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @return {Object} A Buffer object containing the 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;
8 changes: 3 additions & 5 deletions src/impl/authenticator/encodeKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion src/v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions tests/specs/impl/authenticator/decodeKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
];

Expand All @@ -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]);
});
});
});
9 changes: 4 additions & 5 deletions tests/specs/impl/authenticator/encodeKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
];

Expand All @@ -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) => {
Expand Down

0 comments on commit 2a7b0f4

Please sign in to comment.