@openremote/core 1.0.2 → 1.2.0-snapshot.20240512155932

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.
Files changed (58) hide show
  1. package/README.md +86 -8
  2. package/dist/umd/index.bundle.js +2 -0
  3. package/dist/umd/index.bundle.js.LICENSE.txt +26 -0
  4. package/dist/umd/index.js +2 -0
  5. package/dist/umd/index.js.LICENSE.txt +22 -0
  6. package/dist/umd/index.orbundle.js +50 -0
  7. package/dist/umd/index.orbundle.js.LICENSE.txt +38 -0
  8. package/lib/asset-mixin.d.ts +46 -0
  9. package/lib/asset-mixin.js +1 -0
  10. package/lib/asset-mixin.js.map +1 -0
  11. package/{dist → lib}/console.d.ts +21 -12
  12. package/lib/console.js +1 -0
  13. package/lib/console.js.map +1 -0
  14. package/lib/defaults.d.ts +15 -0
  15. package/lib/defaults.js +1 -0
  16. package/lib/defaults.js.map +1 -0
  17. package/{dist → lib}/event.d.ts +30 -16
  18. package/lib/event.js +1 -0
  19. package/lib/event.js.map +1 -0
  20. package/lib/index.d.ts +150 -0
  21. package/lib/index.js +1 -0
  22. package/lib/index.js.map +1 -0
  23. package/lib/util.d.ts +92 -0
  24. package/lib/util.js +1 -0
  25. package/lib/util.js.map +1 -0
  26. package/package.json +31 -14
  27. package/dist/asset-mixin.d.ts +0 -25
  28. package/dist/asset-mixin.js +0 -115
  29. package/dist/asset-mixin.js.map +0 -1
  30. package/dist/console.js +0 -446
  31. package/dist/console.js.map +0 -1
  32. package/dist/event.js +0 -454
  33. package/dist/event.js.map +0 -1
  34. package/dist/index.d.ts +0 -137
  35. package/dist/index.js +0 -683
  36. package/dist/index.js.map +0 -1
  37. package/dist/util.d.ts +0 -17
  38. package/dist/util.js +0 -72
  39. package/dist/util.js.map +0 -1
  40. package/src/asset-mixin.ts +0 -132
  41. package/src/console.d.ts +0 -56
  42. package/src/console.js +0 -451
  43. package/src/console.js.map +0 -1
  44. package/src/console.ts +0 -530
  45. package/src/event.d.ts +0 -75
  46. package/src/event.js +0 -410
  47. package/src/event.js.map +0 -1
  48. package/src/event.ts +0 -584
  49. package/src/index.d.ts +0 -110
  50. package/src/index.js +0 -525
  51. package/src/index.js.map +0 -1
  52. package/src/index.ts +0 -803
  53. package/src/util.d.ts +0 -15
  54. package/src/util.js +0 -46
  55. package/src/util.js.map +0 -1
  56. package/src/util.ts +0 -94
  57. package/tsconfig.json +0 -14
  58. package/tsconfig.tsbuildinfo +0 -9788
