add generateTotpUri

This commit is contained in:
AJ ONeal 2015-11-03 00:31:06 -08:00
parent 3b0abbc015
commit 1454ff4641
2 changed files with 29 additions and 0 deletions

View File

@ -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**
* <https://github.com/google/google-authenticator/wiki/Key-Uri-Format>
* `otpauth://totp/<<ISSUER>>:<<ACCOUNT_NAME>>?secret=<<BASE32_KEY>>&issuer=<<ISSUER>>`
* `otpauth://totp/<<ISSUER>>:<<ACCOUNT_NAME>>?secret=<<BASE32_KEY>>&issuer=<<ISSUER>>&algorithm=<<ALGO>>&digits=<<INT>>&period=<<SECONDS>>`
Note that `ISSUER` is specified twice for backwards / forwards compatibility.
QR Code
-------

View File

@ -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