|
| 1 | +"use strict"; |
| 2 | +/* Login and return session for NSS/PSS via solid-auth-fetcher |
| 3 | +*/ |
| 4 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 5 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 6 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 7 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 8 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 9 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 10 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 11 | + }); |
| 12 | +}; |
| 13 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 14 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 15 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 16 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 17 | + function step(op) { |
| 18 | + if (f) throw new TypeError("Generator is already executing."); |
| 19 | + while (_) try { |
| 20 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 21 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 22 | + switch (op[0]) { |
| 23 | + case 0: case 1: t = op; break; |
| 24 | + case 4: _.label++; return { value: op[1], done: false }; |
| 25 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 26 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 27 | + default: |
| 28 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 29 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 30 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 31 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 32 | + if (t[2]) _.ops.pop(); |
| 33 | + _.trys.pop(); continue; |
| 34 | + } |
| 35 | + op = body.call(thisArg, _); |
| 36 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 37 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 38 | + } |
| 39 | +}; |
| 40 | +exports.__esModule = true; |
| 41 | +exports.NssAuthSession = void 0; |
| 42 | +var solid_auth_fetcher_1 = require("solid-auth-fetcher"); |
| 43 | +var NssAuthSession = /** @class */ (function () { |
| 44 | + function NssAuthSession() { |
| 45 | + } |
| 46 | + NssAuthSession.prototype.login = function (options) { |
| 47 | + return __awaiter(this, void 0, void 0, function () { |
| 48 | + var self; |
| 49 | + var _this = this; |
| 50 | + return __generator(this, function (_a) { |
| 51 | + options = options || {}; |
| 52 | + options.idp = options.idp || process.env.SOLID_IDP || ""; |
| 53 | + options.username = options.username || process.env.SOLID_USERNAME; |
| 54 | + options.password = options.password || process.env.SOLID_PASSWORD; |
| 55 | + options.debug = options.debug || (process.env.SOLID_DEBUG) ? true : false; |
| 56 | + self = this; |
| 57 | + return [2 /*return*/, new Promise(function (resolve, reject) { |
| 58 | + _this._getAuthFetcher(options, function (session) { |
| 59 | + self.session = session; |
| 60 | + resolve(session); |
| 61 | + }); |
| 62 | + })]; |
| 63 | + }); |
| 64 | + }); |
| 65 | + }; |
| 66 | + NssAuthSession.prototype._getAuthFetcher = function (options, callback) { |
| 67 | + return __awaiter(this, void 0, void 0, function () { |
| 68 | + var cookie, e_1, _a, session; |
| 69 | + var _this = this; |
| 70 | + return __generator(this, function (_b) { |
| 71 | + switch (_b.label) { |
| 72 | + case 0: |
| 73 | + _b.trys.push([0, 2, , 3]); |
| 74 | + return [4 /*yield*/, solid_auth_fetcher_1.getNodeSolidServerCookie(options.idp, options.username, options.password)]; |
| 75 | + case 1: |
| 76 | + cookie = _b.sent(); |
| 77 | + return [3 /*break*/, 3]; |
| 78 | + case 2: |
| 79 | + e_1 = _b.sent(); |
| 80 | + this.authFetcher = null; |
| 81 | + return [2 /*return*/, callback(null)]; |
| 82 | + case 3: |
| 83 | + if (!cookie || !cookie.match(/nssidp/)) { |
| 84 | + // couldn't login |
| 85 | + this.authFetcher = null; |
| 86 | + return [2 /*return*/, callback(null)]; |
| 87 | + } |
| 88 | + _a = this; |
| 89 | + return [4 /*yield*/, solid_auth_fetcher_1.getAuthFetcher(options.idp, cookie, "https://solid-node-client")]; |
| 90 | + case 4: |
| 91 | + _a.authFetcher = _b.sent(); |
| 92 | + return [4 /*yield*/, solid_auth_fetcher_1.getSession()]; |
| 93 | + case 5: |
| 94 | + session = _b.sent(); |
| 95 | + this.authFetcher.onSession(function (s) { return __awaiter(_this, void 0, void 0, function () { |
| 96 | + return __generator(this, function (_a) { |
| 97 | + s.info = {}; |
| 98 | + s.info.isLoggedIn = s.isLoggedIn = s.loggedIn; |
| 99 | + s.info.webId = s.webId; |
| 100 | + s.info.isNss = true; |
| 101 | + callback(s); |
| 102 | + return [2 /*return*/]; |
| 103 | + }); |
| 104 | + }); }); |
| 105 | + return [2 /*return*/]; |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + }; |
| 110 | + return NssAuthSession; |
| 111 | +}()); |
| 112 | +exports.NssAuthSession = NssAuthSession; |
| 113 | +// ENDS |
0 commit comments