package/dist/index.js DELETED
@@ -1,683 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- return new (P || (P = Promise))(function (resolve, reject) {
3
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
4
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
5
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
6
- step((generator = generator.apply(thisArg, _arguments || [])).next());
7
- });
8
- };
9
- import "url-search-params-polyfill";
10
- import { Console } from "./console";
11
- import rest from "@openremote/rest";
12
- import { IconSets } from "@openremote/or-icon";
13
- import { EventProviderStatus, WebSocketEventProvider } from "./event";
14
- import i18next from "i18next";
15
- import i18nextXhr from "i18next-xhr-backend";
16
- export var ORError;
17
- (function (ORError) {
18
- ORError["NONE"] = "NONE";
19
- ORError["MANAGER_FAILED_TO_LOAD"] = "MANAGER_FAILED_TO_LOAD";
20
- ORError["KEYCLOAK_FAILED_TO_LOAD"] = "KEYCLOAK_FAILED_TO_LOAD";
21
- ORError["AUTH_TYPE_UNSUPPORTED"] = "AUTH_TYPE_UNSUPPORTED";
22
- ORError["CONSOLE_ERROR"] = "CONSOLE_INIT_ERROR";
23
- ORError["EVENTS_CONNECTION_ERROR"] = "EVENTS_CONNECTION_ERROR";
24
- })(ORError || (ORError = {}));
25
- export var Auth;
26
- (function (Auth) {
27
- Auth["KEYCLOAK"] = "KEYCLOAK";
28
- Auth["BASIC"] = "BASIC";
29
- Auth["NONE"] = "NONE";
30
- })(Auth || (Auth = {}));
31
- export var OREvent;
32
- (function (OREvent) {
33
- OREvent["ERROR"] = "ERROR";
34
- OREvent["READY"] = "READY";
35
- OREvent["CONSOLE_INIT"] = "CONSOLE_INIT";
36
- OREvent["CONSOLE_READY"] = "CONSOLE_READY";
37
- OREvent["EVENTS_CONNECTED"] = "EVENTS_CONNECTED";
38
- OREvent["EVENTS_CONNECTING"] = "EVENTS_CONNECTING";
39
- OREvent["EVENTS_DISCONNECTED"] = "EVENTS_DISCONNECTED";
40
- OREvent["TRANSLATE_INIT"] = "TRANSLATE_INIT";
41
- OREvent["TRANSLATE_LANGUAGE_CHANGED"] = "TRANSLATE_LANGUAGE_CHANGED";
42
- })(OREvent || (OREvent = {}));
43
- export var EventProviderType;
44
- (function (EventProviderType) {
45
- EventProviderType["WEBSOCKET"] = "WEBSOCKET";
46
- EventProviderType["POLLING"] = "POLLING";
47
- })(EventProviderType || (EventProviderType = {}));
48
- export class AssetModelUtil {
49
- static getAssetDescriptors() {
50
- return this._assetDescriptors;
51
- }
52
- static getAttributeDescriptors() {
53
- return this._attributeDescriptors;
54
- }
55
- static getAttributeValueDescriptors() {
56
- return this._attributeValueDescriptors;
57
- }
58
- static getMetaItemDescriptors() {
59
- return this._metaItemDescriptors;
60
- }
61
- static getAssetDescriptor(type) {
62
- if (!type) {
63
- return;
64
- }
65
- return this._assetDescriptors.find((assetDescriptor) => {
66
- return assetDescriptor.type === type;
67
- });
68
- }
69
- static getAssetAttributeDescriptor(assetDescriptor, attributeName) {
70
- if (!attributeName || !assetDescriptor || !assetDescriptor.attributeDescriptors) {
71
- return;
72
- }
73
- return assetDescriptor.attributeDescriptors.find((attributeDescriptor) => attributeDescriptor.attributeName === attributeName);
74
- }
75
- static getAttributeDescriptor(attributeName) {
76
- if (!attributeName) {
77
- return;
78
- }
79
- return this._attributeDescriptors.find((attributeDescriptor) => {
80
- return attributeDescriptor.attributeName === attributeName;
81
- });
82
- }
83
- static getAttributeValueDescriptor(name) {
84
- if (!name) {
85
- return;
86
- }
87
- return this._attributeValueDescriptors.find((attributeValueDescriptor) => {
88
- return attributeValueDescriptor.name === name;
89
- });
90
- }
91
- static getMetaItemDescriptor(urn) {
92
- if (!urn) {
93
- return;
94
- }
95
- return this._metaItemDescriptors.find((metaItemDescriptor) => {
96
- return metaItemDescriptor.urn === urn;
97
- });
98
- }
99
- static attributeValueDescriptorsMatch(attributeValueDescriptor1, attributeValueDescriptor2) {
100
- if (attributeValueDescriptor1 === attributeValueDescriptor2) {
101
- return true;
102
- }
103
- if (!attributeValueDescriptor1 || !attributeValueDescriptor2) {
104
- return false;
105
- }
106
- return attributeValueDescriptor1.name === attributeValueDescriptor2.name && attributeValueDescriptor1.valueType === attributeValueDescriptor2.valueType;
107
- }
108
- }
109
- AssetModelUtil._assetDescriptors = [];
110
- AssetModelUtil._attributeDescriptors = [];
111
- AssetModelUtil._attributeValueDescriptors = [];
112
- AssetModelUtil._metaItemDescriptors = [];
113
- export class Manager {
114
- constructor() {
115
- this._error = ORError.NONE;
116
- this._authenticated = false;
117
- this._ready = false;
118
- this._name = "";
119
- this._username = "";
120
- this._keycloak = null;
121
- this._roles = [];
122
- this._keycloakUpdateTokenInterval = undefined;
123
- this._managerVersion = "";
124
- this._listeners = [];
125
- }
126
- get username() {
127
- return this._username;
128
- }
129
- get error() {
130
- return this._error;
131
- }
132
- get authenticated() {
133
- return this._authenticated;
134
- }
135
- get initialised() {
136
- return this._config != null;
137
- }
138
- get ready() {
139
- return this._ready;
140
- }
141
- get config() {
142
- return this._config;
143
- }
144
- get roles() {
145
- return this._roles;
146
- }
147
- get managerVersion() {
148
- return this._managerVersion;
149
- }
150
- get isManagerAvailable() {
151
- return this._managerVersion && this._managerVersion !== "";
152
- }
153
- get isError() {
154
- return this._error != null && this._error !== ORError.NONE;
155
- }
156
- get connectionStatus() {
157
- return this._events && this._events.status;
158
- }
159
- get console() {
160
- return this._console;
161
- }
162
- get events() {
163
- return this._events;
164
- }
165
- get language() {
166
- return i18next.language;
167
- }
168
- getEventProvider() {
169
- return this.events;
170
- }
171
- static normaliseConfig(config) {
172
- const normalisedConfig = Object.assign({}, config);
173
- if (!normalisedConfig.managerUrl || normalisedConfig.managerUrl === "") {
174
- // Assume manager is running on same host as this code
175
- normalisedConfig.managerUrl = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port;
176
- }
177
- else {
178
- // Normalise by stripping any trailing slashes
179
- normalisedConfig.managerUrl = normalisedConfig.managerUrl.replace(/\/+$/, "");
180
- }
181
- if (!normalisedConfig.realm || normalisedConfig.realm === "") {
182
- // Assume master realm
183
- normalisedConfig.realm = "master";
184
- }
185
- if (normalisedConfig.auth === Auth.KEYCLOAK) {
186
- // Determine URL of keycloak server
187
- if (!normalisedConfig.keycloakUrl || normalisedConfig.keycloakUrl === "") {
188
- // Assume keycloak is running on same host as the manager
189
- normalisedConfig.keycloakUrl = normalisedConfig.managerUrl + "/auth";
190
- }
191
- else {
192
- // Normalise by stripping any trailing slashes
193
- normalisedConfig.keycloakUrl = normalisedConfig.keycloakUrl.replace(/\/+$/, "");
194
- }
195
- }
196
- if (normalisedConfig.consoleAutoEnable === undefined) {
197
- normalisedConfig.consoleAutoEnable = true;
198
- }
199
- if (!normalisedConfig.eventProviderType) {
200
- normalisedConfig.eventProviderType = EventProviderType.WEBSOCKET;
201
- }
202
- if (!normalisedConfig.pollingIntervalMillis || normalisedConfig.pollingIntervalMillis < 5000) {
203
- normalisedConfig.pollingIntervalMillis = 10000;
204
- }
205
- if (normalisedConfig.loadIcons === undefined) {
206
- normalisedConfig.loadIcons = true;
207
- }
208
- if (normalisedConfig.loadTranslations === undefined) {
209
- normalisedConfig.loadTranslations = ["or"];
210
- }
211
- if (normalisedConfig.translationsLoadPath === undefined) {
212
- normalisedConfig.translationsLoadPath = "locales/{{lng}}/{{ns}}.json";
213
- }
214
- if (normalisedConfig.loadDescriptors === undefined) {
215
- normalisedConfig.loadDescriptors = true;
216
- }
217
- return normalisedConfig;
218
- }
219
- isManagerSameOrigin() {
220
- if (!this.initialised) {
221
- return false;
222
- }
223
- const managerUrl = new URL(this._config.managerUrl);
224
- const windowUrl = window.location;
225
- return managerUrl.protocol === windowUrl.protocol
226
- && managerUrl.hostname === windowUrl.hostname
227
- && managerUrl.port === windowUrl.port;
228
- }
229
- addListener(callback) {
230
- const index = this._listeners.indexOf(callback);
231
- if (index < 0) {
232
- this._listeners.push(callback);
233
- }
234
- }
235
- removeListener(callback) {
236
- const index = this._listeners.indexOf(callback);
237
- if (index >= 0) {
238
- this._listeners.splice(index, 1);
239
- }
240
- }
241
- init(config) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- if (this._config) {
244
- console.log("Already initialised");
245
- }
246
- this._config = Manager.normaliseConfig(config);
247
- let success = yield this.doAuthInit();
248
- if (success) {
249
- success = yield this.doInit();
250
- }
251
- if (success) {
252
- success = this.doRestApiInit();
253
- }
254
- if (success) {
255
- success = yield this.doConsoleInit();
256
- }
257
- if (success) {
258
- success = yield this.doTranslateInit();
259
- }
260
- if (success) {
261
- success = yield this.doDescriptorsInit();
262
- }
263
- // TODO: Reinstate this once websocket supports anonymous connections
264
- // if (success) {
265
- // success = await this.doEventsSubscriptionInit();
266
- // }
267
- if (success) {
268
- this._ready = true;
269
- this._emitEvent(OREvent.READY);
270
- }
271
- return success;
272
- });
273
- }
274
- doInit() {
275
- return __awaiter(this, void 0, void 0, function* () {
276
- // Check manager exists by calling the info endpoint
277
- try {
278
- const json = yield new Promise((resolve, reject) => {
279
- const oReq = new XMLHttpRequest();
280
- oReq.addEventListener("load", () => {
281
- resolve(JSON.parse(oReq.responseText));
282
- });
283
- oReq.addEventListener("error", () => {
284
- reject(new Error("Failed to contact the manager"));
285
- });
286
- oReq.open("GET", this._config.managerUrl + "/api/master/info");
287
- oReq.send();
288
- });
289
- this._managerVersion = json && json.version ? json.version : "";
290
- // Async load material design icons if requested
291
- if (this._config.loadIcons) {
292
- const mdiIconSet = yield import(/* webpackChunkName: "mdi-icons" */ "@openremote/or-icon/dist/mdi-icons");
293
- IconSets.addIconSet("mdi", mdiIconSet.default);
294
- }
295
- return true;
296
- }
297
- catch (e) {
298
- // TODO: Implement auto retry?
299
- console.error("Failed to contact the manager", e);
300
- this._setError(ORError.MANAGER_FAILED_TO_LOAD);
301
- return false;
302
- }
303
- });
304
- }
305
- doTranslateInit() {
306
- return __awaiter(this, void 0, void 0, function* () {
307
- i18next.on("initialized", (options) => {
308
- this._emitEvent(OREvent.TRANSLATE_INIT);
309
- });
310
- i18next.on("languageChanged", () => {
311
- this._emitEvent(OREvent.TRANSLATE_LANGUAGE_CHANGED);
312
- });
313
- const initOptions = {
314
- lng: "en",
315
- fallbackLng: "en",
316
- defaultNS: "app",
317
- fallbackNS: "or",
318
- ns: this.config.loadTranslations,
319
- backend: {
320
- loadPath: (langs, namespaces) => {
321
- if (namespaces.length === 1 && namespaces[0] === "or") {
322
- return this.config.managerUrl + "/shared/locales/{{lng}}/{{ns}}.json";
323
- }
324
- if (this.config.translationsLoadPath) {
325
- return this.config.translationsLoadPath;
326
- }
327
- return "locales/{{lng}}/{{ns}}.json";
328
- }
329
- }
330
- };
331
- if (this.config.configureTranslationsOptions) {
332
- this.config.configureTranslationsOptions(initOptions);
333
- }
334
- try {
335
- yield i18next.use(i18nextXhr).init(initOptions);
336
- }
337
- catch (e) {
338
- console.error(e);
339
- return false;
340
- }
341
- return true;
342
- });
343
- }
344
- doDescriptorsInit() {
345
- return __awaiter(this, void 0, void 0, function* () {
346
- if (!this.config.loadDescriptors) {
347
- return true;
348
- }
349
- try {
350
- const assetDescriptorResponse = yield rest.api.AssetModelResource.getAssetDescriptors();
351
- const attributeDescriptorResponse = yield rest.api.AssetModelResource.getAttributeDescriptors();
352
- const attributeValueDescriptorResponse = yield rest.api.AssetModelResource.getAttributeValueDescriptors();
353
- const metaItemDescriptorResponse = yield rest.api.AssetModelResource.getMetaItemDescriptors();
354
- AssetModelUtil._assetDescriptors = assetDescriptorResponse.data;
355
- AssetModelUtil._attributeDescriptors = attributeDescriptorResponse.data;
356
- AssetModelUtil._attributeValueDescriptors = attributeValueDescriptorResponse.data;
357
- AssetModelUtil._metaItemDescriptors = metaItemDescriptorResponse.data;
358
- }
359
- catch (e) {
360
- console.error(e);
361
- return false;
362
- }
363
- return true;
364
- });
365
- }
366
- doAuthInit() {
367
- return __awaiter(this, void 0, void 0, function* () {
368
- let success = true;
369
- switch (this._config.auth) {
370
- case Auth.BASIC:
371
- // TODO: Implement Basic auth support
372
- if (this._config.credentials) {
373
- rest.setBasicAuth(this._config.credentials.username, this._config.credentials.password);
374
- }
375
- this._setError(ORError.AUTH_TYPE_UNSUPPORTED);
376
- success = false;
377
- break;
378
- case Auth.KEYCLOAK:
379
- success = yield this.loadAndInitialiseKeycloak();
380
- // Add interceptor to inject authorization header on each request
381
- rest.addRequestInterceptor((config) => {
382
- if (!config.headers.Authorization) {
383
- const token = this.getKeycloakToken();
384
- if (token) {
385
- config.headers.Authorization = "Bearer " + token;
386
- }
387
- }
388
- return config;
389
- });
390
- break;
391
- case Auth.NONE:
392
- // Nothing for us to do here
393
- break;
394
- default:
395
- this._setError(ORError.AUTH_TYPE_UNSUPPORTED);
396
- success = false;
397
- break;
398
- }
399
- return success;
400
- });
401
- }
402
- doRestApiInit() {
403
- rest.setTimeout(10000);
404
- rest.initialise(this.getApiBaseUrl());
405
- return true;
406
- }
407
- doEventsSubscriptionInit() {
408
- return __awaiter(this, void 0, void 0, function* () {
409
- let connected = false;
410
- switch (this._config.eventProviderType) {
411
- case EventProviderType.WEBSOCKET:
412
- this._events = new WebSocketEventProvider(this._config.managerUrl);
413
- this._events.subscribeStatusChange((status) => this._onEventProviderStatusChanged(status));
414
- connected = yield this._events.connect();
415
- break;
416
- case EventProviderType.POLLING:
417
- break;
418
- }
419
- if (!connected) {
420
- this._setError(ORError.EVENTS_CONNECTION_ERROR);
421
- }
422
- return connected;
423
- });
424
- }
425
- _onEventProviderStatusChanged(status) {
426
- switch (status) {
427
- case EventProviderStatus.DISCONNECTED:
428
- this._emitEvent(OREvent.EVENTS_DISCONNECTED);
429
- break;
430
- case EventProviderStatus.CONNECTED:
431
- this._emitEvent(OREvent.EVENTS_CONNECTED);
432
- break;
433
- case EventProviderStatus.CONNECTING:
434
- this._emitEvent(OREvent.EVENTS_CONNECTING);
435
- break;
436
- }
437
- }
438
- doConsoleInit() {
439
- return __awaiter(this, void 0, void 0, function* () {
440
- try {
441
- let orConsole = new Console(this._config.realm, this._config.consoleAutoEnable, () => {
442
- this._emitEvent(OREvent.CONSOLE_READY);
443
- });
444
- this._console = orConsole;
445
- yield orConsole.initialise();
446
- this._emitEvent(OREvent.CONSOLE_INIT);
447
- return true;
448
- }
449
- catch (e) {
450
- this._setError(ORError.CONSOLE_ERROR);
451
- return false;
452
- }
453
- });
454
- }
455
- logout(redirectUrl) {
456
- if (this._keycloak) {
457
- if (this.console.isMobile) {
458
- this.console.storeData("REFRESH_TOKEN", null);
459
- }
460
- const options = redirectUrl && redirectUrl !== "" ? { redirectUri: redirectUrl } : null;
461
- this._keycloak.logout(options);
462
- }
463
- }
464
- login(options) {
465
- if (!this.initialised) {
466
- return;
467
- }
468
- switch (this._config.auth) {
469
- case Auth.BASIC:
470
- if (options && options.credentials) {
471
- this._config.credentials = Object.assign({}, options.credentials);
472
- }
473
- const username = this._config.credentials ? this._config.credentials.username : null;
474
- const password = this._config.credentials ? this._config.credentials.password : null;
475
- if (username && password && username !== "" && password !== "") {
476
- // TODO: Perform some request to check basic auth credentials
477
- this._setAuthenticated(true);
478
- }
479
- break;
480
- case Auth.KEYCLOAK:
481
- if (this._keycloak) {
482
- const keycloakOptions = {};
483
- if (options && options.redirectUrl && options.redirectUrl !== "") {
484
- keycloakOptions.redirectUri = options.redirectUrl;
485
- }
486
- if (this.isMobile()) {
487
- keycloakOptions.scope = "offline_access";
488
- }
489
- this._keycloak.login(keycloakOptions);
490
- }
491
- break;
492
- case Auth.NONE:
493
- break;
494
- }
495
- }
496
- isSuperUser() {
497
- return this.hasRole("admin");
498
- }
499
- getApiBaseUrl() {
500
- let baseUrl = this._config.managerUrl;
501
- baseUrl += "/api/" + this._config.realm + "/";
502
- return baseUrl;
503
- }
504
- getAppName() {
505
- let pathArr = location.pathname.split('/');
506
- return pathArr.length >= 1 ? pathArr[1] : "";
507
- }
508
- hasRole(role) {
509
- return this._roles && this._roles.indexOf(role) >= 0;
510
- }
511
- getAuthorizationHeader() {
512
- if (this._keycloak && this.authenticated) {
513
- return "Bearer " + this._keycloak.token;
514
- }
515
- return undefined;
516
- }
517
- getKeycloakToken() {
518
- if (this._keycloak && this.authenticated) {
519
- return this._keycloak.token;
520
- }
521
- return undefined;
522
- }
523
- getRealm() {
524
- if (this._config) {
525
- return this._config.realm;
526
- }
527
- return undefined;
528
- }
529
- isMobile() {
530
- return this.console && this.console.isMobile;
531
- }
532
- _onAuthenticated() {
533
- // If native shell is enabled, we need an offline refresh token
534
- if (this.console && this.console.isMobile && this.config.auth === Auth.KEYCLOAK) {
535
- if (this._keycloak.refreshTokenParsed.typ === "Offline") {
536
- console.debug("Storing offline refresh token");
537
- this.console.storeData("REFRESH_TOKEN", this._keycloak.refreshToken);
538
- }
539
- else {
540
- this.login();
541
- }
542
- }
543
- }
544
- // NOTE: The below works with Keycloak 2.x JS API - They made breaking changes in newer versions
545
- // so this will need updating.
546
- loadAndInitialiseKeycloak() {
547
- return __awaiter(this, void 0, void 0, function* () {
548
- // Load the keycloak JS API
549
- const promise = new Promise((resolve, reject) => {
550
- // Load keycloak script from keycloak server
551
- const scriptElement = document.createElement("script");
552
- scriptElement.src = this._config.keycloakUrl + "/js/keycloak.js";
553
- scriptElement.onload = (e) => resolve(e);
554
- scriptElement.onerror = (e) => reject(e);
555
- document.querySelector("head").appendChild(scriptElement);
556
- });
557
- try {
558
- yield promise;
559
- // Should have Keycloak global var now
560
- if (!window.Keycloak) {
561
- this._setError(ORError.KEYCLOAK_FAILED_TO_LOAD);
562
- return false;
563
- }
564
- // Initialise keycloak
565
- this._keycloak = window.Keycloak({
566
- clientId: "openremote",
567
- realm: this._config.realm,
568
- url: this._config.keycloakUrl
569
- });
570
- this._keycloak.onAuthSuccess = () => {
571
- if (keycloakPromise) {
572
- keycloakPromise(true);
573
- }
574
- };
575
- this._keycloak.onAuthError = () => {
576
- this._setAuthenticated(false);
577
- };
578
- // There's a bug in some Keycloak versions which means the init promise doesn't resolve
579
- // so putting a check in place; wrap keycloak promise in proper ES6 promise
580
- let keycloakPromise = null;
581
- try {
582
- // Try to use a stored offline refresh token if defined
583
- const offlineToken = yield this._getNativeOfflineRefreshToken();
584
- const authenticated = yield new Promise(((resolve, reject) => {
585
- keycloakPromise = resolve;
586
- this._keycloak.init({
587
- checkLoginIframe: false,
588
- onLoad: this._config.autoLogin ? "login-required" : "check-sso",
589
- refreshToken: offlineToken
590
- }).success((auth) => {
591
- resolve(auth);
592
- }).error(() => {
593
- reject();
594
- });
595
- }));
596
- keycloakPromise = null;
597
- if (authenticated) {
598
- this._name = this._keycloak.tokenParsed.name;
599
- this._username = this._keycloak.tokenParsed.preferred_username;
600
- this._roles = this._keycloak.resourceAccess.openremote.roles;
601
- // Update the access token every 10s (note keycloak will only update if expiring within configured
602
- // time period.
603
- if (this._keycloakUpdateTokenInterval) {
604
- clearInterval(this._keycloakUpdateTokenInterval);
605
- delete this._keycloakUpdateTokenInterval;
606
- }
607
- this._keycloakUpdateTokenInterval = window.setInterval(() => {
608
- this.updateKeycloakAccessToken();
609
- }, 10000);
610
- this._onAuthenticated();
611
- }
612
- this._setAuthenticated(authenticated);
613
- return true;
614
- }
615
- catch (e) {
616
- console.error(e);
617
- keycloakPromise = null;
618
- this._setAuthenticated(false);
619
- return false;
620
- }
621
- }
622
- catch (error) {
623
- this._setError(ORError.KEYCLOAK_FAILED_TO_LOAD);
624
- return false;
625
- }
626
- });
627
- }
628
- updateKeycloakAccessToken() {
629
- // Access token must be good for X more seconds, should be half of Constants.ACCESS_TOKEN_LIFESPAN_SECONDS
630
- return new Promise(() => {
631
- this._keycloak.updateToken(30)
632
- .success((tokenRefreshed) => {
633
- // If refreshed from server, it means the refresh token was still good for another access token
634
- console.debug("Access token update success, refreshed from server: " + tokenRefreshed);
635
- return tokenRefreshed;
636
- })
637
- .error(() => {
638
- // Refresh token expired (either SSO max session duration or offline idle timeout), see
639
- // IDENTITY_SESSION_MAX_MINUTES and IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES server config
640
- console.info("Access token update failed, refresh token expired, login required");
641
- this._keycloak.clearToken();
642
- this._keycloak.login();
643
- });
644
- });
645
- }
646
- _getNativeOfflineRefreshToken() {
647
- return __awaiter(this, void 0, void 0, function* () {
648
- if (this.console && this.console.isMobile) {
649
- return yield this.console.retrieveData("REFRESH_TOKEN");
650
- }
651
- return null;
652
- });
653
- }
654
- _emitEvent(event) {
655
- window.setTimeout(() => {
656
- let listeners = this._listeners.slice();
657
- for (const listener of listeners) {
658
- listener(event);
659
- }
660
- }, 0);
661
- }
662
- _setError(error) {
663
- this._error = error;
664
- this._emitEvent(OREvent.ERROR);
665
- }
666
- // TODO: Remove events logic once websocket supports anonymous connections
667
- _setAuthenticated(authenticated) {
668
- this._authenticated = authenticated;
669
- if (authenticated) {
670
- if (!this._events) {
671
- this.doEventsSubscriptionInit();
672
- }
673
- }
674
- else {
675
- if (this._events) {
676
- this._events.disconnect();
677
- }
678
- this._events = undefined;
679
- }
680
- }
681
- }
682
- export default new Manager();
683
- //# sourceMappingURL=index.js.map