@samhammer/authentication-vue 2.0.6 → 2.1.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.
@@ -1,4 +1,5 @@
1
1
  import type { KeycloakInitOptions } from "keycloak-js";
2
+ import type { Store } from "./Store";
2
3
  export interface AuthOptions {
3
4
  keycloakInitOptions?: KeycloakInitOptions;
4
5
  apiClientId?: string;
@@ -7,4 +8,5 @@ export interface AuthOptions {
7
8
  authUrl?: string;
8
9
  realm?: string;
9
10
  guestRoles?: string[];
11
+ store?: Store;
10
12
  }
package/lib/AuthPlugin.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import Keycloak from "keycloak-js";
2
2
  import once from "lodash/once";
3
3
  import { includes, throttle } from "lodash";
4
- import LocalStorageUtils from "./LocalStorageUtils";
5
4
  import { v4 as uuidv4 } from "uuid";
6
5
  import { AuthEvents, AuthEventNames } from "./AuthEvents";
6
+ import StoreProvider from "./StoreProvider";
7
7
  export class AuthPlugin {
8
8
  authOptions;
9
9
  keycloak = new KeycloakPlugin();
@@ -25,7 +25,10 @@ export class AuthPlugin {
25
25
  }
26
26
  initOnce = once(async (authOptions) => {
27
27
  this.authOptions = authOptions;
28
- const guestAuthenticated = this.guest.init(this.authOptions);
28
+ if (authOptions.store) {
29
+ StoreProvider.setStore(authOptions.store);
30
+ }
31
+ const guestAuthenticated = await this.guest.init(this.authOptions);
29
32
  if (guestAuthenticated) {
30
33
  AuthEvents.emit(AuthEventNames.isGuestAuthenticated);
31
34
  return;
@@ -106,14 +109,14 @@ class KeycloakPlugin {
106
109
  refreshTokenKey: `${authOptions.appClientId}-refreshToken`,
107
110
  idTokenKey: `${authOptions.appClientId}-idToken`
108
111
  };
109
- keycloak.onAuthSuccess = this.setLocalStorage.bind(this);
110
- keycloak.onAuthRefreshSuccess = this.setLocalStorage.bind(this);
112
+ keycloak.onAuthSuccess = this.setStorage.bind(this);
113
+ keycloak.onAuthRefreshSuccess = this.setStorage.bind(this);
111
114
  keycloak.onTokenExpired = this.onTokenExpired.bind(this);
112
115
  return this.initKeycloak();
113
116
  }
114
117
  catch (error) {
115
118
  console.error("auth init failed", error);
116
- this.clearLocalStorage();
119
+ await this.clearStorage();
117
120
  return false;
118
121
  }
119
122
  }
@@ -124,9 +127,9 @@ class KeycloakPlugin {
124
127
  flow: "standard",
125
128
  timeSkew: 0,
126
129
  enableLogging: false,
127
- refreshToken: LocalStorageUtils.getItem(this.pluginState.refreshTokenKey),
128
- token: LocalStorageUtils.getItem(this.pluginState.accessTokenKey),
129
- idToken: LocalStorageUtils.getItem(this.pluginState.idTokenKey),
130
+ refreshToken: await StoreProvider.store.getItem(this.pluginState.refreshTokenKey),
131
+ token: await StoreProvider.store.getItem(this.pluginState.accessTokenKey),
132
+ idToken: await StoreProvider.store.getItem(this.pluginState.idTokenKey),
130
133
  ...this.pluginState.authOptios.keycloakInitOptions
131
134
  });
132
135
  if (authenticated) {
@@ -158,24 +161,24 @@ class KeycloakPlugin {
158
161
  }
159
162
  async logout(redirectUri) {
160
163
  console.debug("logout navigating to keycloak with returnUrl", redirectUri);
161
- this.clearLocalStorage();
164
+ await this.clearStorage();
162
165
  if (!this.pluginState)
163
166
  return;
164
167
  await this.pluginState.keycloak.logout({ redirectUri });
165
168
  }
166
- setLocalStorage() {
169
+ async setStorage() {
167
170
  if (!this.pluginState)
168
171
  return;
169
- LocalStorageUtils.setItem(this.pluginState.refreshTokenKey, this.pluginState.keycloak.refreshToken ?? "");
170
- LocalStorageUtils.setItem(this.pluginState.accessTokenKey, this.pluginState.keycloak.token ?? "");
171
- LocalStorageUtils.setItem(this.pluginState.idTokenKey, this.pluginState.keycloak.idToken ?? "");
172
+ await StoreProvider.store.setItem(this.pluginState.refreshTokenKey, this.pluginState.keycloak.refreshToken ?? "");
173
+ await StoreProvider.store.setItem(this.pluginState.accessTokenKey, this.pluginState.keycloak.token ?? "");
174
+ await StoreProvider.store.setItem(this.pluginState.idTokenKey, this.pluginState.keycloak.idToken ?? "");
172
175
  }
173
- clearLocalStorage() {
176
+ async clearStorage() {
174
177
  if (!this.pluginState)
175
178
  return;
176
- LocalStorageUtils.removeItem(this.pluginState.refreshTokenKey);
177
- LocalStorageUtils.removeItem(this.pluginState.accessTokenKey);
178
- LocalStorageUtils.removeItem(this.pluginState.idTokenKey);
179
+ await StoreProvider.store.removeItem(this.pluginState.refreshTokenKey);
180
+ await StoreProvider.store.removeItem(this.pluginState.accessTokenKey);
181
+ await StoreProvider.store.removeItem(this.pluginState.idTokenKey);
179
182
  }
180
183
  async refresh(minValidity) {
181
184
  try {
@@ -207,12 +210,12 @@ class GuestPlugin {
207
210
  getGuestId() {
208
211
  return this.pluginState?.guestId ?? "";
209
212
  }
210
- init(authOptions) {
213
+ async init(authOptions) {
211
214
  if (!authOptions.guestClientId) {
212
215
  return false;
213
216
  }
214
217
  const guestKey = `${authOptions.guestClientId}-guestId`;
215
- const guestId = LocalStorageUtils.getItem(guestKey);
218
+ const guestId = await StoreProvider.store.getItem(guestKey);
216
219
  let guestRoles = [];
217
220
  if (guestId) {
218
221
  guestRoles = authOptions.guestRoles || ["User"];
@@ -225,20 +228,20 @@ class GuestPlugin {
225
228
  };
226
229
  return this.authenticated;
227
230
  }
228
- login(authOptions) {
231
+ async login(authOptions) {
229
232
  console.debug("login guest");
230
233
  if (!this.pluginState)
231
234
  throw new Error("Init has to be called first");
232
235
  this.pluginState.guestId = uuidv4();
233
- LocalStorageUtils.setItem(this.pluginState.guestKey, this.pluginState.guestId);
236
+ await StoreProvider.store.setItem(this.pluginState.guestKey, this.pluginState.guestId);
234
237
  this.init(authOptions);
235
238
  AuthEvents.emit(AuthEventNames.isGuestAuthenticated);
236
239
  }
237
- logout() {
240
+ async logout() {
238
241
  console.debug("logout guest");
239
242
  if (!this.pluginState)
240
243
  return;
241
- LocalStorageUtils.removeItem(this.pluginState.guestKey);
244
+ await StoreProvider.store.removeItem(this.pluginState.guestKey);
242
245
  this.pluginState.guestId = "";
243
246
  this.pluginState.guestRoles = [];
244
247
  window.location.reload();
package/lib/Store.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export interface Store {
2
+ setItem(key: string, value: string): Promise<void>;
3
+ getItem(key: string): Promise<string>;
4
+ removeItem(key: string): Promise<void>;
5
+ }
6
+ export declare class LocalStore implements Store {
7
+ setItem(key: string, value: string): Promise<void>;
8
+ getItem(key: string): Promise<string>;
9
+ removeItem(key: string): Promise<void>;
10
+ }
11
+ export declare class ChromeStore implements Store {
12
+ setItem(key: string, value: string): Promise<void>;
13
+ getItem(key: string): Promise<string>;
14
+ removeItem(key: string): Promise<void>;
15
+ }
package/lib/Store.js ADDED
@@ -0,0 +1,35 @@
1
+ export class LocalStore {
2
+ async setItem(key, value) {
3
+ if (value) {
4
+ localStorage.setItem(key, value);
5
+ }
6
+ else {
7
+ localStorage.removeItem(key);
8
+ }
9
+ }
10
+ async getItem(key) {
11
+ const text = localStorage.getItem(key) ?? "";
12
+ return text === "undefined" ? "" : text;
13
+ }
14
+ async removeItem(key) {
15
+ localStorage.removeItem(key);
16
+ }
17
+ }
18
+ export class ChromeStore {
19
+ async setItem(key, value) {
20
+ if (value) {
21
+ await chrome.storage.local.set({ [key]: value });
22
+ }
23
+ else {
24
+ await chrome.storage.local.remove([key]);
25
+ }
26
+ }
27
+ async getItem(key) {
28
+ const result = await chrome.storage.local.get([key]);
29
+ const text = result[key];
30
+ return text ? text : "";
31
+ }
32
+ async removeItem(key) {
33
+ chrome.storage.local.remove([key]);
34
+ }
35
+ }
@@ -0,0 +1,7 @@
1
+ import type { Store } from "./Store";
2
+ declare class StoreProvider {
3
+ store: Store;
4
+ setStore(newStore: Store): void;
5
+ }
6
+ declare const _default: StoreProvider;
7
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import { LocalStore } from "./Store";
2
+ class StoreProvider {
3
+ store = new LocalStore();
4
+ setStore(newStore) {
5
+ this.store = newStore;
6
+ }
7
+ }
8
+ export default new StoreProvider();
package/lib/index.d.ts CHANGED
@@ -2,5 +2,6 @@ import { AuthPlugin } from "./AuthPlugin";
2
2
  export * from "./AuthEvents";
3
3
  export * from "./AuthOptions";
4
4
  export * from "./AxiosAuthInterceptor";
5
+ export * from "./Store";
5
6
  declare const _default: AuthPlugin;
6
7
  export default _default;
package/lib/index.js CHANGED
@@ -2,4 +2,5 @@ import { AuthPlugin } from "./AuthPlugin";
2
2
  export * from "./AuthEvents";
3
3
  export * from "./AuthOptions";
4
4
  export * from "./AxiosAuthInterceptor";
5
+ export * from "./Store";
5
6
  export default new AuthPlugin();
package/package.json CHANGED
@@ -1,47 +1,48 @@
1
- {
2
- "name": "@samhammer/authentication-vue",
3
- "version": "2.0.6",
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
- "@rushstack/eslint-patch": "^1.2.0",
28
- "@types/lodash": "^4.14.191",
29
- "@types/node": "^18.15.1",
30
- "@types/uuid": "^9.0.1",
31
- "@typescript-eslint/eslint-plugin": "^5.54.1",
32
- "@typescript-eslint/parser": "^5.54.1",
33
- "eslint": "^8.36.0",
34
- "eslint-config-prettier": "^8.7.0",
35
- "eslint-plugin-import": "^2.27.5",
36
- "eslint-plugin-prettier": "^4.2.1",
37
- "prettier": "^2.7.1",
38
- "typescript": "^4.7.4"
39
- },
40
- "dependencies": {
41
- "axios": "^1.3.4",
42
- "emittery": "^1.0.1",
43
- "keycloak-js": "^20.0.3",
44
- "lodash": "^4.17.21",
45
- "uuid": "^9.0.0"
46
- }
47
- }
1
+ {
2
+ "name": "@samhammer/authentication-vue",
3
+ "version": "2.1.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
+ "@rushstack/eslint-patch": "^1.2.0",
28
+ "@types/chrome": "^0.0.251",
29
+ "@types/lodash": "^4.14.191",
30
+ "@types/node": "^18.15.1",
31
+ "@types/uuid": "^9.0.1",
32
+ "@typescript-eslint/eslint-plugin": "^5.54.1",
33
+ "@typescript-eslint/parser": "^5.54.1",
34
+ "eslint": "^8.36.0",
35
+ "eslint-config-prettier": "^8.7.0",
36
+ "eslint-plugin-import": "^2.27.5",
37
+ "eslint-plugin-prettier": "^4.2.1",
38
+ "prettier": "^2.7.1",
39
+ "typescript": "^4.7.4"
40
+ },
41
+ "dependencies": {
42
+ "axios": "^1.3.4",
43
+ "emittery": "^1.0.1",
44
+ "keycloak-js": "^20.0.3",
45
+ "lodash": "^4.17.21",
46
+ "uuid": "^9.0.0"
47
+ }
48
+ }
@@ -1,8 +0,0 @@
1
- declare class LocalStorageUtils {
2
- setItem(key: string, value: string): void;
3
- setItems(key: string, values: string[]): void;
4
- getItem(key: string): string;
5
- removeItem(key: string): void;
6
- }
7
- declare const _default: LocalStorageUtils;
8
- export default _default;
@@ -1,22 +0,0 @@
1
- import _ from "lodash";
2
- class LocalStorageUtils {
3
- setItem(key, value) {
4
- if (value) {
5
- localStorage.setItem(key, value);
6
- }
7
- else {
8
- localStorage.removeItem(key);
9
- }
10
- }
11
- setItems(key, values) {
12
- this.setItem(key, _.join(values, ","));
13
- }
14
- getItem(key) {
15
- const text = localStorage.getItem(key) ?? "";
16
- return text === "undefined" ? "" : text;
17
- }
18
- removeItem(key) {
19
- localStorage.removeItem(key);
20
- }
21
- }
22
- export default new LocalStorageUtils();