@nsshunt/stsoauth2plugin 0.1.96 → 0.1.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,335 +1,322 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
4
  };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _STSOAuth2Manager_storageManager, _STSOAuth2Manager_router, _STSOAuth2Manager_cUtils, _STSOAuth2Manager_qParams, _STSOAuth2Manager_STORAGE_AUTHORIZE_OPTIONS_KEY, _STSOAuth2Manager_STORAGE_SESSION_KEY, _STSOAuth2Manager_aic, _STSOAuth2Manager_options, _STSOAuth2Manager_messages, _STSOAuth2Manager_oauth2ManagerPort, _STSOAuth2Manager_messageId, _STSOAuth2Manager_messageHandlers, _STSOAuth2Manager_messageTimeout, _STSOAuth2Manager_worker, _STSOAuth2Manager_transactionStore, _STSOAuth2Manager_stsOauth2Store, _STSOAuth2Manager_ProcessMessageResponse, _STSOAuth2Manager_PostMessage, _STSOAuth2Manager_HandleLogEvent, _STSOAuth2Manager_HandleUpdateInstrumentEvent, _STSOAuth2Manager_HandleErrorEvent, _STSOAuth2Manager_HandleAuthenticateEvent, _STSOAuth2Manager_SetupRoute;
13
- import Debug from "debug";
14
- const debug = Debug(`proc:${process.pid}:stsoauth2manager.ts`);
15
- import { OAuth2ParameterType } from '@nsshunt/stsutils';
16
- import CryptoUtils from './Utils/CryptoUtils';
17
- import QueryParams from './Utils/QueryParams';
18
- import { IOauth2ListenerCommand } from './stsoauth2types';
19
- import { ClientStorageType, ClientStorageFactory } from './stsStorage';
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.STSOAuth2Manager = void 0;
7
+ const debug_1 = __importDefault(require("debug"));
8
+ const debug = (0, debug_1.default)(`proc:${process.pid}:stsoauth2manager.ts`);
9
+ const stsutils_1 = require("@nsshunt/stsutils");
10
+ const CryptoUtils_1 = __importDefault(require("./Utils/CryptoUtils"));
11
+ const QueryParams_1 = __importDefault(require("./Utils/QueryParams"));
12
+ const stsoauth2types_1 = require("./stsoauth2types");
13
+ const stsStorage_1 = require("./stsStorage");
20
14
  //import piniaPersist from 'pinia-plugin-persist'
21
- import { STSOauth2Store } from './stores/stsoauth2store';
15
+ const stsoauth2store_1 = require("./stores/stsoauth2store");
22
16
  // STS Client SDK for SPAs
23
- export class STSOAuth2Manager {
17
+ class STSOAuth2Manager {
18
+ #storageManager = null;
19
+ #router = null;
20
+ #cUtils = new CryptoUtils_1.default();
21
+ #qParams = new QueryParams_1.default();
22
+ #STORAGE_AUTHORIZE_OPTIONS_KEY = 'authorize_options.stsmda.com.au';
23
+ #STORAGE_SESSION_KEY = 'session.stsmda.com.au';
24
+ #aic = null;
25
+ #options = null;
26
+ #messages = {};
27
+ #oauth2ManagerPort;
28
+ #messageId = 0;
29
+ #messageHandlers = {}; // keyed by messageId
30
+ #messageTimeout = 1000;
31
+ #worker = null;
32
+ #transactionStore = null; // Transient transaction data used to establish a session via OAuth2 authorize handshake
33
+ #stsOauth2Store = null;
24
34
  constructor(app, options) {
25
- _STSOAuth2Manager_storageManager.set(this, null);
26
- _STSOAuth2Manager_router.set(this, null);
27
- _STSOAuth2Manager_cUtils.set(this, new CryptoUtils());
28
- _STSOAuth2Manager_qParams.set(this, new QueryParams());
29
- _STSOAuth2Manager_STORAGE_AUTHORIZE_OPTIONS_KEY.set(this, 'authorize_options.stsmda.com.au');
30
- _STSOAuth2Manager_STORAGE_SESSION_KEY.set(this, 'session.stsmda.com.au');
31
- _STSOAuth2Manager_aic.set(this, null);
32
- _STSOAuth2Manager_options.set(this, null);
33
- _STSOAuth2Manager_messages.set(this, {});
34
- _STSOAuth2Manager_oauth2ManagerPort.set(this, void 0);
35
- _STSOAuth2Manager_messageId.set(this, 0);
36
- _STSOAuth2Manager_messageHandlers.set(this, {}); // keyed by messageId
37
- _STSOAuth2Manager_messageTimeout.set(this, 1000);
38
- _STSOAuth2Manager_worker.set(this, null);
39
- _STSOAuth2Manager_transactionStore.set(this, null); // Transient transaction data used to establish a session via OAuth2 authorize handshake
40
- _STSOAuth2Manager_stsOauth2Store.set(this, null);
41
- _STSOAuth2Manager_ProcessMessageResponse.set(this, (data) => {
42
- const messageResponse = data.data;
43
- if (messageResponse.messageId === -1) {
44
- // unsolicted message
45
- switch (messageResponse.command) {
46
- case IOauth2ListenerCommand.AUTHENTICATE_EVENT:
47
- __classPrivateFieldGet(this, _STSOAuth2Manager_HandleAuthenticateEvent, "f").call(this, messageResponse.payload);
48
- break;
49
- case IOauth2ListenerCommand.ERROR:
50
- __classPrivateFieldGet(this, _STSOAuth2Manager_HandleErrorEvent, "f").call(this, messageResponse.payload);
51
- break;
52
- case IOauth2ListenerCommand.LOG:
53
- __classPrivateFieldGet(this, _STSOAuth2Manager_HandleLogEvent, "f").call(this, messageResponse.payload);
54
- break;
55
- case IOauth2ListenerCommand.UPDATE_INSTRUMENT:
56
- __classPrivateFieldGet(this, _STSOAuth2Manager_HandleUpdateInstrumentEvent, "f").call(this, messageResponse.payload.instrumentName, messageResponse.payload.telemetry);
57
- break;
58
- default:
59
- throw new Error(`ProcessMessageResponse command [${messageResponse.command}] not valid.`);
60
- }
61
- }
62
- else {
63
- const callBack = __classPrivateFieldGet(this, _STSOAuth2Manager_messageHandlers, "f")[messageResponse.messageId];
64
- if (callBack) {
65
- callBack(messageResponse);
66
- }
67
- else {
68
- throw new Error(`Message: [${messageResponse.messageId}] does not exists in callBacks.`);
69
- }
70
- }
71
- });
72
- _STSOAuth2Manager_PostMessage.set(this, (message) => {
73
- var _a, _b;
74
- message.messageId = (__classPrivateFieldSet(this, _STSOAuth2Manager_messageId, (_b = __classPrivateFieldGet(this, _STSOAuth2Manager_messageId, "f"), _a = _b++, _b), "f"), _a);
75
- return new Promise((resolve, reject) => {
76
- // Setup message timeout
77
- const timeout = setTimeout(() => {
78
- delete __classPrivateFieldGet(this, _STSOAuth2Manager_messageHandlers, "f")[message.messageId];
79
- reject(`Message: [${message.messageId}] timeout error after: [${__classPrivateFieldGet(this, _STSOAuth2Manager_messageTimeout, "f")}] ms.`);
80
- }, __classPrivateFieldGet(this, _STSOAuth2Manager_messageTimeout, "f"));
81
- // Setup message callback based on messageId
82
- __classPrivateFieldGet(this, _STSOAuth2Manager_messageHandlers, "f")[message.messageId] = (response) => {
83
- clearTimeout(timeout);
84
- delete __classPrivateFieldGet(this, _STSOAuth2Manager_messageHandlers, "f")[message.messageId];
85
- resolve(response);
86
- };
87
- // Send the message
88
- __classPrivateFieldGet(this, _STSOAuth2Manager_oauth2ManagerPort, "f").postMessage(message);
89
- });
90
- });
91
- _STSOAuth2Manager_HandleLogEvent.set(this, (message) => {
92
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_aic, "f")) {
93
- __classPrivateFieldGet(this, _STSOAuth2Manager_aic, "f").LogEx(message);
94
- }
95
- debug(message);
35
+ this.#options = options;
36
+ this.#storageManager = app.config.globalProperties.$sts.storage;
37
+ this.#aic = app.config.globalProperties.$sts.aic.PrimaryPublishInstrumentController;
38
+ this.#router = app.config.globalProperties.$router;
39
+ if (!this.#options.workerOptions.nodeMode) {
40
+ this.#stsOauth2Store = (0, stsoauth2store_1.STSOauth2Store)();
41
+ }
42
+ // Use session storage for the transient nature of the OAuth2 authorize handshake. Once completed, the storage will be removed.
43
+ this.#transactionStore = new stsStorage_1.ClientStorageFactory({ clientStorageType: stsStorage_1.ClientStorageType.SESSION_STORAGE }).GetStorage();
44
+ this.#worker = this.#options.workerFactory();
45
+ this.#worker.onmessage = (data) => {
46
+ console.log(`this.#worker.onmessage = [${data}]`); // green
47
+ };
48
+ this.#worker.onerror = function (error) {
49
+ console.log(`this.#worker.onerror = [${JSON.stringify(error)}]`); // green
50
+ };
51
+ const { port1: oauth2ManagerPort, // this process port
52
+ port2: oauth2WorkerPort // worker port
53
+ } = new MessageChannel();
54
+ this.#oauth2ManagerPort = oauth2ManagerPort;
55
+ const workerMessage = {
56
+ workerPort: oauth2WorkerPort,
57
+ options: this.#options.workerOptions
58
+ };
59
+ this.#worker.postMessage(workerMessage, [oauth2WorkerPort]);
60
+ this.#oauth2ManagerPort.onmessage = (data) => {
61
+ this.#ProcessMessageResponse(data);
62
+ };
63
+ if (!this.#options.workerOptions.nodeMode) {
64
+ this.#SetupRoute(app, this.#router);
96
65
  }
97
- // UpdateInstrument = (instrumentName: Gauge, telemetry: InstrumentBaseTelemetry): void => {
98
- );
99
- // UpdateInstrument = (instrumentName: Gauge, telemetry: InstrumentBaseTelemetry): void => {
100
- _STSOAuth2Manager_HandleUpdateInstrumentEvent.set(this, (instrumentName, telemetry) => {
101
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_aic, "f")) {
102
- __classPrivateFieldGet(this, _STSOAuth2Manager_aic, "f").UpdateInstrument(instrumentName, telemetry);
66
+ }
67
+ #ProcessMessageResponse = (data) => {
68
+ const messageResponse = data.data;
69
+ if (messageResponse.messageId === -1) {
70
+ // unsolicted message
71
+ switch (messageResponse.command) {
72
+ case stsoauth2types_1.IOauth2ListenerCommand.AUTHENTICATE_EVENT:
73
+ this.#HandleAuthenticateEvent(messageResponse.payload);
74
+ break;
75
+ case stsoauth2types_1.IOauth2ListenerCommand.ERROR:
76
+ this.#HandleErrorEvent(messageResponse.payload);
77
+ break;
78
+ case stsoauth2types_1.IOauth2ListenerCommand.LOG:
79
+ this.#HandleLogEvent(messageResponse.payload);
80
+ break;
81
+ case stsoauth2types_1.IOauth2ListenerCommand.UPDATE_INSTRUMENT:
82
+ this.#HandleUpdateInstrumentEvent(messageResponse.payload.instrumentName, messageResponse.payload.telemetry);
83
+ break;
84
+ default:
85
+ throw new Error(`ProcessMessageResponse command [${messageResponse.command}] not valid.`);
103
86
  }
104
87
  }
