@samhammer/authentication-vue 2.0.7 → 2.2.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.
- package/lib/AuthOptions.d.ts +2 -0
- package/lib/AuthPlugin.js +30 -27
- package/lib/Store.d.ts +15 -0
- package/lib/Store.js +35 -0
- package/lib/StoreProvider.d.ts +7 -0
- package/lib/StoreProvider.js +8 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +7 -2
- package/lib/LocalStorageUtils.d.ts +0 -8
- package/lib/LocalStorageUtils.js +0 -22
package/lib/AuthOptions.d.ts
CHANGED
|
@@ -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
|
-
|
|
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;
|
|
@@ -38,7 +41,7 @@ export class AuthPlugin {
|
|
|
38
41
|
async loginGuest() {
|
|
39
42
|
if (!this.authOptions)
|
|
40
43
|
throw new Error("Init has to be called first");
|
|
41
|
-
this.guest.login(this.authOptions);
|
|
44
|
+
await this.guest.login(this.authOptions);
|
|
42
45
|
}
|
|
43
46
|
async login(idp) {
|
|
44
47
|
await this.keycloak.login(window.location.href, idp);
|
|
@@ -59,7 +62,7 @@ export class AuthPlugin {
|
|
|
59
62
|
}
|
|
60
63
|
async logout(redirectUri) {
|
|
61
64
|
if (this.guest.authenticated) {
|
|
62
|
-
this.guest.logout();
|
|
65
|
+
await this.guest.logout();
|
|
63
66
|
return;
|
|
64
67
|
}
|
|
65
68
|
await this.keycloak.logout(redirectUri);
|
|
@@ -106,14 +109,14 @@ class KeycloakPlugin {
|
|
|
106
109
|
refreshTokenKey: `${authOptions.appClientId}-refreshToken`,
|
|
107
110
|
idTokenKey: `${authOptions.appClientId}-idToken`
|
|
108
111
|
};
|
|
109
|
-
keycloak.onAuthSuccess = this.
|
|
110
|
-
keycloak.onAuthRefreshSuccess = 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.
|
|
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:
|
|
128
|
-
token:
|
|
129
|
-
idToken:
|
|
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) {
|
|
@@ -136,7 +139,7 @@ class KeycloakPlugin {
|
|
|
136
139
|
}
|
|
137
140
|
async onTokenExpired() {
|
|
138
141
|
// force refresh
|
|
139
|
-
this.refresh(-1);
|
|
142
|
+
await this.refresh(-1);
|
|
140
143
|
}
|
|
141
144
|
async login(redirectUri, idp) {
|
|
142
145
|
console.debug("login navigating to keycloak with returnUrl", redirectUri);
|
|
@@ -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.
|
|
164
|
+
await this.clearStorage();
|
|
162
165
|
if (!this.pluginState)
|
|
163
166
|
return;
|
|
164
167
|
await this.pluginState.keycloak.logout({ redirectUri });
|
|
165
168
|
}
|
|
166
|
-
|
|
169
|
+
async setStorage() {
|
|
167
170
|
if (!this.pluginState)
|
|
168
171
|
return;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
176
|
+
async clearStorage() {
|
|
174
177
|
if (!this.pluginState)
|
|
175
178
|
return;
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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 =
|
|
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
|
-
|
|
234
|
-
this.init(authOptions);
|
|
236
|
+
await StoreProvider.store.setItem(this.pluginState.guestKey, this.pluginState.guestId);
|
|
237
|
+
await 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
|
-
|
|
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
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samhammer/authentication-vue",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Keycloak authentication for VueJS projects",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@rushstack/eslint-patch": "^1.2.0",
|
|
28
|
+
"@types/chrome": "^0.0.251",
|
|
28
29
|
"@types/lodash": "^4.14.191",
|
|
29
30
|
"@types/node": "^18.15.1",
|
|
30
31
|
"@types/uuid": "^9.0.1",
|
|
@@ -38,10 +39,14 @@
|
|
|
38
39
|
"typescript": "^4.7.4"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"axios": "^1.
|
|
42
|
+
"axios": "^1.6.7",
|
|
42
43
|
"emittery": "^1.0.1",
|
|
43
44
|
"keycloak-js": "^20.0.3",
|
|
44
45
|
"lodash": "^4.17.21",
|
|
45
46
|
"uuid": "^9.0.0"
|
|
47
|
+
},
|
|
48
|
+
"volta": {
|
|
49
|
+
"node": "18.19.0",
|
|
50
|
+
"yarn": "1.22.21"
|
|
46
51
|
}
|
|
47
52
|
}
|
|
@@ -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;
|
package/lib/LocalStorageUtils.js
DELETED
|
@@ -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();
|