UNPKG

3box

Version:
91 lines (79 loc) 2.87 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); /** * A module to verify & validate claims * * @name idUtils * @memberof Box */ var didJWT = require('did-jwt'); var DID_MUPORT_PREFIX = 'did:muport:'; var DID_3_PREFIX = 'did:3:'; module.exports = { /** * Check whether a string is a muport did or not * * @memberOf Box.idUtils * @param {String} did A string containing a user did * @return {*|boolean} Whether the did is a supported did or not */ isSupportedDID: function isSupportedDID(did) { return did.startsWith(DID_MUPORT_PREFIX) || did.startsWith(DID_3_PREFIX); }, // for backwards compatibility isMuportDID: function isMuportDID(did) { return did.startsWith(DID_MUPORT_PREFIX); }, /** * Check whether a string is a valid claim or not * * @memberOf Box.idUtils * @param {String} claim * @param {Object} opts Optional parameters * @param {string} opts.audience The DID of the audience of the JWT * @return {Promise<boolean>} whether the parameter is an actual claim */ isClaim: function () { var _isClaim = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(claim) { var opts, _args = arguments; return _regenerator["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: opts = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}; _context.prev = 1; _context.next = 4; return didJWT.decodeJWT(claim, opts); case 4: return _context.abrupt("return", true); case 7: _context.prev = 7; _context.t0 = _context["catch"](1); return _context.abrupt("return", false); case 10: case "end": return _context.stop(); } } }, _callee, null, [[1, 7]]); })); function isClaim(_x) { return _isClaim.apply(this, arguments); } return isClaim; }(), /** * Verify a claim and return its content. * See https://github.com/uport-project/did-jwt/ for more details. * * @memberOf Box.idUtils * @param {String} claim * @param {Object} opts Optional parameters * @param {string} opts.audience The DID of the JWT's audience * @return {Object} The validated claim */ verifyClaim: didJWT.verifyJWT };