105
- // Will come from message channel
106
- );
107
- // Will come from message channel
108
- _STSOAuth2Manager_HandleErrorEvent.set(this, (error) => {
109
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").workerOptions.nodeMode) {
110
- // Node Mode ...
88
+ else {
89
+ const callBack = this.#messageHandlers[messageResponse.messageId];
90
+ if (callBack) {
91
+ callBack(messageResponse);
111
92
  }
112
93
  else {
113
- // User Agent Mode
114
- __classPrivateFieldGet(this, _STSOAuth2Manager_stsOauth2Store, "f").UpdateError({
115
- message: error
116
- });
117
- // plugin to do this ...
118
- setTimeout(() => {
119
- __classPrivateFieldGet(this, _STSOAuth2Manager_router, "f").replace('/error'); //@@ was push
120
- }, 0);
94
+ throw new Error(`Message: [${messageResponse.messageId}] does not exists in callBacks.`);
121
95
  }
96
+ }
97
+ };
98
+ #PostMessage = (message) => {
99
+ message.messageId = this.#messageId++;
100
+ return new Promise((resolve, reject) => {
101
+ // Setup message timeout
102
+ const timeout = setTimeout(() => {
103
+ delete this.#messageHandlers[message.messageId];
104
+ reject(`Message: [${message.messageId}] timeout error after: [${this.#messageTimeout}] ms.`);
105
+ }, this.#messageTimeout);
106
+ // Setup message callback based on messageId
107
+ this.#messageHandlers[message.messageId] = (response) => {
108
+ clearTimeout(timeout);
109
+ delete this.#messageHandlers[message.messageId];
110
+ resolve(response);
111
+ };
112
+ // Send the message
113
+ this.#oauth2ManagerPort.postMessage(message);
122
114
  });
123
- _STSOAuth2Manager_HandleAuthenticateEvent.set(this, (id_token) => {
124
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").workerOptions.nodeMode) {
125
- // Node Mode ...
115
+ };
116
+ #HandleLogEvent = (message) => {
117
+ if (this.#aic) {
118
+ this.#aic.LogEx(message);
119
+ }
120
+ debug(message);
121
+ };
122
+ // UpdateInstrument = (instrumentName: Gauge, telemetry: InstrumentBaseTelemetry): void => {
123
+ #HandleUpdateInstrumentEvent = (instrumentName, telemetry) => {
124
+ if (this.#aic) {
125
+ this.#aic.UpdateInstrument(instrumentName, telemetry);
126
+ }
127
+ };
128
+ // Will come from message channel
129
+ #HandleErrorEvent = (error) => {
130
+ if (this.#options.workerOptions.nodeMode) {
131
+ // Node Mode ...
132
+ }
133
+ else {
134
+ // User Agent Mode
135
+ this.#stsOauth2Store.UpdateError({
136
+ message: error
137
+ });
138
+ // plugin to do this ...
139
+ setTimeout(() => {
140
+ this.#router.replace('/error'); //@@ was push
141
+ }, 0);
142
+ }
143
+ };
144
+ #HandleAuthenticateEvent = (id_token) => {
145
+ if (this.#options.workerOptions.nodeMode) {
146
+ // Node Mode ...
147
+ }
148
+ else {
149
+ // User Agent Mode
150
+ if (this.#options.authenticateEvent) {
151
+ this.#options.authenticateEvent(id_token);
126
152
  }
127
- else {
128
- // User Agent Mode
129
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").authenticateEvent) {
130
- __classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").authenticateEvent(id_token);
153
+ this.#stsOauth2Store.UpdateIdToken(id_token);
154
+ }
155
+ };
156
+ #SetupRoute = (app, router) => {
157
+ router.beforeEach(async (to, from) => {
158
+ const sts = app.config.globalProperties.$sts;
159
+ debug(`beforeEach: from: [${from.path}], to: [${to.path}]`); // gray
160
+ if (this.#stsOauth2Store.LoggedIn === false) {
161
+ //if (store.getters['stsOAuth2SDK/LoggedIn'] === false) {
162
+ console.log(`Not logged in`);
163
+ // Not logged in
164
+ if (to.path.localeCompare('/authorize') === 0) {
165
+ console.log(`to = /authorize`);
166
+ return true;
131
167
  }
132
- __classPrivateFieldGet(this, _STSOAuth2Manager_stsOauth2Store, "f").UpdateIdToken(id_token);
133
- }
134
- });
135
- _STSOAuth2Manager_SetupRoute.set(this, (app, router) => {
136
- router.beforeEach(async (to, from) => {
137
- const sts = app.config.globalProperties.$sts;
138
- debug(`beforeEach: from: [${from.path}], to: [${to.path}]`); // gray
139
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_stsOauth2Store, "f").LoggedIn === false) {
140
- //if (store.getters['stsOAuth2SDK/LoggedIn'] === false) {
141
- console.log(`Not logged in`);
142
- // Not logged in
143
- if (to.path.localeCompare('/authorize') === 0) {
144
- console.log(`to = /authorize`);
145
- return true;
146
- }
147
- else if (to.path.localeCompare('/consent') === 0) {
148
- // Need to check if we are in the correct state, if not - drop back to the start of the process
149
- if (__classPrivateFieldGet(this, _STSOAuth2Manager_stsOauth2Store, "f").id_token) {
150
- //if (typeof store.getters.Session.sessionId !== 'undefined') {
151
- return true;
152
- }
153
- }
154
- if (to.path.localeCompare('/logout') === 0) {
155
- return true;
156
- }
157
- if (to.path.localeCompare('/error') === 0) {
158
- return true;
159
- }
160
- if (to.path.localeCompare('/logout') === 0) {
168
+ else if (to.path.localeCompare('/consent') === 0) {
169
+ // Need to check if we are in the correct state, if not - drop back to the start of the process
170
+ if (this.#stsOauth2Store.id_token) {
171
+ //if (typeof store.getters.Session.sessionId !== 'undefined') {
161
172
  return true;
162
173
  }
163
- const str = to.query;
164
- // Check if this route is from a redirect from the authorization server
165
- if (str[OAuth2ParameterType.CODE] || str[OAuth2ParameterType.ERROR]) {
166
- debug(`STSOAuth2Manager:#SetupRoute:Processing redirect: [${str}]`);
167
- const retVal = await sts.om.HandleRedirect(str);
168
- debug(`STSOAuth2Manager:#SetupRoute:Redirect result: [${retVal}]`);
169
- if (retVal) {
170
- // Success
171
- setTimeout(() => {
172
- window.history.replaceState({}, document.title, window.location.origin + '/');
173
- }, 0);
174
- return true;
175
- }
176
- else {
177
- // Error
178
- //@@ need the error data here - or use the vuex store ?
179
- __classPrivateFieldGet(this, _STSOAuth2Manager_router, "f").replace('/error'); //@@ was push
180
- //@@ should replaceState be used as in above?
181
- return false;
182
- }
183
- }
184
- debug(`STSOAuth2Manager:#SetupRoute:Restoring session`);
185
- const sessionRestored = await sts.om.RestoreSession();
186
- debug(`STSOAuth2Manager:#SetupRoute:Session restored: [${sessionRestored}]`);
187
- if (sessionRestored !== true) {
188
- debug(`STSOAuth2Manager:#SetupRoute:Session not restored - need to authorize`);
189
- sts.om.Authorize();
190
- return false;
191
- }
192
- else {
193
- return '/';
194
- //router.replace({ path: '/' })
195
- }
196
174
  }
197
- else {
198
- // Prevent pages if already logged in
199
- if (to.path.localeCompare('/consent') === 0) {
200
- return '/';
201
- /*
202
- router.replace({ path: '/' })
203
- return false;
204
- */
205
- }
206
- if (to.path.localeCompare('/authorize') === 0) {
207
- router.replace({ path: '/' });
208
- return false;
209
- }
210
- if (to.path.localeCompare('/logout') === 0) {
211
- router.replace({ path: '/' });
212
- return false;
213
- }
175
+ if (to.path.localeCompare('/logout') === 0) {
214
176
  return true;
215
- /*
216
- if (to.path.localeCompare('/') === 0) {
217
- // In case press the back button in the browser shows previous query string params, replace them ...
177
+ }
178
+ if (to.path.localeCompare('/error') === 0) {
179
+ return true;
180
+ }
181
+ if (to.path.localeCompare('/logout') === 0) {
182
+ return true;
183
+ }
184
+ const str = to.query;
185
+ // Check if this route is from a redirect from the authorization server
186
+ if (str[stsutils_1.OAuth2ParameterType.CODE] || str[stsutils_1.OAuth2ParameterType.ERROR]) {
187
+ debug(`STSOAuth2Manager:#SetupRoute:Processing redirect: [${str}]`);
188
+ const retVal = await sts.om.HandleRedirect(str);
189
+ debug(`STSOAuth2Manager:#SetupRoute:Redirect result: [${retVal}]`);
190
+ if (retVal) {
191
+ // Success
218
192
  setTimeout(() => {
219
- window.history.replaceState(
220
- {},
221
- document.title,
222
- window.location.origin + '/');
193
+ window.history.replaceState({}, document.title, window.location.origin + '/');
223
194
  }, 0);
224
195
  return true;
225
196
  }
226
- */
197
+ else {
198
+ // Error
199
+ //@@ need the error data here - or use the vuex store ?
200
+ this.#router.replace('/error'); //@@ was push
201
+ //@@ should replaceState be used as in above?
202
+ return false;
203
+ }
227
204
  }
228
- });
229
- }
230
- //@@ this needs to be re-implemented so that access_token never leaves the auth worker
231
- );
232
- //@@ this needs to be re-implemented so that access_token never leaves the auth worker
233
- this.GetAccessToken = async () => {
234
- const response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.ACCESS_TOKEN });
235
- return response.payload;
236
- };
237
- this.RestoreSession = async () => {
238
- try {
239
- debug(`STSOAuth2Manager:RestoreSession:postMessage: [${IOauth2ListenerCommand.RESTORE_SESSION}]`);
240
- const response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.RESTORE_SESSION });
241
- debug(`STSOAuth2Manager:RestoreSession:response: [${JSON.stringify(response.payload)}]`);
242
- return response.payload;
243
- }
244
- catch (error) {
245
- console.log(`RestoreSession Error: ${error}`); //red
246
- return false;
247
- }
248
- };
249
- this.Authorize = async () => {
250
- try {
251
- const response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.AUTHORIZE });
252
- __classPrivateFieldGet(this, _STSOAuth2Manager_transactionStore, "f").set(__classPrivateFieldGet(this, _STSOAuth2Manager_STORAGE_AUTHORIZE_OPTIONS_KEY, "f"), response.payload.authorizeOptions);
253
- const url = response.payload.url;
254
- window.location.replace(url);
255
- }
256
- catch (error) {
257
- console.log(`Authorize Error: ${error}`); // red
258
- }
259
- };
260
- this.HandleRedirect = async (queryVars) => {
261
- try {
262
- let response = null;
263
- if (queryVars[OAuth2ParameterType.CODE]) {
264
- const authorizeOptions = __classPrivateFieldGet(this, _STSOAuth2Manager_transactionStore, "f").get(__classPrivateFieldGet(this, _STSOAuth2Manager_STORAGE_AUTHORIZE_OPTIONS_KEY, "f"));
265
- __classPrivateFieldGet(this, _STSOAuth2Manager_transactionStore, "f").remove(__classPrivateFieldGet(this, _STSOAuth2Manager_STORAGE_AUTHORIZE_OPTIONS_KEY, "f"));
266
- response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.HANDLE_REDIRECT, payload: {
267
- queryVars: queryVars,
268
- authorizeOptions
269
- } });
205
+ debug(`STSOAuth2Manager:#SetupRoute:Restoring session`);
206
+ const sessionRestored = await sts.om.RestoreSession();
207
+ debug(`STSOAuth2Manager:#SetupRoute:Session restored: [${sessionRestored}]`);
208
+ if (sessionRestored !== true) {
209
+ debug(`STSOAuth2Manager:#SetupRoute:Session not restored - need to authorize`);
210
+ sts.om.Authorize();
211
+ return false;
270
212
  }
271
213
  else {
272
- response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.HANDLE_REDIRECT, payload: queryVars });
214
+ return '/';
215
+ //router.replace({ path: '/' })
273
216
  }
