Skip to content

Commit

Permalink
feat: encodeURIComponent added to keyuri
Browse files Browse the repository at this point in the history
addresses: #126
  • Loading branch information
yeojz committed Mar 9, 2019
1 parent 1e71658 commit 51bea38
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,14 @@ import otplib from 'otplib';

const user = 'A user name, possibly an email';
const service = 'A service name';

// v10.x.x and below
const otpauth = otplib.authenticator.keyuri(
encodeURIComponent(user), encodeURIComponent(service), secret);

// v11.x.x and above
const otpauth = otplib.authenticator.keyuri(user, service, secret);

qrcode.toDataURL(otpauth, (err, imageUrl) => {
if (err) {
console.log('Error with QR');
Expand Down
7 changes: 5 additions & 2 deletions packages/otplib-authenticator/keyuri.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const data = '{service}:{user}?secret={secret}&issuer={service}';
/**
* Generates an otpauth uri
*
* The "user" and "service" parameters will
* be passed to encodeURIComponent for encoding
*
* @namespace otplib/impl/authenticator
* @module otplib-authenticator/keyuri
* @param {string} user - the name/id of your user
Expand All @@ -13,9 +16,9 @@ const data = '{service}:{user}?secret={secret}&issuer={service}';
function keyuri(user = 'user', service = 'service', secret = '') {
const protocol = 'otpauth://totp/';
const value = data
.replace('{user}', user)
.replace('{user}', encodeURIComponent(user))
.replace('{secret}', secret)
.replace(/{service}/g, service);
.replace(/{service}/g, encodeURIComponent(service));

return protocol + value;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/otplib-authenticator/keyuri.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ describe('keyuri', () => {
void 0,
void 0,
void 0
],
[
'otpauth://totp/test%20got%20space:me%20got%20space?secret=123&issuer=test%20got%20space',
'me got space',
'test got space',
'123'
]
].forEach((entry, idx) => {
const [url, ...args] = entry;
Expand Down

0 comments on commit 51bea38

Please sign in to comment.