@samhammer/authentication-vue 1.0.0

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.
@@ -0,0 +1,8 @@
1
+ import Vue from 'vue';
2
+ export declare enum AuthEventNames {
3
+ isAlreadyAuthenticated = "isAlreadyAuthenticated",
4
+ loginRequired = "loginRequired",
5
+ permissionDenied = "permissionDenied"
6
+ }
7
+ declare const _default: import("vue/types/vue").CombinedVueInstance<Vue, object, object, object, Record<never, any>>;
8
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import Vue from 'vue';
2
+ export var AuthEventNames;
3
+ (function (AuthEventNames) {
4
+ AuthEventNames["isAlreadyAuthenticated"] = "isAlreadyAuthenticated";
5
+ AuthEventNames["loginRequired"] = "loginRequired";
6
+ AuthEventNames["permissionDenied"] = "permissionDenied";
7
+ })(AuthEventNames || (AuthEventNames = {}));
8
+ export default new Vue();
9
+ //# sourceMappingURL=AuthEvents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthEvents.js","sourceRoot":"","sources":["../src/AuthEvents.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,CAAN,IAAY,cAIX;AAJD,WAAY,cAAc;IACtB,mEAAiD,CAAA;IACjD,iDAA+B,CAAA;IAC/B,uDAAqC,CAAA;AACzC,CAAC,EAJW,cAAc,KAAd,cAAc,QAIzB;AAED,eAAe,IAAI,GAAG,EAAE,CAAC"}
@@ -0,0 +1,6 @@
1
+ export interface AuthOptions {
2
+ apiClientId: string;
3
+ appClientId: string;
4
+ authUrl: string;
5
+ realm: string;
6
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=AuthOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthOptions.js","sourceRoot":"","sources":["../src/AuthOptions.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import { AuthOptions } from './AuthOptions';
2
+ declare class AuthPlugin {
3
+ private authOptions;
4
+ private keycloak;
5
+ private guest;
6
+ hasRole(roleName: string): boolean;
7
+ get authenticated(): boolean;
8
+ get isGuest(): boolean;
9
+ getToken(): Promise<string>;
10
+ initOnce: (authOptions: AuthOptions) => Promise<boolean>;
11
+ loginGuest(): Promise<void>;
12
+ login(idp: string): Promise<void>;
13
+ logout(): Promise<void>;
14
+ }
15
+ declare const _default: AuthPlugin;
16
+ export default _default;
@@ -0,0 +1,448 @@
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 () {
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(window.location.href)];
137
+ case 1:
138
+ _a.sent();
139
+ return [2 /*return*/];
140
+ }
141
+ });
142
+ });
143
+ }
144
+ });
145
+ return AuthPlugin;
146
+ }());
147
+ var KeycloakPlugin = /** @class */ (function () {
148
+ function KeycloakPlugin() {
149
+ var _this = this;
150
+ Object.defineProperty(this, "keycloak", {
151
+ enumerable: true,
152
+ configurable: true,
153
+ writable: true,
154
+ value: null
155
+ });
156
+ Object.defineProperty(this, "accessTokenKey", {
157
+ enumerable: true,
158
+ configurable: true,
159
+ writable: true,
160
+ value: null
161
+ });
162
+ Object.defineProperty(this, "refreshTokenKey", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: null
167
+ });
168
+ // this method ensures that we have only one refresh call even when it gets executed multiple times in paralell
169
+ // NOTE: time for 'throttle wait' (ms) must be lower then 'token minValidity' (sec)
170
+ Object.defineProperty(this, "refreshSingle", {
171
+ enumerable: true,
172
+ configurable: true,
173
+ writable: true,
174
+ value: throttle(function () { return _this.refresh(10); }, 5000, { trailing: false })
175
+ });
176
+ }
177
+ Object.defineProperty(KeycloakPlugin.prototype, "hasRole", {
178
+ enumerable: false,
179
+ configurable: true,
180
+ writable: true,
181
+ value: function (authOptions, roleName) {
182
+ return this.keycloak && this.keycloak.hasResourceRole(roleName, authOptions.apiClientId);
183
+ }
184
+ });
185
+ Object.defineProperty(KeycloakPlugin.prototype, "authenticated", {
186
+ get: function () {
187
+ if (!this.keycloak) {
188
+ return false;
189
+ }
190
+ return this.keycloak.authenticated;
191
+ },
192
+ enumerable: false,
193
+ configurable: true
194
+ });
195
+ Object.defineProperty(KeycloakPlugin.prototype, "getToken", {
196
+ enumerable: false,
197
+ configurable: true,
198
+ writable: true,
199
+ value: function () {
200
+ return __awaiter(this, void 0, void 0, function () {
201
+ return __generator(this, function (_a) {
202
+ switch (_a.label) {
203
+ case 0:
204
+ if (!this.keycloak) {
205
+ return [2 /*return*/, null];
206
+ }
207
+ return [4 /*yield*/, this.refreshSingle()];
208
+ case 1:
209
+ _a.sent();
210
+ return [2 /*return*/, this.keycloak.token];
211
+ }
212
+ });
213
+ });
214
+ }
215
+ });
216
+ Object.defineProperty(KeycloakPlugin.prototype, "init", {
217
+ enumerable: false,
218
+ configurable: true,
219
+ writable: true,
220
+ value: function (authOptions) {
221
+ return __awaiter(this, void 0, void 0, function () {
222
+ var authenticated, error_1;
223
+ return __generator(this, function (_a) {
224
+ switch (_a.label) {
225
+ case 0:
226
+ _a.trys.push([0, 2, , 3]);
227
+ this.accessTokenKey = "".concat(authOptions.appClientId, "-accessToken");
228
+ this.refreshTokenKey = "".concat(authOptions.appClientId, "-refreshToken");
229
+ this.keycloak = Keycloak({
230
+ url: authOptions.authUrl,
231
+ realm: authOptions.realm,
232
+ clientId: authOptions.appClientId
233
+ });
234
+ this.keycloak.onAuthSuccess = this.onAuthSuccess.bind(this);
235
+ this.keycloak.onAuthRefreshSuccess = this.onAuthRefreshSuccess.bind(this);
236
+ this.keycloak.onTokenExpired = this.onTokenExpired.bind(this);
237
+ return [4 /*yield*/, this.keycloak.init({
238
+ flow: 'standard',
239
+ timeSkew: 0,
240
+ enableLogging: false,
241
+ refreshToken: LocalStorageUtils.getItem(this.refreshTokenKey),
242
+ token: LocalStorageUtils.getItem(this.accessTokenKey)
243
+ })];
244
+ case 1:
245
+ authenticated = _a.sent();
246
+ if (authenticated) {
247
+ console.debug('authenticated keycloak');
248
+ }
249
+ return [2 /*return*/, authenticated];
250
+ case 2:
251
+ error_1 = _a.sent();
252
+ console.error('auth init failed', error_1);
253
+ LocalStorageUtils.removeItem(this.refreshTokenKey);
254
+ LocalStorageUtils.removeItem(this.accessTokenKey);
255
+ return [2 /*return*/, false];
256
+ case 3: return [2 /*return*/];
257
+ }
258
+ });
259
+ });
260
+ }
261
+ });
262
+ Object.defineProperty(KeycloakPlugin.prototype, "onAuthSuccess", {
263
+ enumerable: false,
264
+ configurable: true,
265
+ writable: true,
266
+ value: function () {
267
+ LocalStorageUtils.setItem(this.refreshTokenKey, this.keycloak.refreshToken);
268
+ LocalStorageUtils.setItem(this.accessTokenKey, this.keycloak.token);
269
+ }
270
+ });
271
+ Object.defineProperty(KeycloakPlugin.prototype, "onAuthRefreshSuccess", {
272
+ enumerable: false,
273
+ configurable: true,
274
+ writable: true,
275
+ value: function () {
276
+ LocalStorageUtils.setItem(this.refreshTokenKey, this.keycloak.refreshToken);
277
+ LocalStorageUtils.setItem(this.accessTokenKey, this.keycloak.token);
278
+ }
279
+ });
280
+ Object.defineProperty(KeycloakPlugin.prototype, "onTokenExpired", {
281
+ enumerable: false,
282
+ configurable: true,
283
+ writable: true,
284
+ value: function () {
285
+ return __awaiter(this, void 0, void 0, function () {
286
+ return __generator(this, function (_a) {
287
+ // force refresh
288
+ this.refresh(-1);
289
+ return [2 /*return*/];
290
+ });
291
+ });
292
+ }
293
+ });
294
+ Object.defineProperty(KeycloakPlugin.prototype, "login", {
295
+ enumerable: false,
296
+ configurable: true,
297
+ writable: true,
298
+ value: function (redirectUri, idp) {
299
+ return __awaiter(this, void 0, void 0, function () {
300
+ return __generator(this, function (_a) {
301
+ switch (_a.label) {
302
+ case 0:
303
+ console.debug('login navigating to keycloak with returnUrl', redirectUri);
304
+ return [4 /*yield*/, this.keycloak.login({ redirectUri: redirectUri, idpHint: idp })];
305
+ case 1:
306
+ _a.sent();
307
+ return [2 /*return*/];
308
+ }
309
+ });
310
+ });
311
+ }
312
+ });
313
+ Object.defineProperty(KeycloakPlugin.prototype, "logout", {
314
+ enumerable: false,
315
+ configurable: true,
316
+ writable: true,
317
+ value: function (redirectUri) {
318
+ return __awaiter(this, void 0, void 0, function () {
319
+ return __generator(this, function (_a) {
320
+ switch (_a.label) {
321
+ case 0:
322
+ console.debug('logout navigating to keycloak with returnUrl', redirectUri);
323
+ LocalStorageUtils.removeItem(this.refreshTokenKey);
324
+ LocalStorageUtils.removeItem(this.accessTokenKey);
325
+ return [4 /*yield*/, this.keycloak.logout({ redirectUri: redirectUri })];
326
+ case 1:
327
+ _a.sent();
328
+ return [2 /*return*/];
329
+ }
330
+ });
331
+ });
332
+ }
333
+ });
334
+ Object.defineProperty(KeycloakPlugin.prototype, "refresh", {
335
+ enumerable: false,
336
+ configurable: true,
337
+ writable: true,
338
+ value: function (minValidity) {
339
+ return __awaiter(this, void 0, void 0, function () {
340
+ var successful, error_2;
341
+ return __generator(this, function (_a) {
342
+ switch (_a.label) {
343
+ case 0:
344
+ _a.trys.push([0, 2, , 3]);
345
+ return [4 /*yield*/, this.keycloak.updateToken(minValidity)];
346
+ case 1:
347
+ successful = _a.sent();
348
+ if (successful) {
349
+ console.debug('token refreshed');
350
+ }
351
+ return [3 /*break*/, 3];
352
+ case 2:
353
+ error_2 = _a.sent();
354
+ console.error('token refresh failed', error_2 || 'token may be empty');
355
+ return [3 /*break*/, 3];
356
+ case 3: return [2 /*return*/];
357
+ }
358
+ });
359
+ });
360
+ }
361
+ });
362
+ return KeycloakPlugin;
363
+ }());
364
+ var GuestPlugin = /** @class */ (function () {
365
+ function GuestPlugin() {
366
+ Object.defineProperty(this, "guestKey", {
367
+ enumerable: true,
368
+ configurable: true,
369
+ writable: true,
370
+ value: null
371
+ });
372
+ Object.defineProperty(this, "guestId", {
373
+ enumerable: true,
374
+ configurable: true,
375
+ writable: true,
376
+ value: null
377
+ });
378
+ Object.defineProperty(this, "guestRoles", {
379
+ enumerable: true,
380
+ configurable: true,
381
+ writable: true,
382
+ value: []
383
+ });
384
+ }
385
+ Object.defineProperty(GuestPlugin.prototype, "hasRole", {
386
+ enumerable: false,
387
+ configurable: true,
388
+ writable: true,
389
+ value: function (roleName) {
390
+ return includes(this.guestRoles, roleName);
391
+ }
392
+ });
393
+ Object.defineProperty(GuestPlugin.prototype, "authenticated", {
394
+ get: function () {
395
+ return this.guestId ? true : false;
396
+ },
397
+ enumerable: false,
398
+ configurable: true
399
+ });
400
+ Object.defineProperty(GuestPlugin.prototype, "getGuestId", {
401
+ enumerable: false,
402
+ configurable: true,
403
+ writable: true,
404
+ value: function () {
405
+ return this.guestId;
406
+ }
407
+ });
408
+ Object.defineProperty(GuestPlugin.prototype, "init", {
409
+ enumerable: false,
410
+ configurable: true,
411
+ writable: true,
412
+ value: function (authOptions) {
413
+ this.guestKey = "".concat(authOptions.appClientId, "-guestId");
414
+ this.guestId = LocalStorageUtils.getItem(this.guestKey);
415
+ if (this.authenticated) {
416
+ this.guestRoles = ['User'];
417
+ console.debug('authenticated guest');
418
+ }
419
+ return this.authenticated;
420
+ }
421
+ });
422
+ Object.defineProperty(GuestPlugin.prototype, "login", {
423
+ enumerable: false,
424
+ configurable: true,
425
+ writable: true,
426
+ value: function (authOptions) {
427
+ console.debug('login guest');
428
+ this.guestId = uuidv4();
429
+ LocalStorageUtils.setItem(this.guestKey, this.guestId);
430
+ this.init(authOptions);
431
+ }
432
+ });
433
+ Object.defineProperty(GuestPlugin.prototype, "logout", {
434
+ enumerable: false,
435
+ configurable: true,
436
+ writable: true,
437
+ value: function () {
438
+ console.debug('logout guest');
439
+ LocalStorageUtils.removeItem(this.guestKey);
440
+ this.guestId = null;
441
+ this.guestRoles = null;
442
+ window.location.reload();
443
+ }
444
+ });
445
+ return GuestPlugin;
446
+ }());
447
+ export default new AuthPlugin();
448
+ //# sourceMappingURL=AuthPlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthPlugin.js","sourceRoot":"","sources":["../src/AuthPlugin.ts"],"names":[],"mappings":";AAAA,OAAO,QAAQ,MAAM,aAAa,CAAC;AACnC,OAAO,IAAI,MAAM,aAAa,CA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C;;;;;;eAED,UAAY,WAAwB;YAChC,IAAI,CAAC,QAAQ,GAAG,UAAG,WAAW,CAAC,WAAW,aAAU,CAAC;YACrD,IAAI,CAAC,OAAO,GAAG,iBAAiB,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAExD,IAAI,IAAI,CAAC,aAAa,EAAE;gBACpB,IAAI,CAAC,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC;gBAC3B,OAAO,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;aACxC;YAED,OAAO,IAAI,CAAC,aAAa,CAAC;QAC9B,CAAC;;;;;;eAED,UAAa,WAAwB;YACjC,OAAO,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;YAC7B,IAAI,CAAC,OAAO,GAAG,MAAM,EAAE,CAAC;YACxB,iBAAiB,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YACvD,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC3B,CAAC;;;;;;eAED;YACI,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;YAC9B,iBAAiB,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC5C,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;QAC7B,CAAC;;IACL,kBAAC;AAAD,CAAC,AA5CD,IA4CC;AAED,eAAe,IAAI,UAAU,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { AxiosInstance } from 'axios';
2
+ export declare function addAuthTokenInterceptor(axiosInstance: AxiosInstance): number;
3
+ export declare function addAuthErrorInterceptor(axiosInstance: AxiosInstance): number;
@@ -0,0 +1,45 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import AuthEvents, { AuthEventNames } from './AuthEvents';
3
+ import AuthPlugin from './AuthPlugin';
4
+ export function addAuthTokenInterceptor(axiosInstance) {
5
+ var _this = this;
6
+ var onRequest = function (requestConfig) { return __awaiter(_this, void 0, void 0, function () {
7
+ var token;
8
+ return __generator(this, function (_a) {
9
+ switch (_a.label) {
10
+ case 0: return [4 /*yield*/, AuthPlugin.getToken()];
11
+ case 1:
12
+ token = _a.sent();
13
+ if (token) {
14
+ if (AuthPlugin.isGuest) {
15
+ requestConfig.headers['guestid'] = token;
16
+ }
17
+ else {
18
+ requestConfig.headers['Authorization'] = "Bearer ".concat(token);
19
+ }
20
+ }
21
+ return [2 /*return*/, requestConfig];
22
+ }
23
+ });
24
+ }); };
25
+ return axiosInstance.interceptors.request.use(onRequest, null);
26
+ }
27
+ export function addAuthErrorInterceptor(axiosInstance) {
28
+ var _this = this;
29
+ var onError = function (error) { return __awaiter(_this, void 0, void 0, function () {
30
+ var _a, _b, _c, _d;
31
+ return __generator(this, function (_e) {
32
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
33
+ console.warn('axios api request requires authentication', (_b = error.config) === null || _b === void 0 ? void 0 : _b.url, error.message);
34
+ AuthEvents.$emit(AuthEventNames.loginRequired);
35
+ }
36
+ else if (((_c = error.response) === null || _c === void 0 ? void 0 : _c.status) === 403) {
37
+ console.warn('axios api request requires permission', (_d = error.config) === null || _d === void 0 ? void 0 : _d.url, error.message);
38
+ AuthEvents.$emit(AuthEventNames.permissionDenied);
39
+ }
40
+ throw error;
41
+ });
42
+ }); };
43
+ return axiosInstance.interceptors.response.use(null, onError);
44
+ }
45
+ //# sourceMappingURL=AxiosRequestInterceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AxiosRequestInterceptor.js","sourceRoot":"","sources":["../src/AxiosRequestInterceptor.ts"],"names":[],"mappings":";AACA,OAAO,UAAU,EAAE,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,UAAU,MAAM,cAAc,CAAC;AAEtC,MAAM,UAAU,uB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CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAClE,CAAC"}
@@ -0,0 +1,9 @@
1
+ declare class LocalStorageUtils {
2
+ setItem(key: string, value: string): void;
3
+ setItems(key: string, values: string[]): void;
4
+ getItem(key: string): string;
5
+ getItems(key: string): string[];
6
+ removeItem(key: string): void;
7
+ }
8
+ declare const _default: LocalStorageUtils;
9
+ export default _default;
@@ -0,0 +1,55 @@
1
+ import _ from 'lodash';
2
+ var LocalStorageUtils = /** @class */ (function () {
3
+ function LocalStorageUtils() {
4
+ }
5
+ Object.defineProperty(LocalStorageUtils.prototype, "setItem", {
6
+ enumerable: false,
7
+ configurable: true,
8
+ writable: true,
9
+ value: function (key, value) {
10
+ if (value) {
11
+ localStorage.setItem(key, value);
12
+ }
13
+ else {
14
+ localStorage.removeItem(key);
15
+ }
16
+ }
17
+ });
18
+ Object.defineProperty(LocalStorageUtils.prototype, "setItems", {
19
+ enumerable: false,
20
+ configurable: true,
21
+ writable: true,
22
+ value: function (key, values) {
23
+ this.setItem(key, _.join(values, ','));
24
+ }
25
+ });
26
+ Object.defineProperty(LocalStorageUtils.prototype, "getItem", {
27
+ enumerable: false,
28
+ configurable: true,
29
+ writable: true,
30
+ value: function (key) {
31
+ var text = localStorage.getItem(key);
32
+ return typeof text === 'undefined' ? null : text;
33
+ }
34
+ });
35
+ Object.defineProperty(LocalStorageUtils.prototype, "getItems", {
36
+ enumerable: false,
37
+ configurable: true,
38
+ writable: true,
39
+ value: function (key) {
40
+ var text = this.getItem(key);
41
+ return text !== null ? _.split(text, ',') : null;
42
+ }
43
+ });
44
+ Object.defineProperty(LocalStorageUtils.prototype, "removeItem", {
45
+ enumerable: false,
46
+ configurable: true,
47
+ writable: true,
48
+ value: function (key) {
49
+ localStorage.removeItem(key);
50
+ }
51
+ });
52
+ return LocalStorageUtils;
53
+ }());
54
+ export default new LocalStorageUtils();
55
+ //# sourceMappingURL=LocalStorageUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalStorageUtils.js","sourceRoot":"","sources":["../src/LocalStorageUtils.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAC;AAEvB;IAAA;IA0BA,CAAC;;;;;eAzBG,UAAe,GAAW,EAAE,KAAa;YACrC,IAAI,KAAK,EAAE;gBACP,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;aACpC;iBAAM;gBACH,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;aAChC;QACL,CAAC;;;;;;eAED,UAAgB,GAAW,EAAE,MAAgB;YACzC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;QAC3C,CAAC;;;;;;eAED,UAAe,GAAW;YACtB,IAAM,IAAI,GAAW,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC/C,OAAO,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;QACrD,CAAC;;;;;;eAED,UAAgB,GAAW;YACvB,IAAM,IAAI,GAAW,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACvC,OAAO,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACrD,CAAC;;;;;;eAED,UAAkB,GAAW;YACzB,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACjC,CAAC;;IACL,wBAAC;AAAD,CAAC,AA1BD,IA0BC;AAED,eAAe,IAAI,iBAAiB,EAAE,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './AuthEvents';
2
+ export * from './AuthOptions';
3
+ export * from './AuthPlugin';
4
+ export * from './AxiosRequestInterceptor';
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './AuthEvents';
2
+ export * from './AuthOptions';
3
+ export * from './AuthPlugin';
4
+ export * from './AxiosRequestInterceptor';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,2BAA2B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@samhammer/authentication-vue",
3
+ "version": "1.0.0",
4
+ "description": "Keycloak authentication for VueJS projects",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib/**/*"
9
+ ],
10
+ "repository": "https://github.com/SamhammerAG/Samhammer.Authentication.Vue",
11
+ "author": "SamhammerAG (https://www.samhammer.de/)",
12
+ "bugs": {
13
+ "url": "https://github.com/SamhammerAG/Samhammer.Authentication.Vue/issues"
14
+ },
15
+ "homepage": "https://github.com/SamhammerAG/Samhammer.Authentication.Vue#readme",
16
+ "license": "MIT",
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "format": "prettier --write --parser typescript ./src",
20
+ "lint": "eslint . --fix",
21
+ "prepare": "yarn run build",
22
+ "prepublishOnly": "yarn run lint",
23
+ "version": "git add .",
24
+ "postversion": "git push && git push --tags"
25
+ },
26
+ "devDependencies": {
27
+ "@types/lodash": "^4.14.178",
28
+ "@types/uuid": "^8.3.3",
29
+ "@typescript-eslint/eslint-plugin": "^5.9.0",
30
+ "@typescript-eslint/parser": "^5.9.0",
31
+ "eslint": "^8.6.0",
32
+ "eslint-config-prettier": "^8.3.0",
33
+ "eslint-plugin-import": "^2.25.4",
34
+ "eslint-plugin-prettier": "^4.0.0",
35
+ "prettier": "^2.5.1",
36
+ "tslib": "^2.3.1",
37
+ "typescript": "^4.5.4"
38
+ },
39
+ "dependencies": {
40
+ "axios": "^0.24.0",
41
+ "keycloak-js": "^16.1.0",
42
+ "lodash": "^4.17.21",
43
+ "uuid": "^8.3.2",
44
+ "vue": "^2.6.14"
45
+ }
46
+ }