274
- return response.payload;
275
- }
276
- catch (error) {
277
- console.log(`HandleRedirect Error: ${error}`); // red
278
- return false;
279
217
  }
280
- };
281
- this.Logout = async () => {
282
- try {
283
- const response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.LOGOUT });
284
- return response.payload;
285
- }
286
- catch (error) {
287
- console.log(`Logout Error: ${error}`); // red
288
- return false;
218
+ else {
219
+ // Prevent pages if already logged in
220
+ if (to.path.localeCompare('/consent') === 0) {
221
+ return '/';
222
+ /*
223
+ router.replace({ path: '/' })
224
+ return false;
225
+ */
226
+ }
227
+ if (to.path.localeCompare('/authorize') === 0) {
228
+ router.replace({ path: '/' });
229
+ return false;
230
+ }
231
+ if (to.path.localeCompare('/logout') === 0) {
232
+ router.replace({ path: '/' });
233
+ return false;
234
+ }
235
+ return true;
236
+ /*
237
+ if (to.path.localeCompare('/') === 0) {
238
+ // In case press the back button in the browser shows previous query string params, replace them ...
239
+ setTimeout(() => {
240
+ window.history.replaceState(
241
+ {},
242
+ document.title,
243
+ window.location.origin + '/');
244
+ }, 0);
245
+ return true;
246
+ }
247
+ */
289
248
  }
290
- };
291
- this.InvokeExternalAPI = async () => {
292
- try {
293
- const response = await __classPrivateFieldGet(this, _STSOAuth2Manager_PostMessage, "f").call(this, { command: IOauth2ListenerCommand.LOGOUT });
294
- return response.payload;
249
+ });
250
+ };
251
+ //@@ this needs to be re-implemented so that access_token never leaves the auth worker
252
+ GetAccessToken = async () => {
253
+ const response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.ACCESS_TOKEN });
254
+ return response.payload;
255
+ };
256
+ RestoreSession = async () => {
257
+ try {
258
+ debug(`STSOAuth2Manager:RestoreSession:postMessage: [${stsoauth2types_1.IOauth2ListenerCommand.RESTORE_SESSION}]`);
259
+ const response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.RESTORE_SESSION });
260
+ debug(`STSOAuth2Manager:RestoreSession:response: [${JSON.stringify(response.payload)}]`);
261
+ return response.payload;
262
+ }
263
+ catch (error) {
264
+ console.log(`RestoreSession Error: ${error}`); //red
265
+ return false;
266
+ }
267
+ };
268
+ Authorize = async () => {
269
+ try {
270
+ const response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.AUTHORIZE });
271
+ this.#transactionStore.set(this.#STORAGE_AUTHORIZE_OPTIONS_KEY, response.payload.authorizeOptions);
272
+ const url = response.payload.url;
273
+ window.location.replace(url);
274
+ }
275
+ catch (error) {
276
+ console.log(`Authorize Error: ${error}`); // red
277
+ }
278
+ };
279
+ HandleRedirect = async (queryVars) => {
280
+ try {
281
+ let response = null;
282
+ if (queryVars[stsutils_1.OAuth2ParameterType.CODE]) {
283
+ const authorizeOptions = this.#transactionStore.get(this.#STORAGE_AUTHORIZE_OPTIONS_KEY);
284
+ this.#transactionStore.remove(this.#STORAGE_AUTHORIZE_OPTIONS_KEY);
285
+ response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.HANDLE_REDIRECT, payload: {
286
+ queryVars: queryVars,
287
+ authorizeOptions
288
+ } });
295
289
  }
