Showing
3 changed files
with
17 additions
and
11 deletions
| ... | @@ -8,7 +8,6 @@ const Authenticator = function (secret, options) { | ... | @@ -8,7 +8,6 @@ const Authenticator = function (secret, options) { |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | const _ = require('lodash'), | 10 | const _ = require('lodash'), |
| 11 | - path = require('path'), | ||
| 12 | crypto_utils = require('kml-crypto-utils'), | 11 | crypto_utils = require('kml-crypto-utils'), |
| 13 | speakeasy = require('speakeasy'), | 12 | speakeasy = require('speakeasy'), |
| 14 | qr = require('qr-image'); | 13 | qr = require('qr-image'); |
| ... | @@ -30,15 +29,16 @@ const Authenticator = function (secret, options) { | ... | @@ -30,15 +29,16 @@ const Authenticator = function (secret, options) { |
| 30 | * @param token | 29 | * @param token |
| 31 | */ | 30 | */ |
| 32 | this.verify = function(token) { | 31 | this.verify = function(token) { |
| 33 | - return speakeasy.totp.verify(_.merge({token: token}, TOTP_OPTIONS)); | 32 | + return speakeasy.totp.verify(_.merge({}, TOTP_OPTIONS, {token: token})); |
| 34 | }; | 33 | }; |
| 35 | 34 | ||
| 36 | /** | 35 | /** |
| 37 | * 在options.window的范围内验证token的有效性 | 36 | * 在options.window的范围内验证token的有效性 |
| 38 | * @param token | 37 | * @param token |
| 38 | + * @param window | ||
| 39 | */ | 39 | */ |
| 40 | - this.verifyDelta = function(token) { | 40 | + this.verifyDelta = function(token, window) { |
| 41 | - return speakeasy.totp.verifyDelta(_.merge({token: token}, TOTP_OPTIONS)); | 41 | + return speakeasy.totp.verifyDelta(_.merge({}, TOTP_OPTIONS, {token: token, window: window})); |
| 42 | }; | 42 | }; |
| 43 | 43 | ||
| 44 | /** | 44 | /** |
| ... | @@ -48,7 +48,7 @@ const Authenticator = function (secret, options) { | ... | @@ -48,7 +48,7 @@ const Authenticator = function (secret, options) { |
| 48 | * @return {string} | 48 | * @return {string} |
| 49 | */ | 49 | */ |
| 50 | this.getOtpAuth = function (title, issuer) { | 50 | this.getOtpAuth = function (title, issuer) { |
| 51 | - return speakeasy.otpauthURL(_.merge({ label: title, issuer: issuer }, TOTP_OPTIONS)); | 51 | + return speakeasy.otpauthURL(_.merge({}, TOTP_OPTIONS, {label: title, issuer: issuer})); |
| 52 | }; | 52 | }; |
| 53 | 53 | ||
| 54 | /** | 54 | /** | ... | ... |
| ... | @@ -2,16 +2,18 @@ | ... | @@ -2,16 +2,18 @@ |
| 2 | * Created by lintry on 2017/5/19. | 2 | * Created by lintry on 2017/5/19. |
| 3 | */ | 3 | */ |
| 4 | 4 | ||
| 5 | -const Authenticator = require('../lib/authenticator'); | 5 | +const TOTP = require('../lib/totp'); |
| 6 | const chalk = require('chalk'); | 6 | const chalk = require('chalk'); |
| 7 | const fs = require('fs-extra'); | 7 | const fs = require('fs-extra'); |
| 8 | const path = require('path'); | 8 | const path = require('path'); |
| 9 | 9 | ||
| 10 | +//使用外部生成的密钥 | ||
| 10 | let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'; | 11 | let secret = 'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'; |
| 11 | -let authenticator = new Authenticator(secret, {algorithm: 'sha512'}); | 12 | +let authenticator = new TOTP({algorithm: 'sha512'}).parse(secret); |
| 12 | console.log(chalk.cyan('totp的secret')); | 13 | console.log(chalk.cyan('totp的secret')); |
| 13 | console.log(secret, authenticator.totp_options); | 14 | console.log(secret, authenticator.totp_options); |
| 14 | 15 | ||
| 16 | +//外部传入的token参数 | ||
| 15 | let token = process.argv[2]; | 17 | let token = process.argv[2]; |
| 16 | if (!token) { | 18 | if (!token) { |
| 17 | token = authenticator.totp(); | 19 | token = authenticator.totp(); |
| ... | @@ -20,11 +22,13 @@ if (!token) { | ... | @@ -20,11 +22,13 @@ if (!token) { |
| 20 | } | 22 | } |
| 21 | console.log(chalk.green('token is'), token); | 23 | console.log(chalk.green('token is'), token); |
| 22 | 24 | ||
| 25 | +//精确验证token | ||
| 23 | let verify = authenticator.verify(token); | 26 | let verify = authenticator.verify(token); |
| 24 | console.log(chalk.magenta('verify is '), (verify ? chalk.green : chalk.red)(verify)); | 27 | console.log(chalk.magenta('verify is '), (verify ? chalk.green : chalk.red)(verify)); |
| 25 | -console.log(chalk.blue('verifyDelta is '), authenticator.verifyDelta(token)); | 28 | +//允许误差验证token |
| 26 | - | 29 | +console.log(chalk.blue('verifyDelta is '), authenticator.verifyDelta(token, 2)); |
| 27 | 30 | ||
| 31 | +//生成密钥二维码图片 | ||
| 28 | let img_path = path.resolve(process.cwd(), 'img'); | 32 | let img_path = path.resolve(process.cwd(), 'img'); |
| 29 | let qr = path.resolve(img_path, 'qr.svg'); | 33 | let qr = path.resolve(img_path, 'qr.svg'); |
| 30 | 34 | ||
| ... | @@ -38,7 +42,7 @@ fs.ensureDir(img_path, function (err, added_root) { | ... | @@ -38,7 +42,7 @@ fs.ensureDir(img_path, function (err, added_root) { |
| 38 | fs.writeSync(fd, authenticator.getQR('totp@gitlab.kmlab.com', '通行密钥')); | 42 | fs.writeSync(fd, authenticator.getQR('totp@gitlab.kmlab.com', '通行密钥')); |
| 39 | fs.closeSync(fd); | 43 | fs.closeSync(fd); |
| 40 | 44 | ||
| 41 | - console.log(chalk.yellow(authenticator.getOtpAuth('totp@gitlab.kmlab.com', '通行密钥'))) | 45 | + console.log(chalk.green('密钥字符串'), chalk.yellow(authenticator.getOtpAuth('totp@gitlab.kmlab.com', '通行密钥'))) |
| 42 | }); | 46 | }); |
| 43 | 47 | ||
| 44 | console.log('QR SVG output is', img_path, qr); | 48 | console.log('QR SVG output is', img_path, qr); |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -6,6 +6,7 @@ const TOTP = require('../lib/totp'); | ... | @@ -6,6 +6,7 @@ const TOTP = require('../lib/totp'); |
| 6 | const chalk = require('chalk'); | 6 | const chalk = require('chalk'); |
| 7 | const fs = require('fs-extra'); | 7 | const fs = require('fs-extra'); |
| 8 | 8 | ||
| 9 | +//根据第二个参数算法创建密钥,默认sha512 | ||
| 9 | let algorithm = process.argv[2] || 'sha512'; | 10 | let algorithm = process.argv[2] || 'sha512'; |
| 10 | let totp = new TOTP({algorithm: algorithm}); | 11 | let totp = new TOTP({algorithm: algorithm}); |
| 11 | 12 | ||
| ... | @@ -19,7 +20,8 @@ let authenticator = totp.parse(secret); | ... | @@ -19,7 +20,8 @@ let authenticator = totp.parse(secret); |
| 19 | 20 | ||
| 20 | console.log(authenticator.totp_options); | 21 | console.log(authenticator.totp_options); |
| 21 | 22 | ||
| 23 | +//根据实际生成token | ||
| 22 | let token = authenticator.totp(); | 24 | let token = authenticator.totp(); |
| 23 | console.log(chalk.green('token is'), token); | 25 | console.log(chalk.green('token is'), token); |
| 24 | - | 26 | +//验证token |
| 25 | console.log(chalk.magenta('verify is '), authenticator.verify(token)); | 27 | console.log(chalk.magenta('verify is '), authenticator.verify(token)); |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment