@samhammer/authentication-vue 1.0.5 → 1.0.6

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.
package/lib/AuthPlugin.js CHANGED
@@ -1,448 +1,449 @@
1
- import { __awaiter, __generator } from "tslib";
2
- import Keycloak from 'keycloak-js';
3
- import once from 'lodash/once';
4
- import { includes, throttle } from 'lodash';
5
- import LocalStorageUtils from './LocalStorageUtils';
6
- import { v4 as uuidv4 } from 'uuid';
7
- import { AuthEvents, AuthEventNames } from './AuthEvents';
8
- var AuthPlugin = /** @class */ (function () {
9
- function AuthPlugin() {
10
- var _this = this;
11
- Object.defineProperty(this, "authOptions", {
12
- enumerable: true,
13
- configurable: true,
14
- writable: true,
15
- value: void 0
16
- });
17
- Object.defineProperty(this, "keycloak", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: new KeycloakPlugin()
22
- });
23
- Object.defineProperty(this, "guest", {
24
- enumerable: true,
25
- configurable: true,
26
- writable: true,
27
- value: new GuestPlugin()
28
- });
29
- Object.defineProperty(this, "initOnce", {
30
- enumerable: true,
31
- configurable: true,
32
- writable: true,
33
- value: once(function (authOptions) { return __awaiter(_this, void 0, void 0, function () {
34
- var authenticated, _a;
35
- return __generator(this, function (_b) {
36
- switch (_b.label) {
37
- case 0:
38
- this.authOptions = authOptions;
39
- _a = this.guest.init(this.authOptions);
40
- if (_a) return [3 /*break*/, 2];
41
- return [4 /*yield*/, this.keycloak.init(this.authOptions)];
42
- case 1:
43
- _a = (_b.sent());
44
- _b.label = 2;
45
- case 2:
46
- authenticated = _a;
47
- if (authenticated) {
48
- AuthEvents.$emit(AuthEventNames.isAlreadyAuthenticated);
49
- }
50
- return [2 /*return*/, authenticated];
51
- }
52
- });
53
- }); })
54
- });
55
- }
56
- Object.defineProperty(AuthPlugin.prototype, "hasRole", {
57
- enumerable: false,
58
- configurable: true,
59
- writable: true,
60
- value: function (roleName) {
61
- if (!roleName) {
62
- return true;
63
- }
64
- return this.guest.hasRole(roleName) || this.keycloak.hasRole(this.authOptions, roleName);
65
- }
66
- });
67
- Object.defineProperty(AuthPlugin.prototype, "authenticated", {
68
- get: function () {
69
- return this.guest.authenticated || this.keycloak.authenticated;
70
- },
71
- enumerable: false,
72
- configurable: true
73
- });
74
- Object.defineProperty(AuthPlugin.prototype, "isGuest", {
75
- get: function () {
76
- return this.guest.authenticated;
77
- },
78
- enumerable: false,
79
- configurable: true
80
- });
81
- Object.defineProperty(AuthPlugin.prototype, "getToken", {
82
- enumerable: false,
83
- configurable: true,
84
- writable: true,
85
- value: function () {
86
- return __awaiter(this, void 0, void 0, function () {
87
- return __generator(this, function (_a) {
88
- return [2 /*return*/, this.guest.authenticated ? this.guest.getGuestId() : this.keycloak.getToken()];
89
- });
90
- });
91
- }
92
- });
93
- Object.defineProperty(AuthPlugin.prototype, "loginGuest", {
94
- enumerable: false,
95
- configurable: true,
96
- writable: true,
97
- value: function () {
98
- return __awaiter(this, void 0, void 0, function () {
99
- return __generator(this, function (_a) {
100
- this.guest.login(this.authOptions);
101
- return [2 /*return*/];
102
- });
103
- });
104
- }
105
- });
106
- Object.defineProperty(AuthPlugin.prototype, "login", {
107
- enumerable: false,
108
- configurable: true,
109
- writable: true,
110
- value: function (idp) {
111
- return __awaiter(this, void 0, void 0, function () {
112
- return __generator(this, function (_a) {
113
- switch (_a.label) {
114
- case 0: return [4 /*yield*/, this.keycloak.login(window.location.href, idp)];
115
- case 1:
116
- _a.sent();
117
- return [2 /*return*/];
118
- }
119
- });
120
- });
121
- }
122
- });
123
- Object.defineProperty(AuthPlugin.prototype, "logout", {
124
- enumerable: false,
125
- configurable: true,
126
- writable: true,
127
- value: function (redirectUri) {
128
- return __awaiter(this, void 0, void 0, function () {
129
- return __generator(this, function (_a) {
130
- switch (_a.label) {
131
- case 0:
132
- if (this.guest.authenticated) {
133
- this.guest.logout();
134
- return [2 /*return*/];
135
- }
136
- return [4 /*yield*/, this.keycloak.logout(redirectUri)];
137
- case 1:
138
- _a.sent();
139
- return [2 /*return*/];
140
- }
141
- });
142
- });
143
- }
144
- });
145
- return AuthPlugin;
146
- }());
147
- export { AuthPlugin };
148
- var KeycloakPlugin = /** @class */ (function () {
149
- function KeycloakPlugin() {
150
- var _this = this;
151
- Object.defineProperty(this, "keycloak", {
152
- enumerable: true,
153
- configurable: true,
154
- writable: true,
155
- value: null
156
- });
157
- Object.defineProperty(this, "accessTokenKey", {
158
- enumerable: true,
159
- configurable: true,
160
- writable: true,
161
- value: null
162
- });
163
- Object.defineProperty(this, "refreshTokenKey", {
164
- enumerable: true,
165
- configurable: true,
166
- writable: true,
167
- value: null
168
- });
169
- // this method ensures that we have only one refresh call even when it gets executed multiple times in paralell
170
- // NOTE: time for 'throttle wait' (ms) must be lower then 'token minValidity' (sec)
171
- Object.defineProperty(this, "refreshSingle", {
172
- enumerable: true,
173
- configurable: true,
174
- writable: true,
175
- value: throttle(function () { return _this.refresh(10); }, 5000, { trailing: false })
176
- });
177
- }
178
- Object.defineProperty(KeycloakPlugin.prototype, "hasRole", {
179
- enumerable: false,
180
- configurable: true,
181
- writable: true,
182
- value: function (authOptions, roleName) {
183
- return this.keycloak && this.keycloak.hasResourceRole(roleName, authOptions.apiClientId);
184
- }
185
- });
186
- Object.defineProperty(KeycloakPlugin.prototype, "authenticated", {
187
- get: function () {
188
- if (!this.keycloak) {
189
- return false;
190
- }
191
- return this.keycloak.authenticated;
192
- },
193
- enumerable: false,
194
- configurable: true
195
- });
196
- Object.defineProperty(KeycloakPlugin.prototype, "getToken", {
197
- enumerable: false,
198
- configurable: true,
199
- writable: true,
200
- value: function () {
201
- return __awaiter(this, void 0, void 0, function () {
202
- return __generator(this, function (_a) {
203
- switch (_a.label) {
204
- case 0:
205
- if (!this.keycloak) {
206
- return [2 /*return*/, null];
207
- }
208
- return [4 /*yield*/, this.refreshSingle()];
209
- case 1:
210
- _a.sent();
211
- return [2 /*return*/, this.keycloak.token];
212
- }
213
- });
214
- });
215
- }
216
- });
217
- Object.defineProperty(KeycloakPlugin.prototype, "init", {
218
- enumerable: false,
219
- configurable: true,
220
- writable: true,
221
- value: function (authOptions) {
222
- return __awaiter(this, void 0, void 0, function () {
223
- var authenticated, error_1;
224
- return __generator(this, function (_a) {
225
- switch (_a.label) {
226
- case 0:
227
- _a.trys.push([0, 2, , 3]);
228
- this.accessTokenKey = "".concat(authOptions.appClientId, "-accessToken");
229
- this.refreshTokenKey = "".concat(authOptions.appClientId, "-refreshToken");
230
- this.keycloak = Keycloak({
231
- url: authOptions.authUrl,
232
- realm: authOptions.realm,
233
- clientId: authOptions.appClientId
234
- });
235
- this.keycloak.onAuthSuccess = this.onAuthSuccess.bind(this);
236
- this.keycloak.onAuthRefreshSuccess = this.onAuthRefreshSuccess.bind(this);
237
- this.keycloak.onTokenExpired = this.onTokenExpired.bind(this);
238
- return [4 /*yield*/, this.keycloak.init({
239
- flow: 'standard',
240
- timeSkew: 0,
241
- enableLogging: false,
242
- refreshToken: LocalStorageUtils.getItem(this.refreshTokenKey),
243
- token: LocalStorageUtils.getItem(this.accessTokenKey)
244
- })];
245
- case 1:
246
- authenticated = _a.sent();
247
- if (authenticated) {
248
- console.debug('authenticated keycloak');
249
- }
250
- return [2 /*return*/, authenticated];
251
- case 2:
252
- error_1 = _a.sent();
253
- console.error('auth init failed', error_1);
254
- LocalStorageUtils.removeItem(this.refreshTokenKey);
255
- LocalStorageUtils.removeItem(this.accessTokenKey);
256
- return [2 /*return*/, false];
257
- case 3: return [2 /*return*/];
258
- }
259
- });
260
- });
261
- }
262
- });
263
- Object.defineProperty(KeycloakPlugin.prototype, "onAuthSuccess", {
264
- enumerable: false,
265
- configurable: true,
266
- writable: true,
267
- value: function () {
268
- LocalStorageUtils.setItem(this.refreshTokenKey, this.keycloak.refreshToken);
269
- LocalStorageUtils.setItem(this.accessTokenKey, this.keycloak.token);
270
- }
271
- });
272
- Object.defineProperty(KeycloakPlugin.prototype, "onAuthRefreshSuccess", {
273
- enumerable: false,
274
- configurable: true,
275
- writable: true,
276
- value: function () {
277
- LocalStorageUtils.setItem(this.refreshTokenKey, this.keycloak.refreshToken);
278
- LocalStorageUtils.setItem(this.accessTokenKey, this.keycloak.token);
279
- }
280
- });
281
- Object.defineProperty(KeycloakPlugin.prototype, "onTokenExpired", {
282
- enumerable: false,
283
- configurable: true,
284
- writable: true,
285
- value: function () {
286
- return __awaiter(this, void 0, void 0, function () {
287
- return __generator(this, function (_a) {
288
- // force refresh
289
- this.refresh(-1);
290
- return [2 /*return*/];
291
- });
292
- });
293
- }
294
- });
295
- Object.defineProperty(KeycloakPlugin.prototype, "login", {
296
- enumerable: false,
297
- configurable: true,
298
- writable: true,
299
- value: function (redirectUri, idp) {
300
- return __awaiter(this, void 0, void 0, function () {
301
- return __generator(this, function (_a) {
302
- switch (_a.label) {
303
- case 0:
304
- console.debug('login navigating to keycloak with returnUrl', redirectUri);
305
- return [4 /*yield*/, this.keycloak.login({ redirectUri: redirectUri, idpHint: idp })];
306
- case 1:
307
- _a.sent();
308
- return [2 /*return*/];
309
- }
310
- });
311
- });
312
- }
313
- });
314
- Object.defineProperty(KeycloakPlugin.prototype, "logout", {
315
- enumerable: false,
316
- configurable: true,
317
- writable: true,
318
- value: function (redirectUri) {
319
- return __awaiter(this, void 0, void 0, function () {
320
- return __generator(this, function (_a) {
321
- switch (_a.label) {
322
- case 0:
323
- console.debug('logout navigating to keycloak with returnUrl', redirectUri);
324
- LocalStorageUtils.removeItem(this.refreshTokenKey);
325
- LocalStorageUtils.removeItem(this.accessTokenKey);
326
- return [4 /*yield*/, this.keycloak.logout({ redirectUri: redirectUri })];
327
- case 1:
328
- _a.sent();
329
- return [2 /*return*/];
330
- }
331
- });
332
- });
333
- }
334
- });
335
- Object.defineProperty(KeycloakPlugin.prototype, "refresh", {
336
- enumerable: false,
337
- configurable: true,
338
- writable: true,
339
- value: function (minValidity) {
340
- return __awaiter(this, void 0, void 0, function () {
341
- var successful, error_2;
342
- return __generator(this, function (_a) {
343
- switch (_a.label) {
344
- case 0:
345
- _a.trys.push([0, 2, , 3]);
346
- return [4 /*yield*/, this.keycloak.updateToken(minValidity)];
347
- case 1:
348
- successful = _a.sent();
349
- if (successful) {
350
- console.debug('token refreshed');
351
- }
352
- return [3 /*break*/, 3];
353
- case 2:
354
- error_2 = _a.sent();
355
- console.error('token refresh failed', error_2 || 'token may be empty');
356
- return [3 /*break*/, 3];
357
- case 3: return [2 /*return*/];
358
- }
359
- });
360
- });
361
- }
362
- });
363
- return KeycloakPlugin;
364
- }());
365
- var GuestPlugin = /** @class */ (function () {
366
- function GuestPlugin() {
367
- Object.defineProperty(this, "guestKey", {
368
- enumerable: true,
369
- configurable: true,
370
- writable: true,
371
- value: null
372
- });
373
- Object.defineProperty(this, "guestId", {
374
- enumerable: true,
375
- configurable: true,
376
- writable: true,
377
- value: null
378
- });
379
- Object.defineProperty(this, "guestRoles", {
380
- enumerable: true,
381
- configurable: true,
382
- writable: true,
383
- value: []
384
- });
385
- }
386
- Object.defineProperty(GuestPlugin.prototype, "hasRole", {
387
- enumerable: false,
388
- configurable: true,
389
- writable: true,
390
- value: function (roleName) {
391
- return includes(this.guestRoles, roleName);
392
- }
393
- });
394
- Object.defineProperty(GuestPlugin.prototype, "authenticated", {
395
- get: function () {
396
- return this.guestId ? true : false;
397
- },
398
- enumerable: false,
399
- configurable: true
400
- });
401
- Object.defineProperty(GuestPlugin.prototype, "getGuestId", {
402
- enumerable: false,
403
- configurable: true,
404
- writable: true,
405
- value: function () {
406
- return this.guestId;
407
- }
408
- });
409
- Object.defineProperty(GuestPlugin.prototype, "init", {
410
- enumerable: false,
411
- configurable: true,
412
- writable: true,
413
- value: function (authOptions) {
414
- this.guestKey = "".concat(authOptions.appClientId, "-guestId");
415
- this.guestId = LocalStorageUtils.getItem(this.guestKey);
416
- if (this.authenticated) {
417
- this.guestRoles = ['User'];
418
- console.debug('authenticated guest');
419
- }
420
- return this.authenticated;
421
- }
422
- });
423
- Object.defineProperty(GuestPlugin.prototype, "login", {
424
- enumerable: false,
425
- configurable: true,
426
- writable: true,
427
- value: function (authOptions) {
428
- console.debug('login guest');
429
- this.guestId = uuidv4();
430
- LocalStorageUtils.setItem(this.guestKey, this.guestId);
431
- this.init(authOptions);
432
- }
433
- });
434
- Object.defineProperty(GuestPlugin.prototype, "logout", {
435
- enumerable: false,
436
- configurable: true,
437
- writable: true,
438
- value: function () {
439
- console.debug('logout guest');
440
- LocalStorageUtils.removeItem(this.guestKey);
441
- this.guestId = null;
442
- this.guestRoles = null;
443
- window.location.reload();
444
- }
445
- });
446
- return GuestPlugin;
447
- }());
1
+ import { __awaiter, __generator } from "tslib";
2
+ import Keycloak from 'keycloak-js';
3
+ import once from 'lodash/once';
4
+ import { includes, throttle } from 'lodash';
5
+ import LocalStorageUtils from './LocalStorageUtils';
6
+ import { v4 as uuidv4 } from 'uuid';
7
+ import { AuthEvents, AuthEventNames } from './AuthEvents';
8
+ var AuthPlugin = /** @class */ (function () {
9
+ function AuthPlugin() {
10
+ var _this = this;
11
+ Object.defineProperty(this, "authOptions", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "keycloak", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: new KeycloakPlugin()
22
+ });
23
+ Object.defineProperty(this, "guest", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: new GuestPlugin()
28
+ });
29
+ Object.defineProperty(this, "initOnce", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: once(function (authOptions) { return __awaiter(_this, void 0, void 0, function () {
34
+ var authenticated, _a;
35
+ return __generator(this, function (_b) {
36
+ switch (_b.label) {
37
+ case 0:
38
+ this.authOptions = authOptions;
39
+ _a = this.guest.init(this.authOptions);
40
+ if (_a) return [3 /*break*/, 2];
41
+ return [4 /*yield*/, this.keycloak.init(this.authOptions)];
42
+ case 1:
43
+ _a = (_b.sent());
44
+ _b.label = 2;
45
+ case 2:
46
+ authenticated = _a;
47
+ if (authenticated) {
48
+ AuthEvents.$emit(AuthEventNames.isAlreadyAuthenticated);
49
+ }
50
+ return [2 /*return*/, authenticated];
51
+ }
52
+ });
53
+ }); })
54
+ });
55
+ }
56
+ Object.defineProperty(AuthPlugin.prototype, "hasRole", {
57
+ enumerable: false,
58
+ configurable: true,
59
+ writable: true,
60
+ value: function (roleName) {
61
+ if (!roleName) {
62
+ return true;
63
+ }
64
+ return this.guest.hasRole(roleName) || this.keycloak.hasRole(this.authOptions, roleName);
65
+ }
66
+ });
67
+ Object.defineProperty(AuthPlugin.prototype, "authenticated", {
68
+ get: function () {
69
+ return this.guest.authenticated || this.keycloak.authenticated;
70
+ },
71
+ enumerable: false,
72
+ configurable: true
73
+ });
74
+ Object.defineProperty(AuthPlugin.prototype, "isGuest", {
75
+ get: function () {
76
+ return this.guest.authenticated;
77
+ },
78
+ enumerable: false,
79
+ configurable: true
80
+ });
81
+ Object.defineProperty(AuthPlugin.prototype, "getToken", {
82
+ enumerable: false,
83
+ configurable: true,
84
+ writable: true,
85
+ value: function () {
86
+ return __awaiter(this, void 0, void 0, function () {
87
+ return __generator(this, function (_a) {
88
+ return [2 /*return*/, this.guest.authenticated ? this.guest.getGuestId() : this.keycloak.getToken()];
89
+ });
90
+ });
91
+ }
92
+ });
93
+ Object.defineProperty(AuthPlugin.prototype, "loginGuest", {
94
+ enumerable: false,
95
+ configurable: true,
96
+ writable: true,
97
+ value: function () {
98
+ return __awaiter(this, void 0, void 0, function () {
99
+ return __generator(this, function (_a) {
100
+ this.guest.login(this.authOptions);
101
+ return [2 /*return*/];
102
+ });
103
+ });
104
+ }
105
+ });
106
+ Object.defineProperty(AuthPlugin.prototype, "login", {
107
+ enumerable: false,
108
+ configurable: true,
109
+ writable: true,
110
+ value: function (idp) {
111
+ return __awaiter(this, void 0, void 0, function () {
112
+ return __generator(this, function (_a) {
113
+ switch (_a.label) {
114
+ case 0: return [4 /*yield*/, this.keycloak.login(window.location.href, idp)];
115
+ case 1:
116
+ _a.sent();
117
+ return [2 /*return*/];
118
+ }
119
+ });
120
+ });
121
+ }
122
+ });
123
+ Object.defineProperty(AuthPlugin.prototype, "logout", {
124
+ enumerable: false,
125
+ configurable: true,
126
+ writable: true,
127
+ value: function (redirectUri) {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ return __generator(this, function (_a) {
130
+ switch (_a.label) {
131
+ case 0:
132
+ if (this.guest.authenticated) {
133
+ this.guest.logout();
134
+ return [2 /*return*/];
135
+ }
136
+ return [4 /*yield*/, this.keycloak.logout(redirectUri)];
137
+ case 1:
138
+ _a.sent();
139
+ return [2 /*return*/];
140
+ }
141
+ });
142
+ });
143
+ }
144
+ });
145
+ return AuthPlugin;
146
+ }());
147
+ export { AuthPlugin };
148
+ var KeycloakPlugin = /** @class */ (function () {
149
+ function KeycloakPlugin() {
150
+ var _this = this;
151
+ Object.defineProperty(this, "keycloak", {
152
+ enumerable: true,
153
+ configurable: true,
154
+ writable: true,
155
+ value: null
156
+ });
157
+ Object.defineProperty(this, "accessTokenKey", {
158
+ enumerable: true,
159
+ configurable: true,
160
+ writable: true,
161
+ value: null
162
+ });
163
+ Object.defineProperty(this, "refreshTokenKey", {
164
+ enumerable: true,
165
+ configurable: true,
166
+ writable: true,
167
+ value: null
168
+ });
169
+ // this method ensures that we have only one refresh call even when it gets executed multiple times in paralell
170
+ // NOTE: time for 'throttle wait' (ms) must be lower then 'token minValidity' (sec)
171
+ Object.defineProperty(this, "refreshSingle", {
172
+ enumerable: true,
173
+ configurable: true,
174
+ writable: true,
175
+ value: throttle(function () { return _this.refresh(10); }, 5000, { trailing: false })
176
+ });
177
+ }
178
+ Object.defineProperty(KeycloakPlugin.prototype, "hasRole", {
179
+ enumerable: false,
180
+ configurable: true,
181
+ writable: true,
182
+ value: function (authOptions, roleName) {
183
+ return this.keycloak && this.keycloak.hasResourceRole(roleName, authOptions.apiClientId);
184
+ }
185
+ });
186
+ Object.defineProperty(KeycloakPlugin.prototype, "authenticated", {
187
+ get: function () {
188
+ if (!this.keycloak) {
189
+ return false;
190
+ }
191
+ return this.keycloak.authenticated;
192
+ },
193
+ enumerable: false,
194
+ configurable: true
195
+ });
196
+ Object.defineProperty(KeycloakPlugin.prototype, "getToken", {
197
+ enumerable: false,
198
+ configurable: true,
199
+ writable: true,
200
+ value: function () {
201
+ return __awaiter(this, void 0, void 0, function () {
202
+ return __generator(this, function (_a) {
203
+ switch (_a.label) {
204
+ case 0:
205
+ if (!this.keycloak) {
206
+ return [2 /*return*/, null];
207
+ }
208
+ return [4 /*yield*/, this.refreshSingle()];
209
+ case 1:
210
+ _a.sent();
211
+ return [2 /*return*/, this.keycloak.token];
212
+ }
213
+ });
214
+ });
215
+ }
216
+ });
217
+ Object.defineProperty(KeycloakPlugin.prototype, "init", {
218
+ enumerable: false,
219
+ configurable: true,
220
+ writable: true,
221
+ value: function (authOptions) {
222
+ return __awaiter(this, void 0, void 0, function () {
223
+ var authenticated, error_1;
224
+ return __generator(this, function (_a) {
225
+ switch (_a.label) {
226
+ case 0:
227
+ _a.trys.push([0, 2, , 3]);
228
+ this.accessTokenKey = "".concat(authOptions.appClientId, "-accessToken");
229
+ this.refreshTokenKey = "".concat(authOptions.appClientId, "-refreshToken");
230
+ this.keycloak = Keycloak({
231
+ url: authOptions.authUrl,
232
+ realm: authOptions.realm,
233
+ clientId: authOptions.appClientId
234
+ });
235
+ this.keycloak.onAuthSuccess = this.onAuthSuccess.bind(this);
236
+ this.keycloak.onAuthRefreshSuccess = this.onAuthRefreshSuccess.bind(this);
237
+ this.keycloak.onTokenExpired = this.onTokenExpired.bind(this);
238
+ return [4 /*yield*/, this.keycloak.init({
239
+ flow: 'standard',
240
+ timeSkew: 0,
241
+ enableLogging: false,
242
+ refreshToken: LocalStorageUtils.getItem(this.refreshTokenKey),
243
+ token: LocalStorageUtils.getItem(this.accessTokenKey)
244
+ })];
245
+ case 1:
246
+ authenticated = _a.sent();
247
+ if (authenticated) {
248
+ console.debug('authenticated keycloak');
249
+ }
250
+ return [2 /*return*/, authenticated];
251
+ case 2:
252
+ error_1 = _a.sent();
253
+ console.error('auth init failed', error_1);
254
+ LocalStorageUtils.removeItem(this.refreshTokenKey);
255
+ LocalStorageUtils.removeItem(this.accessTokenKey);
256
+ return [2 /*return*/, false];
257
+ case 3: return [2 /*return*/];
258
+ }
259
+ });
260
+ });
261
+ }
262
+ });
263
+ Object.defineProperty(KeycloakPlugin.prototype, "onAuthSuccess", {
264
+ enumerable: false,
265
+ configurable: true,
266
+ writable: true,
267
+ value: function () {
268
+ LocalStorageUtils.setItem(this.refreshTokenKey, this.keycloak.refreshToken);
269
+ LocalStorageUtils.setItem(this.accessTokenKey, this.keycloak.token);
270
+ }
271
+ });
272
+ Object.defineProperty(KeycloakPlugin.prototype, "onAuthRefreshSuccess", {
273
+ enumerable: false,
274
+ configurable: true,
275
+ writable: true,
276
+ value: function () {
277
+ LocalStorageUtils.setItem(this.refreshTokenKey, this.keycloak.refreshToken);
278
+ LocalStorageUtils.setItem(this.accessTokenKey, this.keycloak.token);
279
+ }
280
+ });
281
+ Object.defineProperty(KeycloakPlugin.prototype, "onTokenExpired", {
282
+ enumerable: false,
283
+ configurable: true,
284
+ writable: true,
285
+ value: function () {
286
+ return __awaiter(this, void 0, void 0, function () {
287
+ return __generator(this, function (_a) {
288
+ // force refresh
289
+ this.refresh(-1);
290
+ return [2 /*return*/];
291
+ });
292
+ });
293
+ }
294
+ });
295
+ Object.defineProperty(KeycloakPlugin.prototype, "login", {
296
+ enumerable: false,
297
+ configurable: true,
298
+ writable: true,
299
+ value: function (redirectUri, idp) {
300
+ return __awaiter(this, void 0, void 0, function () {
301
+ return __generator(this, function (_a) {
302
+ switch (_a.label) {
303
+ case 0:
304
+ console.debug('login navigating to keycloak with returnUrl', redirectUri);
305
+ return [4 /*yield*/, this.keycloak.login({ redirectUri: redirectUri, idpHint: idp })];
306
+ case 1:
307
+ _a.sent();
308
+ return [2 /*return*/];
309
+ }
310
+ });
311
+ });
312
+ }
313
+ });
314
+ Object.defineProperty(KeycloakPlugin.prototype, "logout", {
315
+ enumerable: false,
316
+ configurable: true,
317
+ writable: true,
318
+ value: function (redirectUri) {
319
+ return __awaiter(this, void 0, void 0, function () {
320
+ return __generator(this, function (_a) {
321
+ switch (_a.label) {
322
+ case 0:
323
+ console.debug('logout navigating to keycloak with returnUrl', redirectUri);
324
+ LocalStorageUtils.removeItem(this.refreshTokenKey);
325
+ LocalStorageUtils.removeItem(this.accessTokenKey);
326
+ return [4 /*yield*/, this.keycloak.logout({ redirectUri: redirectUri })];
327
+ case 1:
328
+ _a.sent();
329
+ return [2 /*return*/];
330
+ }
331
+ });
332
+ });
333
+ }
334
+ });
335
+ Object.defineProperty(KeycloakPlugin.prototype, "refresh", {
336
+ enumerable: false,
337
+ configurable: true,
338
+ writable: true,
339
+ value: function (minValidity) {
340
+ return __awaiter(this, void 0, void 0, function () {
341
+ var successful, error_2;
342
+ return __generator(this, function (_a) {
343
+ switch (_a.label) {
344
+ case 0:
345
+ _a.trys.push([0, 2, , 3]);
346
+ return [4 /*yield*/, this.keycloak.updateToken(minValidity)];
347
+ case 1:
348
+ successful = _a.sent();
349
+ if (successful) {
350
+ console.debug('token refreshed');
351
+ }
352
+ return [3 /*break*/, 3];
353
+ case 2:
354
+ error_2 = _a.sent();
355
+ console.error('token refresh failed', error_2 || 'token may be empty');
356
+ return [3 /*break*/, 3];
357
+ case 3: return [2 /*return*/];
358
+ }
359
+ });
360
+ });
361
+ }
362
+ });
363
+ return KeycloakPlugin;
364
+ }());
365
+ var GuestPlugin = /** @class */ (function () {
366
+ function GuestPlugin() {
367
+ Object.defineProperty(this, "guestKey", {
368
+ enumerable: true,
369
+ configurable: true,
370
+ writable: true,
371
+ value: null
372
+ });
373
+ Object.defineProperty(this, "guestId", {
374
+ enumerable: true,
375
+ configurable: true,
376
+ writable: true,
377
+ value: null
378
+ });
379
+ Object.defineProperty(this, "guestRoles", {
380
+ enumerable: true,
381
+ configurable: true,
382
+ writable: true,
383
+ value: []
384
+ });
385
+ }
386
+ Object.defineProperty(GuestPlugin.prototype, "hasRole", {
387
+ enumerable: false,
388
+ configurable: true,
389
+ writable: true,
390
+ value: function (roleName) {
391
+ return includes(this.guestRoles, roleName);
392
+ }
393
+ });
394
+ Object.defineProperty(GuestPlugin.prototype, "authenticated", {
395
+ get: function () {
396
+ return this.guestId ? true : false;
397
+ },
398
+ enumerable: false,
399
+ configurable: true
400
+ });
401
+ Object.defineProperty(GuestPlugin.prototype, "getGuestId", {
402
+ enumerable: false,
403
+ configurable: true,
404
+ writable: true,
405
+ value: function () {
406
+ return this.guestId;
407
+ }
408
+ });
409
+ Object.defineProperty(GuestPlugin.prototype, "init", {
410
+ enumerable: false,
411
+ configurable: true,
412
+ writable: true,
413
+ value: function (authOptions) {
414
+ this.guestKey = "".concat(authOptions.appClientId, "-guestId");
415
+ this.guestId = LocalStorageUtils.getItem(this.guestKey);
416
+ if (this.authenticated) {
417
+ this.guestRoles = ['User'];
418
+ console.debug('authenticated guest');
419
+ }
420
+ return this.authenticated;
421
+ }
422
+ });
423
+ Object.defineProperty(GuestPlugin.prototype, "login", {
424
+ enumerable: false,
425
+ configurable: true,
426
+ writable: true,
427
+ value: function (authOptions) {
428
+ console.debug('login guest');
429
+ this.guestId = uuidv4();
430
+ LocalStorageUtils.setItem(this.guestKey, this.guestId);
431
+ this.init(authOptions);
432
+ AuthEvents.$emit(AuthEventNames.isGuestAuthenticated);
433
+ }
434
+ });
435
+ Object.defineProperty(GuestPlugin.prototype, "logout", {
436
+ enumerable: false,
437
+ configurable: true,
438
+ writable: true,
439
+ value: function () {
440
+ console.debug('logout guest');
441
+ LocalStorageUtils.removeItem(this.guestKey);
442
+ this.guestId = null;
443
+ this.guestRoles = null;
444
+ window.location.reload();
445
+ }
446
+ });
447
+ return GuestPlugin;
448
+ }());
448
449
  //# sourceMappingURL=AuthPlugin.js.map