296
- catch (error) {
297
- console.log(`InvokeExternalAPI Error: ${error}`); // red
298
- return null;
290
+ else {
291
+ response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.HANDLE_REDIRECT, payload: queryVars });
299
292
  }
300
- };
301
- __classPrivateFieldSet(this, _STSOAuth2Manager_options, options, "f");
302
- __classPrivateFieldSet(this, _STSOAuth2Manager_storageManager, app.config.globalProperties.$sts.storage, "f");
303
- __classPrivateFieldSet(this, _STSOAuth2Manager_aic, app.config.globalProperties.$sts.aic.PrimaryPublishInstrumentController, "f");
304
- __classPrivateFieldSet(this, _STSOAuth2Manager_router, app.config.globalProperties.$router, "f");
305
- if (!__classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").workerOptions.nodeMode) {
306
- __classPrivateFieldSet(this, _STSOAuth2Manager_stsOauth2Store, STSOauth2Store(), "f");
293
+ return response.payload;
307
294
  }
308
- // Use session storage for the transient nature of the OAuth2 authorize handshake. Once completed, the storage will be removed.
309
- __classPrivateFieldSet(this, _STSOAuth2Manager_transactionStore, new ClientStorageFactory({ clientStorageType: ClientStorageType.SESSION_STORAGE }).GetStorage(), "f");
310
- __classPrivateFieldSet(this, _STSOAuth2Manager_worker, __classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").workerFactory(), "f");
311
- __classPrivateFieldGet(this, _STSOAuth2Manager_worker, "f").onmessage = (data) => {
312
- console.log(`this.#worker.onmessage = [${data}]`); // green
313
- };
314
- __classPrivateFieldGet(this, _STSOAuth2Manager_worker, "f").onerror = function (error) {
315
- console.log(`this.#worker.onerror = [${JSON.stringify(error)}]`); // green
316
- };
317
- const { port1: oauth2ManagerPort, // this process port
318
- port2: oauth2WorkerPort // worker port
319
- } = new MessageChannel();
320
- __classPrivateFieldSet(this, _STSOAuth2Manager_oauth2ManagerPort, oauth2ManagerPort, "f");
321
- const workerMessage = {
322
- workerPort: oauth2WorkerPort,
323
- options: __classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").workerOptions
324
- };
325
- __classPrivateFieldGet(this, _STSOAuth2Manager_worker, "f").postMessage(workerMessage, [oauth2WorkerPort]);
326
- __classPrivateFieldGet(this, _STSOAuth2Manager_oauth2ManagerPort, "f").onmessage = (data) => {
327
- __classPrivateFieldGet(this, _STSOAuth2Manager_ProcessMessageResponse, "f").call(this, data);
328
- };
329
- if (!__classPrivateFieldGet(this, _STSOAuth2Manager_options, "f").workerOptions.nodeMode) {
330
- __classPrivateFieldGet(this, _STSOAuth2Manager_SetupRoute, "f").call(this, app, __classPrivateFieldGet(this, _STSOAuth2Manager_router, "f"));
295
+ catch (error) {
296
+ console.log(`HandleRedirect Error: ${error}`); // red
297
+ return false;
331
298
  }
332
- }
299
+ };
300
+ Logout = async () => {
301
+ try {
302
+ const response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.LOGOUT });
303
+ return response.payload;
304
+ }
305
+ catch (error) {
306
+ console.log(`Logout Error: ${error}`); // red
307
+ return false;
308
+ }
309
+ };
310
+ InvokeExternalAPI = async () => {
311
+ try {
312
+ const response = await this.#PostMessage({ command: stsoauth2types_1.IOauth2ListenerCommand.LOGOUT });
313
+ return response.payload;
314
+ }
315
+ catch (error) {
316
+ console.log(`InvokeExternalAPI Error: ${error}`); // red
317
+ return null;
318
+ }
319
+ };
333
320
  }
