From 1454ff46416131f23f4645d0903a980a296d081e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 3 Nov 2015 00:31:06 -0800 Subject: [PATCH] add generateTotpUri --- README.md | 17 +++++++++++++++++ authenticator.js | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 1616dfd..cb780ac 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,11 @@ var authenticator = window.Authenticator; authenticator.generateKey().then(function (formattedKey) { // "acqo ua72 d3yf a4e5 uorx ztkh j2xl 3wiz" + authenticator.generateTotpUri(formattedKey, "john.doe@email.com", "ACME Co", 'SHA1', 6, 30); + // + // otpauth://totp/ACME%20Co:john.doe@email.com?secret=ACQOUA72D3YFA4E5UORXZTKHJ2XL3WIZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 + + authenticator.generateToken(formattedKey).then(function (formattedToken) { // "957 124" @@ -108,6 +113,18 @@ validates a time-based token within a +/- 30 second (90 seconds) window returns `null` on failure or an object such as `{ delta: 0 }` on success +### generateTotpUri(formattedKey, accountName, issuer, algorithm, digits, period) + +generates an `OTPAUTH://` scheme URI for QR Code generation. + +**OTPAuth Scheme** + +* +* `otpauth://totp/<>:<>?secret=<>&issuer=<>` +* `otpauth://totp/<>:<>?secret=<>&issuer=<>&algorithm=<>&digits=<>&period=<>` + +Note that `ISSUER` is specified twice for backwards / forwards compatibility. + QR Code ------- diff --git a/authenticator.js b/authenticator.js index b9f2c25..1512174 100644 --- a/authenticator.js +++ b/authenticator.js @@ -81,6 +81,18 @@ function verifyGoogleAuthToken(key, token) { Authenticator.generateKey = generateGoogleAuthKey; Authenticator.generateToken = generateGoogleAuthToken; Authenticator.verifyToken = verifyGoogleAuthToken; +Authenticator.generateTotpUri = function (secret, accountName, issuer, algo, digits, period) { + // Full OTPAUTH URI spec as explained at + // https://github.com/google/google-authenticator/wiki/Key-Uri-Format + return 'otpauth://totp/' + + encodeURI(issuer || '') + ':' + encodeURI(accountName || '') + + '?secret=' + secret.replace(/[\s\.\_\-]+/g, '').toUpperCase() + + '&issuer=' + encodeURIComponent(issuer || '') + + '&algorithm=' + (algo || 'SHA1') + + '&digits=' + (digits || 6) + + '&period=' + (period || 30) + ; +}; }( 'undefined' !== typeof window ? (window.Authenticator = {}) : module.exports