UNPKG

wampy-cra

Version:

Wampy.js challenge response authentication (CRA) plugin

74 lines (65 loc) 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deriveKey = deriveKey; exports.setCrypto = setCrypto; exports.sign = sign; exports.signManual = signManual; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } /** * Project: wampy-cra.js * * https://github.com/KSDaemon/wampy-cra * * Wampy.js Challenge Response Authentication plugin * * Copyright 2016 KSDaemon. Licensed under the MIT License. * See @license text at http://www.opensource.org/licenses/mit-license.php * */ var isNode = (typeof process === "undefined" ? "undefined" : _typeof(process)) === 'object' && Object.prototype.toString.call(process) === '[object process]'; var crypto; function setCrypto(lib) { crypto = lib; } function deriveKey(secret, salt) { var iterations = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000; var keylen = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 32; var key; if (isNode) { key = crypto.pbkdf2Sync(secret, salt, iterations, keylen, 'sha256'); return key.toString('base64'); } else { var config = { keySize: keylen / 4, iterations: iterations, hasher: crypto.algo.SHA256 }; key = crypto.PBKDF2(secret, salt, config); return key.toString(crypto.enc.Base64); } } function signManual(key, challenge) { if (isNode) { var hmac = crypto.createHmac('sha256', key); hmac.update(challenge); return hmac.digest('base64'); } else { return crypto.HmacSHA256(challenge, key).toString(crypto.enc.Base64); } } function sign(secret) { return function (method, info) { if (method === 'wampcra') { if (info.salt) { return signManual(deriveKey(secret, info.salt, info.iterations, info.keylen), info.challenge); } else { return signManual(secret, info.challenge); } } else { throw new Error('Unknown authentication method requested!'); } }; } //# sourceMappingURL=wampy-cra.js.map