334
- _STSOAuth2Manager_storageManager = new WeakMap(), _STSOAuth2Manager_router = new WeakMap(), _STSOAuth2Manager_cUtils = new WeakMap(), _STSOAuth2Manager_qParams = new WeakMap(), _STSOAuth2Manager_STORAGE_AUTHORIZE_OPTIONS_KEY = new WeakMap(), _STSOAuth2Manager_STORAGE_SESSION_KEY = new WeakMap(), _STSOAuth2Manager_aic = new WeakMap(), _STSOAuth2Manager_options = new WeakMap(), _STSOAuth2Manager_messages = new WeakMap(), _STSOAuth2Manager_oauth2ManagerPort = new WeakMap(), _STSOAuth2Manager_messageId = new WeakMap(), _STSOAuth2Manager_messageHandlers = new WeakMap(), _STSOAuth2Manager_messageTimeout = new WeakMap(), _STSOAuth2Manager_worker = new WeakMap(), _STSOAuth2Manager_transactionStore = new WeakMap(), _STSOAuth2Manager_stsOauth2Store = new WeakMap(), _STSOAuth2Manager_ProcessMessageResponse = new WeakMap(), _STSOAuth2Manager_PostMessage = new WeakMap(), _STSOAuth2Manager_HandleLogEvent = new WeakMap(), _STSOAuth2Manager_HandleUpdateInstrumentEvent = new WeakMap(), _STSOAuth2Manager_HandleErrorEvent = new WeakMap(), _STSOAuth2Manager_HandleAuthenticateEvent = new WeakMap(), _STSOAuth2Manager_SetupRoute = new WeakMap();
321
+ exports.STSOAuth2Manager = STSOAuth2Manager;
335
322
  //# sourceMappingURL=stsoauth2manager.js.map