1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.SessionManager = void 0;
|
13 | const events_1 = require("events");
|
14 | const tsyringe_1 = require("tsyringe");
|
15 | const Session_1 = require("./Session");
|
16 | const dependencies_1 = require("./dependencies");
|
17 | let SessionManager = class SessionManager extends events_1.EventEmitter {
|
18 | constructor(options = {}) {
|
19 | super();
|
20 | this.sessionRecords = {};
|
21 | this.isInitialized = false;
|
22 | this.handledIncomingRedirect = false;
|
23 | this.clientAuthn = dependencies_1.getClientAuthenticationWithDependencies({
|
24 | secureStorage: options.secureStorage,
|
25 | insecureStorage: options.insecureStorage,
|
26 | });
|
27 | }
|
28 | async init() {
|
29 | if (!this.isInitialized) {
|
30 | await this.handleIncomingRedirect(window.location.href);
|
31 | this.isInitialized = true;
|
32 | }
|
33 | }
|
34 | addNewSessionRecord(session) {
|
35 | const logoutCallback = () => {
|
36 | this.emit("sessionLogout", session);
|
37 | };
|
38 | session.onLogout(logoutCallback);
|
39 | this.sessionRecords[session.info.sessionId] = {
|
40 | session,
|
41 | logoutCallback,
|
42 | };
|
43 | return session;
|
44 | }
|
45 | getSessionFromCurrentSessionInfo(sessionInfo) {
|
46 | const sessionRecord = this.sessionRecords[sessionInfo.sessionId];
|
47 | if (sessionRecord) {
|
48 | sessionRecord.session.info.webId = sessionInfo.webId;
|
49 | sessionRecord.session.info.isLoggedIn = sessionInfo.isLoggedIn;
|
50 | return sessionRecord.session;
|
51 | }
|
52 | return this.addNewSessionRecord(new Session_1.Session({
|
53 | clientAuthentication: this.clientAuthn,
|
54 | sessionInfo,
|
55 | }));
|
56 | }
|
57 | async getSessions() {
|
58 | await this.init();
|
59 | const sessionInfos = await this.clientAuthn.getAllSessionInfo();
|
60 | return sessionInfos.map((sessionInfo) => this.getSessionFromCurrentSessionInfo(sessionInfo));
|
61 | }
|
62 | async getSession(sessionId) {
|
63 | await this.init();
|
64 | let session;
|
65 | if (sessionId) {
|
66 | const retrievedSessionInfo = await this.clientAuthn.getSessionInfo(sessionId);
|
67 | if (retrievedSessionInfo) {
|
68 | session = this.getSessionFromCurrentSessionInfo(retrievedSessionInfo);
|
69 | }
|
70 | else {
|
71 | session = this.addNewSessionRecord(new Session_1.Session({ clientAuthentication: this.clientAuthn }, sessionId));
|
72 | }
|
73 | }
|
74 | else {
|
75 | session = this.addNewSessionRecord(new Session_1.Session({ clientAuthentication: this.clientAuthn }));
|
76 | }
|
77 | return session;
|
78 | }
|
79 | async hasSession(sessionId) {
|
80 | await this.init();
|
81 | return (await this.clientAuthn.getSessionInfo(sessionId)) !== undefined;
|
82 | }
|
83 | onSessionLogin(callback) {
|
84 | this.on("sessionLogin", callback);
|
85 | }
|
86 | onSessionLogout(callback) {
|
87 | this.on("sessionLogout", callback);
|
88 | }
|
89 | detachSession(sessionId) {
|
90 | const sessionRecord = this.sessionRecords[sessionId];
|
91 | if (sessionRecord) {
|
92 | sessionRecord.session.removeListener("onLogout", sessionRecord.logoutCallback);
|
93 | delete this.sessionRecords[sessionId];
|
94 | }
|
95 | }
|
96 | async handleIncomingRedirect(url) {
|
97 | const sessionInfo = await this.clientAuthn.handleIncomingRedirect(url);
|
98 | if (sessionInfo) {
|
99 | const session = this.getSessionFromCurrentSessionInfo(sessionInfo);
|
100 | this.emit("sessionLogin", session);
|
101 | session.emit("login");
|
102 | return session;
|
103 | }
|
104 | return undefined;
|
105 | }
|
106 | };
|
107 | SessionManager = __decorate([
|
108 | tsyringe_1.injectable(),
|
109 | __metadata("design:paramtypes", [Object])
|
110 | ], SessionManager);
|
111 | exports.SessionManager = SessionManager;
|
112 |
|
\ | No newline at end of file |