UNPKG

@treecg/ldes-orchestrator

Version:

Fills the gaps that a Linked Data Platform (LDP) cannot do by itself for creating a Linked Data Event Stream (LDES) in LDP.

181 lines (180 loc) 9.59 kB
"use strict"; /*************************************** * Title: Login.ts * Description: TODO * Author: Wout Slabbinck (wout.slabbinck@ugent.be) * Created on 26/11/2021 *****************************************/ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSession = exports.isLoggedin = exports.login = void 0; var fs_1 = require("fs"); var path_1 = __importDefault(require("path")); var solid_client_authn_isomorphic_1 = require("@rubensworks/solid-client-authn-isomorphic"); var dotenv_1 = require("dotenv"); var express_1 = __importDefault(require("express")); var Util_1 = require("./util/Util"); (0, dotenv_1.config)(); var validatedOptions = { applicationName: "LDES-orchestrator", registrationType: "dynamic", solidIdentityProvider: process.env.SOLID_IDP }; function login() { return __awaiter(this, void 0, void 0, function () { var app, port, iriBase, storage, session, server; var _this = this; return __generator(this, function (_a) { app = (0, express_1.default)(); port = 3000; iriBase = "http://localhost:".concat(port); storage = new solid_client_authn_isomorphic_1.InMemoryStorage(); session = new solid_client_authn_isomorphic_1.Session({ insecureStorage: storage, secureStorage: storage, }); server = app.listen(port, function () { return __awaiter(_this, void 0, void 0, function () { var loginOptions; return __generator(this, function (_a) { console.log("Listening at: [".concat(iriBase, "].")); loginOptions = { clientName: validatedOptions.applicationName, oidcIssuer: validatedOptions.solidIdentityProvider, redirectUrl: iriBase, tokenType: "DPoP", handleRedirect: function (url) { console.log("\nPlease visit ".concat(url, " in a web browser.\n")); }, }; console.log("Logging in to Solid Identity Provider ".concat(validatedOptions.solidIdentityProvider, " to get a refresh token.")); session.login(loginOptions).catch(function (e) { throw new Error("Logging in to Solid Identity Provider [".concat(validatedOptions.solidIdentityProvider, "] failed: ").concat(e.toString())); }); return [2 /*return*/]; }); }); }); app.get("/", function (_req, res) { return __awaiter(_this, void 0, void 0, function () { var redirectIri, rawStoredSession, storedSession; return __generator(this, function (_a) { switch (_a.label) { case 0: redirectIri = new URL(_req.url, iriBase).href; console.log("Login into the Identity Provider successful, receiving request to redirect IRI [".concat(redirectIri, "].")); return [4 /*yield*/, session.handleIncomingRedirect(redirectIri)]; case 1: _a.sent(); return [4 /*yield*/, storage.get("solidClientAuthenticationUser:".concat(session.info.sessionId))]; case 2: rawStoredSession = _a.sent(); if (rawStoredSession === undefined) { throw new Error("Cannot find session with ID [".concat(session.info.sessionId, "] in storage.")); } storedSession = JSON.parse(rawStoredSession); console.log("\nThese are your login credentials:\n{\n \"refreshToken\" : \"".concat(storedSession.refreshToken, "\",\n \"clientId\" : \"").concat(storedSession.clientId, "\",\n \"clientSecret\" : \"").concat(storedSession.clientSecret, "\",\n \"issuer\" : \"").concat(storedSession.issuer, "\",\n}\n")); res.send("The tokens have been sent to @inrupt/generate-oidc-token. You can close this window."); // write session away (0, fs_1.writeFileSync)(path_1.default.join(__dirname, 'config.json'), JSON.stringify(storedSession)); server.close(); return [2 /*return*/]; } }); }); }); return [2 /*return*/]; }); }); } exports.login = login; /** * Function only stops when a config file is created -> indicating that a user is logged in */ function isLoggedin() { return __awaiter(this, void 0, void 0, function () { var rootPath, loggedIn, files; return __generator(this, function (_a) { switch (_a.label) { case 0: rootPath = __dirname; loggedIn = false; _a.label = 1; case 1: if (!!loggedIn) return [3 /*break*/, 3]; files = (0, fs_1.readdirSync)(rootPath); if (files.includes('config.json')) { loggedIn = true; return [3 /*break*/, 3]; } return [4 /*yield*/, (0, Util_1.sleep)(1000)]; case 2: _a.sent(); return [3 /*break*/, 1]; case 3: return [2 /*return*/]; } }); }); } exports.isLoggedin = isLoggedin; function getSession() { return __awaiter(this, void 0, void 0, function () { var configPath, credentials, session; return __generator(this, function (_a) { switch (_a.label) { case 0: configPath = path_1.default.join(__dirname, 'config.json'); credentials = JSON.parse((0, fs_1.readFileSync)(configPath, 'utf-8')); session = new solid_client_authn_isomorphic_1.Session(); session.onNewRefreshToken(function (newToken) { console.log("New refresh token: ", newToken); }); return [4 /*yield*/, session.login({ clientId: credentials.clientId, clientSecret: credentials.clientSecret, refreshToken: credentials.refreshToken, oidcIssuer: credentials.issuer, })]; case 1: _a.sent(); (0, fs_1.unlinkSync)(configPath); return [2 /*return*/, session]; } }); }); } exports.getSession = getSession;