UNPKG

solid-auth-client

Version:
143 lines (115 loc) 4.79 kB
import _regeneratorRuntime from "@babel/runtime/regenerator"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } import 'isomorphic-fetch'; import { toUrlString } from './url-util'; import { getHost, updateHostFromResponse } from './host'; import { getSession } from './session'; import { fetchWithCredentials } from './webid-oidc'; export function authnFetch(_x, _x2, _x3, _x4) { return _authnFetch.apply(this, arguments); } function _authnFetch() { _authnFetch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(storage, fetch, input, options) { var headers, session, resp; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: // Copy headers into a modifiable object if (options) { headers = copyHeaders(options.headers); options = _objectSpread(_objectSpread({}, options), {}, { headers: headers }); } // If not authenticated, perform a regular fetch _context.next = 3; return getSession(storage); case 3: session = _context.sent; if (session) { _context.next = 6; break; } return _context.abrupt("return", fetch(input, options)); case 6: _context.next = 8; return shouldShareCredentials(storage, input); case 8: if (!_context.sent) { _context.next = 10; break; } return _context.abrupt("return", fetchWithCredentials(session, fetch, input, options)); case 10: _context.next = 12; return fetch(input, options); case 12: resp = _context.sent; if (!(resp.status === 401)) { _context.next = 20; break; } _context.next = 16; return updateHostFromResponse(storage)(resp); case 16: _context.next = 18; return shouldShareCredentials(storage, input); case 18: if (!_context.sent) { _context.next = 20; break; } resp = fetchWithCredentials(session, fetch, input, options); case 20: return _context.abrupt("return", resp); case 21: case "end": return _context.stop(); } } }, _callee); })); return _authnFetch.apply(this, arguments); } function shouldShareCredentials(_x5, _x6) { return _shouldShareCredentials.apply(this, arguments); } function _shouldShareCredentials() { _shouldShareCredentials = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(storage, input) { var requestHost; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return getHost(storage)(toUrlString(input)); case 2: requestHost = _context2.sent; return _context2.abrupt("return", requestHost != null && requestHost.requiresAuth); case 4: case "end": return _context2.stop(); } } }, _callee2); })); return _shouldShareCredentials.apply(this, arguments); } function copyHeaders(origHeaders) { var headers = {}; if (origHeaders) { if (typeof origHeaders.forEach === 'function') { origHeaders.forEach(function (value, key) { headers[key] = value; }); } else { for (var key in origHeaders) { headers[key] = origHeaders[key]; } } } return headers; }