From 62c23f4208a79e713bc21d447d41d5ce870ac3c9 Mon Sep 17 00:00:00 2001 From: lancegin Date: Mon, 10 Jul 2017 12:18:10 +0800 Subject: [PATCH] upd: finish url_gen function --- src/hotp.js | 13 +++++++++++++ src/otp.js | 16 ++++++++++++++++ src/totp.js | 13 +++++++++++++ test/hotp_test.js | 6 ++++++ test/totp_test.js | 6 ++++++ 5 files changed, 54 insertions(+) diff --git a/src/hotp.js b/src/hotp.js index 3761ee7..e36b3c2 100644 --- a/src/hotp.js +++ b/src/hotp.js @@ -56,4 +56,17 @@ export class HOTP extends OTP { return false; } } + + /** + * Generate a url with HOTP instance. + * + * @param {issuer} + * @type {String} + * @desc maybe it is the Service name + * + * @return {String} + */ + url_gen(issuer="") { + return super.url_gen(issuer, "hotp"); + } } \ No newline at end of file diff --git a/src/otp.js b/src/otp.js index 9c7d950..15a6d30 100644 --- a/src/otp.js +++ b/src/otp.js @@ -77,5 +77,21 @@ export class OTP { return str_code; } + /** + * Generate a url with TOTP or HOTP instance. + * + * @param {issuer} + * @type {String} + * @desc maybe it is the Service name + * + * @param {type} + * @type {String} + * @desc type of OTP instance + * + * @return {String} + */ + url_gen(issuer, type) { + return `otpauth://${type}/SK?secret=${this.secret}&issuer=${issuer}`; + } } diff --git a/src/totp.js b/src/totp.js index 3ff1460..3c93f0a 100644 --- a/src/totp.js +++ b/src/totp.js @@ -81,4 +81,17 @@ export class TOTP extends OTP { return false; } } + + /** + * Generate a url with TOTP instance. + * + * @param {issuer} + * @type {String} + * @desc maybe it is the Service name + * + * @return {String} + */ + url_gen(issuer="") { + return super.url_gen(issuer, "totp"); + } } \ No newline at end of file diff --git a/test/hotp_test.js b/test/hotp_test.js index 8b5a18c..db71391 100644 --- a/test/hotp_test.js +++ b/test/hotp_test.js @@ -17,5 +17,11 @@ describe('HOTP module test', function() { assert.equal(true, a.verify(a.at(0), 0)); assert.equal(false, a.verify(a.at(0), 1)); }) + }); + + describe('url_gen() function', function() { + it("should return string", function() { + assert.equal("string", typeof(a.url_gen())); + }) }) }); diff --git a/test/totp_test.js b/test/totp_test.js index 9541ef5..484369a 100644 --- a/test/totp_test.js +++ b/test/totp_test.js @@ -16,5 +16,11 @@ describe('TOTP module test', function() { it("should verify the digit", function() { assert.equal(true, a.verify(a.now())); }) + }); + + describe('url_gen() function', function() { + it("should return string", function() { + assert.equal("string", typeof(a.url_gen())); + }) }) });