@samhammer/authentication-vue 2.4.2 → 2.5.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/AuthLogger.d.ts +9 -0
- package/lib/AuthLogger.js +16 -0
- package/lib/AuthPlugin.js +19 -15
- package/lib/AxiosAuthInterceptor.js +3 -2
- package/package.json +3 -5
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare class AuthLogger {
|
|
2
|
+
private prefix;
|
|
3
|
+
debug(...messages: unknown[]): void;
|
|
4
|
+
log(...messages: unknown[]): void;
|
|
5
|
+
warn(...messages: unknown[]): void;
|
|
6
|
+
error(...messages: unknown[]): void;
|
|
7
|
+
}
|
|
8
|
+
declare const _default: AuthLogger;
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class AuthLogger {
|
|
2
|
+
prefix = "[SAGAUTH]";
|
|
3
|
+
debug(...messages) {
|
|
4
|
+
console.debug(this.prefix, ...messages);
|
|
5
|
+
}
|
|
6
|
+
log(...messages) {
|
|
7
|
+
console.log(this.prefix, ...messages);
|
|
8
|
+
}
|
|
9
|
+
warn(...messages) {
|
|
10
|
+
console.warn(this.prefix, ...messages);
|
|
11
|
+
}
|
|
12
|
+
error(...messages) {
|
|
13
|
+
console.error(this.prefix, ...messages);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export default new AuthLogger();
|
package/lib/AuthPlugin.js
CHANGED
|
@@ -4,6 +4,7 @@ import { includes, throttle } from "lodash";
|
|
|
4
4
|
import { v4 as uuidv4 } from "uuid";
|
|
5
5
|
import { AuthEvents, AuthEventNames } from "./AuthEvents";
|
|
6
6
|
import StoreProvider from "./StoreProvider";
|
|
7
|
+
import AuthLogger from "./AuthLogger";
|
|
7
8
|
export class AuthPlugin {
|
|
8
9
|
authOptions;
|
|
9
10
|
keycloak = new KeycloakPlugin();
|
|
@@ -103,12 +104,14 @@ class KeycloakPlugin {
|
|
|
103
104
|
refreshTokenKey: `${authOptions.appClientId}-refreshToken`,
|
|
104
105
|
idTokenKey: `${authOptions.appClientId}-idToken`
|
|
105
106
|
};
|
|
106
|
-
|
|
107
|
+
AuthLogger.log("Init keycloak");
|
|
108
|
+
return await this.initKeycloak();
|
|
107
109
|
}
|
|
108
110
|
catch (error) {
|
|
109
|
-
|
|
111
|
+
AuthLogger.warn("Init keycloak failed", error);
|
|
110
112
|
await this.clearStorage();
|
|
111
|
-
|
|
113
|
+
AuthLogger.log("Retry init keycloak without tokens");
|
|
114
|
+
return await this.initKeycloak();
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
async initKeycloak() {
|
|
@@ -128,9 +131,7 @@ class KeycloakPlugin {
|
|
|
128
131
|
idToken: await StoreProvider.store.getItem(this.pluginState.idTokenKey),
|
|
129
132
|
...this.pluginState.authOptions.keycloakInitOptions
|
|
130
133
|
});
|
|
131
|
-
|
|
132
|
-
console.debug("authenticated keycloak");
|
|
133
|
-
}
|
|
134
|
+
AuthLogger.debug(authenticated ? "Authenticated keycloak" : "Not authenticated keycloak");
|
|
134
135
|
return authenticated;
|
|
135
136
|
}
|
|
136
137
|
async onTokenExpired() {
|
|
@@ -138,25 +139,25 @@ class KeycloakPlugin {
|
|
|
138
139
|
await this.refresh(-1);
|
|
139
140
|
}
|
|
140
141
|
async login(redirectUri, idp) {
|
|
141
|
-
|
|
142
|
+
AuthLogger.debug("Login navigating to keycloak with returnUrl", redirectUri);
|
|
142
143
|
if (!this.pluginState || !this.pluginState?.keycloak)
|
|
143
144
|
throw new Error("Init has to be called first");
|
|
144
145
|
await this.pluginState.keycloak.login({ redirectUri, idpHint: idp });
|
|
145
146
|
}
|
|
146
147
|
createLoginUrl(redirectUri, idp) {
|
|
147
|
-
|
|
148
|
+
AuthLogger.debug("Create loginUrl to keycloak with returnUrl", redirectUri);
|
|
148
149
|
if (!this.pluginState || !this.pluginState.keycloak)
|
|
149
150
|
throw new Error("Init has to be called first");
|
|
150
151
|
return this.pluginState.keycloak.createLoginUrl({ redirectUri, idpHint: idp });
|
|
151
152
|
}
|
|
152
153
|
createLogoutUrl(redirectUri) {
|
|
153
|
-
|
|
154
|
+
AuthLogger.debug("Create logoutUrl to keycloak with returnUrl", redirectUri);
|
|
154
155
|
if (!this.pluginState || !this.pluginState.keycloak)
|
|
155
156
|
throw new Error("Init has to be called first");
|
|
156
157
|
return this.pluginState.keycloak.createLogoutUrl({ redirectUri });
|
|
157
158
|
}
|
|
158
159
|
async logout(redirectUri) {
|
|
159
|
-
|
|
160
|
+
AuthLogger.debug("Logout navigating to keycloak with returnUrl", redirectUri);
|
|
160
161
|
await this.clearStorage();
|
|
161
162
|
if (!this.pluginState || !this.pluginState.keycloak)
|
|
162
163
|
return;
|
|
@@ -182,11 +183,11 @@ class KeycloakPlugin {
|
|
|
182
183
|
return;
|
|
183
184
|
const successful = await this.pluginState.keycloak.updateToken(minValidity);
|
|
184
185
|
if (successful) {
|
|
185
|
-
|
|
186
|
+
AuthLogger.debug("Token refreshed");
|
|
186
187
|
}
|
|
187
188
|
}
|
|
188
189
|
catch (error) {
|
|
189
|
-
|
|
190
|
+
AuthLogger.error("Token refresh failed", error || "token may be empty");
|
|
190
191
|
}
|
|
191
192
|
}
|
|
192
193
|
// this method ensures that we have only one refresh call even when it gets executed multiple times in paralell
|
|
@@ -226,7 +227,10 @@ class GuestPlugin {
|
|
|
226
227
|
let guestRoles = [];
|
|
227
228
|
if (guestId) {
|
|
228
229
|
guestRoles = authOptions.guestRoles || ["User"];
|
|
229
|
-
|
|
230
|
+
AuthLogger.debug("Authenticated guest");
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
AuthLogger.debug("Not authenticated guest");
|
|
230
234
|
}
|
|
231
235
|
this.pluginState = {
|
|
232
236
|
guestKey: guestKey,
|
|
@@ -236,7 +240,7 @@ class GuestPlugin {
|
|
|
236
240
|
return this.authenticated;
|
|
237
241
|
}
|
|
238
242
|
async login(authOptions) {
|
|
239
|
-
|
|
243
|
+
AuthLogger.debug("Login guest");
|
|
240
244
|
if (!this.pluginState)
|
|
241
245
|
throw new Error("Init has to be called first");
|
|
242
246
|
this.pluginState.guestId = uuidv4();
|
|
@@ -245,7 +249,7 @@ class GuestPlugin {
|
|
|
245
249
|
AuthEvents.emit(AuthEventNames.isGuestAuthenticated);
|
|
246
250
|
}
|
|
247
251
|
async logout() {
|
|
248
|
-
|
|
252
|
+
AuthLogger.debug("Logout guest");
|
|
249
253
|
if (!this.pluginState)
|
|
250
254
|
return;
|
|
251
255
|
await StoreProvider.store.removeItem(this.pluginState.guestKey);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AuthEvents, AuthEventNames } from "./AuthEvents";
|
|
2
2
|
import Auth from "./index";
|
|
3
|
+
import AuthLogger from "./AuthLogger";
|
|
3
4
|
export class AxiosAuthInterceptor {
|
|
4
5
|
static addAuthTokenInterceptor(axiosInstance) {
|
|
5
6
|
return axiosInstance.interceptors.request.use(async (requestConfig) => {
|
|
@@ -18,11 +19,11 @@ export class AxiosAuthInterceptor {
|
|
|
18
19
|
static addAuthErrorInterceptor(axiosInstance) {
|
|
19
20
|
return axiosInstance.interceptors.response.use(null, (error) => {
|
|
20
21
|
if (error.response?.status === 401) {
|
|
21
|
-
|
|
22
|
+
AuthLogger.warn("Axios api request requires authentication", error.config?.url, error.message);
|
|
22
23
|
AuthEvents.emit(AuthEventNames.loginRequired);
|
|
23
24
|
}
|
|
24
25
|
else if (error.response?.status === 403) {
|
|
25
|
-
|
|
26
|
+
AuthLogger.warn("Axios api request requires permission", error.config?.url, error.message);
|
|
26
27
|
AuthEvents.emit(AuthEventNames.permissionDenied);
|
|
27
28
|
}
|
|
28
29
|
throw error;
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samhammer/authentication-vue",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "Keycloak authentication for VueJS projects",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"lib/**/*"
|
|
9
|
-
"../LICENSE",
|
|
10
|
-
"../README.md"
|
|
8
|
+
"lib/**/*"
|
|
11
9
|
],
|
|
12
10
|
"repository": "https://github.com/SamhammerAG/Samhammer.Authentication.Vue",
|
|
13
11
|
"author": "SamhammerAG (https://www.samhammer.de/)",
|
|
@@ -37,7 +35,7 @@
|
|
|
37
35
|
"typescript": "^4.7.4"
|
|
38
36
|
},
|
|
39
37
|
"dependencies": {
|
|
40
|
-
"axios": "^1.
|
|
38
|
+
"axios": "^1.7.9",
|
|
41
39
|
"emittery": "^1.0.1",
|
|
42
40
|
"keycloak-js": "25.0.2",
|
|
43
41
|
"lodash": "^4.17.21",
|