UNPKG

4.82 kBJavaScriptView Raw
1/**
2 * otplib-authenticator
3 *
4 * @author Gerald Yeo <contact@fusedthought.com>
5 * @version: 7.0.0
6 * @license: MIT
7 **/
8'use strict';
9
10var _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; }; }();
11
12var _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; };
13
14function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
16function _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; }
17
18function _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; }
19
20function _interopDefault(ex) {
21 return ex && (typeof ex === 'undefined' ? 'undefined' : _typeof(ex)) === 'object' && 'default' in ex ? ex['default'] : ex;
22}
23
24var totp = _interopDefault(require('./totp'));
25var otplibUtils = require('./utils');
26var otplibCore = require('./core');
27var base32 = _interopDefault(require('thirty-two'));
28
29function decodeKey(encodedKey) {
30 return base32.decode(encodedKey).toString('hex');
31}
32
33function _check(token, secret, options) {
34 return otplibCore.totpCheck(token, decodeKey(secret), options);
35}
36
37function encodeKey(secret) {
38 return base32.encode(secret).toString().replace(/=/g, '');
39}
40
41var data = '{service}:{user}?secret={secret}&issuer={service}';
42function _keyuri() {
43 var user = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'user';
44 var service = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'service';
45 var secret = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
46
47 var protocol = 'otpauth://totp/';
48 var value = data.replace('{user}', user).replace('{secret}', secret).replace(/{service}/g, service);
49 return protocol + value;
50}
51
52function token(secret, options) {
53 return otplibCore.totpToken(decodeKey(secret), options);
54}
55
56var TOTP = totp.TOTP;
57
58var Authenticator = function (_TOTP) {
59 _inherits(Authenticator, _TOTP);
60
61 function Authenticator() {
62 _classCallCheck(this, Authenticator);
63
64 return _possibleConstructorReturn(this, (Authenticator.__proto__ || Object.getPrototypeOf(Authenticator)).call(this));
65 }
66
67 _createClass(Authenticator, [{
68 key: 'encode',
69 value: function encode() {
70 return encodeKey.apply(undefined, arguments);
71 }
72 }, {
73 key: 'decode',
74 value: function decode() {
75 return decodeKey.apply(undefined, arguments);
76 }
77 }, {
78 key: 'keyuri',
79 value: function keyuri() {
80 return _keyuri.apply(undefined, arguments);
81 }
82 }, {
83 key: 'generateSecret',
84 value: function generateSecret() {
85 var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 20;
86
87 if (!len) {
88 return '';
89 }
90 var secret = otplibUtils.secretKey(len, this.options);
91 return encodeKey(secret);
92 }
93 }, {
94 key: 'generate',
95 value: function generate(secret) {
96 return token(secret || this.options.secret, this.options);
97 }
98 }, {
99 key: 'check',
100 value: function check(token$$1, secret) {
101 return _check(token$$1, secret || this.options.secret, this.options);
102 }
103 }, {
104 key: 'defaultOptions',
105 get: function get() {
106 return {
107 encoding: 'hex',
108 epoch: null,
109 step: 30
110 };
111 }
112 }]);
113
114 return Authenticator;
115}(TOTP);
116
117Authenticator.prototype.Authenticator = Authenticator;
118Authenticator.prototype.utils = {
119 check: _check,
120 decodeKey: decodeKey,
121 encodeKey: encodeKey,
122 keyuri: _keyuri,
123 token: token
124};
125
126var index = new Authenticator();
127
128module.exports = index;
\No newline at end of file