Comparing version 7.1.0 to 8.0.0-1
@@ -5,3 +5,3 @@ /** | ||
* @author Gerald Yeo <contact@fusedthought.com> | ||
* @version: 7.1.0 | ||
* @version: 8.0.0-1 | ||
* @license: MIT | ||
@@ -11,20 +11,10 @@ **/ | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _interopDefault(ex) { | ||
return ex && (typeof ex === 'undefined' ? 'undefined' : _typeof(ex)) === 'object' && 'default' in ex ? ex['default'] : ex; | ||
return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex; | ||
} | ||
var base32 = _interopDefault(require('thirty-two')); | ||
var otplibCore = require('./core'); | ||
var totp = _interopDefault(require('./totp')); | ||
var otplibUtils = require('./utils'); | ||
var otplibCore = require('./core'); | ||
var base32 = _interopDefault(require('thirty-two')); | ||
@@ -35,4 +25,4 @@ function decodeKey(encodedKey) { | ||
function _check(token, secret, options) { | ||
return otplibCore.totpCheck(token, decodeKey(secret), options); | ||
function check(token, secret, options) { | ||
return otplibCore.totpCheckWithWindow(token, decodeKey(secret), options) >= 0; | ||
} | ||
@@ -44,10 +34,6 @@ | ||
var data = '{service}:{user}?secret={secret}&issuer={service}'; | ||
function _keyuri() { | ||
var user = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'user'; | ||
var service = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'service'; | ||
var secret = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | ||
var protocol = 'otpauth://totp/'; | ||
var value = data.replace('{user}', user).replace('{secret}', secret).replace(/{service}/g, service); | ||
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('{secret}', secret).replace(/{service}/g, service); | ||
return protocol + value; | ||
@@ -60,70 +46,46 @@ } | ||
var TOTP = totp.TOTP; | ||
var Authenticator = function (_TOTP) { | ||
_inherits(Authenticator, _TOTP); | ||
function Authenticator() { | ||
_classCallCheck(this, Authenticator); | ||
return _possibleConstructorReturn(this, (Authenticator.__proto__ || Object.getPrototypeOf(Authenticator)).call(this)); | ||
const TOTP = totp.TOTP; | ||
class Authenticator extends TOTP { | ||
constructor() { | ||
super(); | ||
} | ||
_createClass(Authenticator, [{ | ||
key: 'encode', | ||
value: function encode() { | ||
return encodeKey.apply(undefined, arguments); | ||
get defaultOptions() { | ||
return { | ||
encoding: 'hex', | ||
epoch: null, | ||
step: 30 | ||
}; | ||
} | ||
encode(...args) { | ||
return encodeKey(...args); | ||
} | ||
decode(...args) { | ||
return decodeKey(...args); | ||
} | ||
keyuri(...args) { | ||
return keyuri(...args); | ||
} | ||
generateSecret(len = 20) { | ||
if (!len) { | ||
return ''; | ||
} | ||
}, { | ||
key: 'decode', | ||
value: function decode() { | ||
return decodeKey.apply(undefined, arguments); | ||
} | ||
}, { | ||
key: 'keyuri', | ||
value: function keyuri() { | ||
return _keyuri.apply(undefined, arguments); | ||
} | ||
}, { | ||
key: 'generateSecret', | ||
value: function generateSecret() { | ||
var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 20; | ||
if (!len) { | ||
return ''; | ||
} | ||
var secret = otplibUtils.secretKey(len, this.options); | ||
return encodeKey(secret); | ||
} | ||
}, { | ||
key: 'generate', | ||
value: function generate(secret) { | ||
return token(secret || this.options.secret, this.options); | ||
} | ||
}, { | ||
key: 'check', | ||
value: function check(token$$1, secret) { | ||
return _check(token$$1, secret || this.options.secret, this.options); | ||
} | ||
}, { | ||
key: 'defaultOptions', | ||
get: function get() { | ||
return { | ||
encoding: 'hex', | ||
epoch: null, | ||
step: 30 | ||
}; | ||
} | ||
}]); | ||
return Authenticator; | ||
}(TOTP); | ||
const secret = otplibUtils.secretKey(len, this.optionsAll); | ||
return encodeKey(secret); | ||
} | ||
generate(secret) { | ||
const opt = this.optionsAll; | ||
return token(secret || opt.secret, opt); | ||
} | ||
check(token$$1, secret) { | ||
const opt = this.optionsAll; | ||
return check(token$$1, secret || opt.secret, opt); | ||
} | ||
} | ||
Authenticator.prototype.Authenticator = Authenticator; | ||
Authenticator.prototype.utils = { | ||
check: _check, | ||
decodeKey: decodeKey, | ||
encodeKey: encodeKey, | ||
keyuri: _keyuri, | ||
token: token | ||
check, | ||
decodeKey, | ||
encodeKey, | ||
keyuri, | ||
token | ||
}; | ||
@@ -130,0 +92,0 @@ |
155
core.js
@@ -5,3 +5,3 @@ /** | ||
* @author Gerald Yeo <contact@fusedthought.com> | ||
* @version: 7.1.0 | ||
* @version: 8.0.0-1 | ||
* @license: MIT | ||
@@ -13,4 +13,2 @@ **/ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
@@ -21,27 +19,53 @@ | ||
function hotpCounter(counter) { | ||
var hexCounter = otplibUtils.intToHex(counter); | ||
const hexCounter = otplibUtils.intToHex(counter); | ||
return otplibUtils.leftPad(hexCounter, 16); | ||
} | ||
function hotpSecret(secret, options) { | ||
return new Buffer(secret, options.encoding); | ||
} | ||
function hotpDigest(secret, counter, options) { | ||
if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) !== 'object' || options == null) { | ||
throw new Error('Expecting options to be an object'); | ||
} | ||
if (!options.crypto || typeof options.crypto.createHmac !== 'function') { | ||
throw new Error('Expecting options.crypto to have a createHmac function'); | ||
} | ||
var createHmacSecret = options.createHmacSecret || hotpSecret; | ||
var hmacSecret = createHmacSecret(secret, options); | ||
var hexCounter = hotpCounter(counter); | ||
var cryptoHmac = options.crypto.createHmac(options.algorithm, hmacSecret); | ||
if (typeof options.createHmacSecret !== 'function') { | ||
throw new Error('Expecting options.createHmacSecret to be a function'); | ||
} | ||
if (typeof options.algorithm !== 'string') { | ||
throw new Error('Expecting options.algorithm to be a string'); | ||
} | ||
const hmacSecret = options.createHmacSecret(secret, options); | ||
const hexCounter = hotpCounter(counter); | ||
const cryptoHmac = options.crypto.createHmac(options.algorithm, hmacSecret); | ||
return cryptoHmac.update(new Buffer(hexCounter, 'hex')).digest(); | ||
} | ||
function hotpOptions() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
function hotpToken(secret, counter, options) { | ||
if (counter == null) { | ||
return ''; | ||
} | ||
if (typeof options.digits !== 'number') { | ||
throw new Error('Expecting options.digits to be a number'); | ||
} | ||
const digest = hotpDigest(secret, counter, options); | ||
const offset = digest[digest.length - 1] & 0xf; | ||
const binary = (digest[offset] & 0x7f) << 24 | (digest[offset + 1] & 0xff) << 16 | (digest[offset + 2] & 0xff) << 8 | digest[offset + 3] & 0xff; | ||
let token = binary % Math.pow(10, options.digits); | ||
token = otplibUtils.leftPad(token, options.digits); | ||
return token; | ||
} | ||
function hotpCheck(token, secret, counter, options) { | ||
const systemToken = hotpToken(secret, counter, options); | ||
if (systemToken.length < 1) { | ||
return false; | ||
} | ||
return otplibUtils.isSameToken(token, systemToken); | ||
} | ||
function hotpSecret(secret, options) { | ||
if (typeof options.encoding !== 'string') { | ||
throw new Error('Expecting options.encoding to be a string'); | ||
} | ||
return new Buffer(secret, options.encoding); | ||
} | ||
function hotpOptions(options = {}) { | ||
return _extends({ | ||
@@ -56,21 +80,19 @@ algorithm: 'sha1', | ||
function hotpToken(secret, counter) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
function totpCounter(epoch, step) { | ||
return Math.floor(epoch / step / 1000); | ||
} | ||
if (counter == null) { | ||
return ''; | ||
function totpToken(secret, options) { | ||
if (typeof options.epoch !== 'number') { | ||
throw new Error('Expecting options.epoch to be a number'); | ||
} | ||
var opt = hotpOptions(options); | ||
var digest = hotpDigest(secret, counter, opt); | ||
var offset = digest[digest.length - 1] & 0xf; | ||
var binary = (digest[offset] & 0x7f) << 24 | (digest[offset + 1] & 0xff) << 16 | (digest[offset + 2] & 0xff) << 8 | digest[offset + 3] & 0xff; | ||
var token = binary % Math.pow(10, opt.digits); | ||
token = otplibUtils.leftPad(token, opt.digits); | ||
return token; | ||
if (typeof options.step !== 'number') { | ||
throw new Error('Expecting options.step to be a number'); | ||
} | ||
const counter = totpCounter(options.epoch, options.step); | ||
return hotpToken(secret, counter, options); | ||
} | ||
function hotpCheck(token, secret, counter) { | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var systemToken = hotpToken(secret, counter, options); | ||
function totpCheck(token, secret, options) { | ||
const systemToken = totpToken(secret, options || {}); | ||
if (systemToken.length < 1) { | ||
@@ -82,9 +104,27 @@ return false; | ||
function totpCounter(epoch, step) { | ||
return Math.floor(epoch / step / 1000); | ||
function totpCheckWithWindow(token, secret, options) { | ||
let opt = _extends({}, options); | ||
if (typeof opt.window !== 'number') { | ||
throw new Error('Expecting options.window to be a number'); | ||
} | ||
const decrement = opt.step * 1000; | ||
const epoch = opt.epoch; | ||
for (let i = 0; i <= opt.window; i++) { | ||
opt.epoch = epoch - i * decrement; | ||
if (totpCheck(token, secret, opt)) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
function totpSecret(secret, options) { | ||
var encoded = new Buffer(secret, options.encoding); | ||
var algorithm = options.algorithm.toLowerCase(); | ||
if (typeof options.algorithm !== 'string') { | ||
throw new Error('Expecting options.algorithm to be a string'); | ||
} | ||
if (typeof options.encoding !== 'string') { | ||
throw new Error('Expecting options.encoding to be a string'); | ||
} | ||
const encoded = new Buffer(secret, options.encoding); | ||
const algorithm = options.algorithm.toLowerCase(); | ||
switch (algorithm) { | ||
@@ -98,7 +138,7 @@ case 'sha1': | ||
default: | ||
throw new Error('Unsupported algorithm ' + algorithm + '. Accepts: sha1, sha256, sha512'); | ||
throw new Error(`Unsupported algorithm ${algorithm}. Accepts: sha1, sha256, sha512`); | ||
} | ||
} | ||
var defaultOptions = { | ||
const defaultOptions = { | ||
createHmacSecret: totpSecret, | ||
@@ -109,6 +149,4 @@ epoch: null, | ||
}; | ||
function totpOptions() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var opt = _extends(hotpOptions(), defaultOptions, options); | ||
function totpOptions(options = {}) { | ||
let opt = _extends(hotpOptions(), defaultOptions, options); | ||
opt.window = Math.floor(opt.window || 0); | ||
@@ -119,35 +157,2 @@ opt.epoch = typeof opt.epoch === 'number' ? opt.epoch * 1000 : Date.now(); | ||
function totpToken(secret) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var opt = totpOptions(options); | ||
var counter = totpCounter(opt.epoch, opt.step); | ||
return hotpToken(secret, counter, opt); | ||
} | ||
function totpCheck(token, secret, options) { | ||
var systemToken = totpToken(secret, options || {}); | ||
if (systemToken.length < 1) { | ||
return false; | ||
} | ||
return otplibUtils.isSameToken(token, systemToken); | ||
} | ||
function getPrevWindowOption(options, windowCount) { | ||
return _extends(options, { | ||
epoch: options.epoch - options.step * windowCount | ||
}); | ||
} | ||
function totpCheckWithWindow(token, secret, options) { | ||
var opt = _extends({}, options); | ||
var rounds = Math.floor(opt.window || 0) + 1; | ||
for (var i = 0; i < rounds; i++) { | ||
opt = getPrevWindowOption(opt, i); | ||
if (totpCheck(token, secret, opt)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
exports.hotpCheck = hotpCheck; | ||
@@ -154,0 +159,0 @@ exports.hotpCounter = hotpCounter; |
90
hotp.js
@@ -5,3 +5,3 @@ /** | ||
* @author Gerald Yeo <contact@fusedthought.com> | ||
* @version: 7.1.0 | ||
* @version: 8.0.0-1 | ||
* @license: MIT | ||
@@ -13,63 +13,41 @@ **/ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var otplibCore = require('./core'); | ||
var HOTP = function () { | ||
function HOTP() { | ||
_classCallCheck(this, HOTP); | ||
class HOTP { | ||
constructor() { | ||
this._options = this.defaultOptions; | ||
} | ||
_createClass(HOTP, [{ | ||
key: 'resetOptions', | ||
value: function resetOptions() { | ||
this._options = this.defaultOptions; | ||
return this; | ||
get defaultOptions() { | ||
return {}; | ||
} | ||
set options(opt = {}) { | ||
if (opt) { | ||
this._options = _extends({}, this._options, opt); | ||
} | ||
}, { | ||
key: 'generate', | ||
value: function generate(secret, counter) { | ||
return otplibCore.hotpToken(secret || this.options.secret, counter, this.options); | ||
} | ||
get options() { | ||
return _extends({}, this._options); | ||
} | ||
get optionsAll() { | ||
return otplibCore.hotpOptions(this._options); | ||
} | ||
resetOptions() { | ||
this._options = this.defaultOptions; | ||
return this; | ||
} | ||
generate(secret, counter) { | ||
const opt = this.optionsAll; | ||
return otplibCore.hotpToken(secret || opt.secret, counter, opt); | ||
} | ||
check(token, secret, counter) { | ||
const opt = this.optionsAll; | ||
return otplibCore.hotpCheck(token, secret || opt.secret, counter, opt); | ||
} | ||
verify(opts) { | ||
if (typeof opts !== 'object' || opts == null) { | ||
return false; | ||
} | ||
}, { | ||
key: 'check', | ||
value: function check(token, secret, counter) { | ||
return otplibCore.hotpCheck(token, secret || this.options.secret, counter, this.options); | ||
} | ||
}, { | ||
key: 'verify', | ||
value: function verify(opts) { | ||
if ((typeof opts === 'undefined' ? 'undefined' : _typeof(opts)) !== 'object' || opts == null) { | ||
return false; | ||
} | ||
return this.check(opts.token, opts.secret, opts.counter); | ||
} | ||
}, { | ||
key: 'defaultOptions', | ||
get: function get() { | ||
return {}; | ||
} | ||
}, { | ||
key: 'options', | ||
set: function set() { | ||
var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
if (opt) { | ||
this._options = _extends({}, this._options, opt); | ||
} | ||
}, | ||
get: function get() { | ||
return _extends({}, this._options); | ||
} | ||
}]); | ||
return HOTP; | ||
}(); | ||
return this.check(opts.token, opts.secret, opts.counter); | ||
} | ||
} | ||
HOTP.prototype.HOTP = HOTP; | ||
@@ -76,0 +54,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
* @author Gerald Yeo <contact@fusedthought.com> | ||
* @version: 7.1.0 | ||
* @version: 8.0.0-1 | ||
* @license: MIT | ||
@@ -11,6 +11,4 @@ **/ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _interopDefault(ex) { | ||
return ex && (typeof ex === 'undefined' ? 'undefined' : _typeof(ex)) === 'object' && 'default' in ex ? ex['default'] : ex; | ||
return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex; | ||
} | ||
@@ -23,5 +21,5 @@ | ||
authenticator.options = { crypto: crypto }; | ||
hotp.options = { crypto: crypto }; | ||
totp.options = { crypto: crypto }; | ||
authenticator.options = { crypto }; | ||
hotp.options = { crypto }; | ||
totp.options = { crypto }; | ||
var index = { | ||
@@ -31,7 +29,7 @@ Authenticator: authenticator.Authenticator, | ||
TOTP: totp.TOTP, | ||
authenticator: authenticator, | ||
hotp: hotp, | ||
totp: totp | ||
authenticator, | ||
hotp, | ||
totp | ||
}; | ||
module.exports = index; |
{ | ||
"name": "otplib", | ||
"version": "7.1.0", | ||
"version": "8.0.0-1", | ||
"description": "HMAC-based (HOTP) and Time-based (TOTP) One-Time Password library", | ||
@@ -17,3 +17,4 @@ "main": "otplib.js", | ||
"deploy-package": "./scripts/build-publish.sh", | ||
"lint": "eslint --ext js src", | ||
"lint": "eslint \"packages/**/**\"", | ||
"lint:format": "prettier --write \"{packages,scripts,site/scripts,site/public}/**/**.js\"", | ||
"test": "jest --coverage", | ||
@@ -47,20 +48,22 @@ "test:watch": "jest --coverage --watch" | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.24.1", | ||
"babel-eslint": "^7.2.1", | ||
"babel-loader": "^6.4.1", | ||
"babel-plugin-module-resolver": "^2.7.1", | ||
"babel-loader": "^7.1.3", | ||
"babel-plugin-module-resolver": "^3.1.0", | ||
"babel-plugin-transform-class-properties": "^6.23.0", | ||
"babel-plugin-transform-object-assign": "^6.22.0", | ||
"babel-preset-es2015": "^6.24.0", | ||
"coveralls": "^2.13.0", | ||
"babel-preset-env": "^1.6.1", | ||
"coveralls": "^3.0.0", | ||
"create-hmac": "^1.1.4", | ||
"eslint": "^3.19.0", | ||
"jest": "^20.0.4", | ||
"eslint": "^4.18.2", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-plugin-prettier": "^2.6.0", | ||
"jest": "^22.4.2", | ||
"jsdoc": "^3.4.3", | ||
"minami": "^1.1.1", | ||
"prettier": "1.11.1", | ||
"rimraf": "^2.6.1", | ||
"rollup": "^0.49.2", | ||
"rollup-plugin-cleanup": "^1.0.1", | ||
"rollup": "^0.56.3", | ||
"rollup-plugin-cleanup": "^2.0.0", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"webpack": "^2.3.3" | ||
"webpack": "^4.0.1", | ||
"webpack-cli": "^2.0.10" | ||
}, | ||
@@ -67,0 +70,0 @@ "dependencies": { |
@@ -10,2 +10,3 @@ # otplib | ||
[![PRs Welcome][pr-welcome-badge]][pr-welcome-link] | ||
[![Support Project][donate-badge]][donate-link] | ||
@@ -46,4 +47,4 @@ <img width="150" src="https://yeojz.github.io/otplib/otplib.png" /> | ||
- [RFC 4226 Dataset](https://github.com/yeojz/otplib/blob/master/packages/tests/rfc4226.js) | ||
- [RFC 6238 Dataset](https://github.com/yeojz/otplib/blob/master/packages/tests/rfc6238.js) | ||
* [RFC 4226 Dataset](https://github.com/yeojz/otplib/blob/master/packages/tests/rfc4226.js) | ||
* [RFC 6238 Dataset](https://github.com/yeojz/otplib/blob/master/packages/tests/rfc6238.js) | ||
@@ -54,4 +55,4 @@ This library is also compatible with [Google Authenticator](https://github.com/google/google-authenticator), and includes additional methods to allow you to work with Google Authenticator. | ||
- [Documentation][project-docs] | ||
- [Demo][project-web] | ||
* [Documentation][project-docs] | ||
* [Demo][project-web] | ||
@@ -88,3 +89,2 @@ ## Installation | ||
}); | ||
``` | ||
@@ -102,3 +102,3 @@ | ||
__Note__: If you import the libraries directly, you'll have to provide a crypto | ||
**Note**: If you import the libraries directly, you'll have to provide a crypto | ||
solution (this is to allow custom crypto solutions), as long as they implement `createHmac` and `randomBytes`. | ||
@@ -113,3 +113,3 @@ Take a look at the [browser implementation](https://github.com/yeojz/otplib/blob/master/packages/otplib-browser) | ||
import crypto from 'crypto'; | ||
authenticator.options = { crypto } | ||
authenticator.options = { crypto }; | ||
@@ -129,3 +129,2 @@ // Or if you're using the other options | ||
```js | ||
import hotp from 'otplib/hotp'; | ||
@@ -144,3 +143,4 @@ const HOTP = hotp.HOTP; | ||
#### Detailed example | ||
__Example Usage__ | ||
```js | ||
@@ -150,13 +150,9 @@ import { Authenticator } from 'otplib/authenticator'; | ||
const secret = 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD'; | ||
const algorithm = 'sha512'; | ||
const step = 20; | ||
const digits = 8; | ||
const authenticator = new Authenticator(); | ||
const authenticator = new Authenticator(); | ||
authenticator.options = { | ||
secret, | ||
algorithm, | ||
step, | ||
digits, | ||
secret: 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD', | ||
algorithm: 'sha512', | ||
step: 20, | ||
digits: 8, | ||
crypto | ||
@@ -185,4 +181,4 @@ }; | ||
- Download from [gh-pages][project-lib]. | ||
- Use unpkg.com | ||
* Download from [gh-pages][project-lib]. | ||
* Use unpkg.com | ||
@@ -204,6 +200,6 @@ ```html | ||
__Output sizes:__ | ||
**Output sizes:** | ||
- with node crypto: ~311Kb | ||
- with alternative crypto: ~96Kb | ||
* with node crypto: ~311Kb | ||
* with alternative crypto: ~96Kb | ||
@@ -250,3 +246,3 @@ ## Advanced Usage | ||
step: 30 | ||
} | ||
}; | ||
@@ -269,6 +265,6 @@ // getting | ||
| encoding | string | 'ascii' ('hex' for Authenticator) | The encoding of secret which is given to digest | | ||
| epoch (totp) | integer | null | starting time since the UNIX epoch (seconds). *Note* non-javascript epoch. i.e. `Date.now() / 1000` | | ||
| epoch (totp) | integer | null | starting time since the UNIX epoch (seconds). _Note_ non-javascript epoch. i.e. `Date.now() / 1000` | | ||
| step (totp) | integer | 30 | Time step (seconds) | | ||
| window (totp) | integer | 0 | Tokens in the previous x-windows that should be considered valid | | ||
### Seed / secret length | ||
@@ -334,3 +330,3 @@ | ||
- Check out: [CONTRIBUTING.md][pr-welcome-link] | ||
Check out: [CONTRIBUTING.md][pr-welcome-link] | ||
@@ -344,24 +340,19 @@ ## License | ||
[npm-downloads-badge]: https://img.shields.io/npm/dt/otplib.svg?style=flat-square | ||
[circle-badge]: https://img.shields.io/circleci/project/github/yeojz/otplib/master.svg?style=flat-square | ||
[circle-link]: https://circleci.com/gh/yeojz/otplib | ||
[coveralls-badge]: https://img.shields.io/coveralls/yeojz/otplib/master.svg?style=flat-square | ||
[coveralls-link]: https://coveralls.io/github/yeojz/otplib | ||
[pr-welcome-badge]: https://img.shields.io/badge/PRs-Welcome-ff69b4.svg?style=flat-square | ||
[pr-welcome-link]: https://github.com/yeojz/otplib/blob/master/CONTRIBUTING.md | ||
[mdn-uint8array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array | ||
[mdn-crypto]: https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto | ||
[project-web]: https://yeojz.github.io/otplib | ||
[project-docs]: https://yeojz.github.io/otplib/docs | ||
[project-lib]: https://github.com/yeojz/otplib/tree/gh-pages/lib | ||
[rfc-4226]: http://tools.ietf.org/html/rfc4226 | ||
[rfc-6238]: http://tools.ietf.org/html/rfc6238 | ||
[rfc-3548]: http://tools.ietf.org/html/rfc3548 | ||
[rfc-4226-wiki]: http://en.wikipedia.org/wiki/HMAC-based_One-time_Password_Algorithm | ||
[rfc-6238-wiki]: http://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm | ||
[donate-badge]: https://img.shields.io/badge/donate-%3C3-red.svg?longCache=true&style=flat-square | ||
[donate-link]: https://www.paypal.me/yeojz |
82
totp.js
@@ -5,3 +5,3 @@ /** | ||
* @author Gerald Yeo <contact@fusedthought.com> | ||
* @version: 7.1.0 | ||
* @version: 8.0.0-1 | ||
* @license: MIT | ||
@@ -11,14 +11,4 @@ **/ | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _interopDefault(ex) { | ||
return ex && (typeof ex === 'undefined' ? 'undefined' : _typeof(ex)) === 'object' && 'default' in ex ? ex['default'] : ex; | ||
return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex; | ||
} | ||
@@ -29,44 +19,32 @@ | ||
var HOTP = hotp.HOTP; | ||
var TOTP = function (_HOTP) { | ||
_inherits(TOTP, _HOTP); | ||
function TOTP() { | ||
_classCallCheck(this, TOTP); | ||
return _possibleConstructorReturn(this, (TOTP.__proto__ || Object.getPrototypeOf(TOTP)).call(this)); | ||
const HOTP = hotp.HOTP; | ||
class TOTP extends HOTP { | ||
constructor() { | ||
super(); | ||
} | ||
_createClass(TOTP, [{ | ||
key: 'generate', | ||
value: function generate(secret) { | ||
return otplibCore.totpToken(secret || this.options.secret, this.options); | ||
get defaultOptions() { | ||
return { | ||
epoch: null, | ||
step: 30, | ||
window: 0 | ||
}; | ||
} | ||
get optionsAll() { | ||
return otplibCore.totpOptions(this._options); | ||
} | ||
generate(secret) { | ||
const opt = this.optionsAll; | ||
return otplibCore.totpToken(secret || opt.secret, opt); | ||
} | ||
check(token, secret) { | ||
const opt = this.optionsAll; | ||
return otplibCore.totpCheckWithWindow(token, secret || opt.secret, opt) >= 0; | ||
} | ||
verify(opts) { | ||
if (typeof opts !== 'object' || opts == null) { | ||
return false; | ||
} | ||
}, { | ||
key: 'check', | ||
value: function check(token, secret) { | ||
return otplibCore.totpCheck(token, secret || this.options.secret, this.options); | ||
} | ||
}, { | ||
key: 'verify', | ||
value: function verify(opts) { | ||
if ((typeof opts === 'undefined' ? 'undefined' : _typeof(opts)) !== 'object' || opts == null) { | ||
return false; | ||
} | ||
return this.check(opts.token, opts.secret); | ||
} | ||
}, { | ||
key: 'defaultOptions', | ||
get: function get() { | ||
return { | ||
epoch: null, | ||
step: 30 | ||
}; | ||
} | ||
}]); | ||
return TOTP; | ||
}(HOTP); | ||
return this.check(opts.token, opts.secret); | ||
} | ||
} | ||
TOTP.prototype.TOTP = TOTP; | ||
@@ -73,0 +51,0 @@ |
37
utils.js
@@ -5,3 +5,3 @@ /** | ||
* @author Gerald Yeo <contact@fusedthought.com> | ||
* @version: 7.1.0 | ||
* @version: 8.0.0-1 | ||
* @license: MIT | ||
@@ -26,4 +26,4 @@ **/ | ||
function leftPad(value, length) { | ||
var total = !length ? 0 : length; | ||
var padded = value + ''; | ||
const total = !length ? 0 : length; | ||
let padded = value + ''; | ||
while (padded.length < total) { | ||
@@ -36,6 +36,6 @@ padded = '0' + padded; | ||
function padSecret(secretBuffer, size, encoding) { | ||
var secret = secretBuffer.toString(encoding); | ||
var len = secret.length; | ||
const secret = secretBuffer.toString(encoding); | ||
const len = secret.length; | ||
if (size && len < size) { | ||
var newSecret = new Array(size - len + 1).join(secretBuffer.toString('hex')); | ||
const newSecret = new Array(size - len + 1).join(secretBuffer.toString('hex')); | ||
return new Buffer(newSecret, 'hex').slice(0, size); | ||
@@ -46,5 +46,3 @@ } | ||
function removeSpaces() { | ||
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
function removeSpaces(value = '') { | ||
if (value == null) { | ||
@@ -56,5 +54,3 @@ return ''; | ||
function secretKey(length) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
function secretKey(length, options = {}) { | ||
if (!length || length < 1) { | ||
@@ -69,11 +65,8 @@ return ''; | ||
function setsOf(value) { | ||
var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4; | ||
var divider = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ' '; | ||
var num = parseInt(amount, 10); | ||
function setsOf(value, amount = 4, divider = ' ') { | ||
const num = parseInt(amount, 10); | ||
if (Number.isNaN(num) || typeof value !== 'string') { | ||
return ''; | ||
} | ||
var regex = new RegExp('.{1,' + amount + '}', 'g'); | ||
const regex = new RegExp('.{1,' + amount + '}', 'g'); | ||
return value.match(regex).join(divider); | ||
@@ -83,6 +76,6 @@ } | ||
function stringToHex(value) { | ||
var val = value == null ? '' : value; | ||
var hex = ''; | ||
var tmp = ''; | ||
for (var i = 0; i < val.length; i++) { | ||
const val = value == null ? '' : value; | ||
let hex = ''; | ||
let tmp = ''; | ||
for (let i = 0; i < val.length; i++) { | ||
tmp = ('0000' + val.charCodeAt(i).toString(16)).slice(-2); | ||
@@ -89,0 +82,0 @@ hex += '' + tmp; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131392
21
11
807
1
344