@samhammer/authentication-vue 2.4.3 → 2.5.1

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,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();
@@ -14,4 +14,5 @@ export declare class AuthPlugin {
14
14
  createLogoutUrl(customRedirectUri?: string): string;
15
15
  update(): Promise<void>;
16
16
  logout(redirectUri: string): Promise<void>;
17
+ clearStorage(): Promise<void>;
17
18
  }
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();
@@ -67,6 +68,9 @@ export class AuthPlugin {
67
68
  }
68
69
  await this.keycloak.logout(redirectUri);
69
70
  }
71
+ async clearStorage() {
72
+ await this.keycloak.clearStorage();
73
+ }
70
74
  }
71
75
  class KeycloakPlugin {
72
76
  pluginState;
@@ -103,12 +107,14 @@ class KeycloakPlugin {
103
107
  refreshTokenKey: `${authOptions.appClientId}-refreshToken`,
104
108
  idTokenKey: `${authOptions.appClientId}-idToken`
105
109
  };
106
- return this.initKeycloak();
110
+ AuthLogger.log("Init keycloak");
111
+ return await this.initKeycloak();
107
112
  }
108
113
  catch (error) {
109
- console.error("auth init failed", error);
114
+ AuthLogger.warn("Init keycloak failed", error);
110
115
  await this.clearStorage();
111
- return false;
116
+ AuthLogger.log("Retry init keycloak without tokens");
117
+ return await this.initKeycloak();
112
118
  }
113
119
  }
114
120
  async initKeycloak() {
@@ -128,9 +134,7 @@ class KeycloakPlugin {
128
134
  idToken: await StoreProvider.store.getItem(this.pluginState.idTokenKey),
129
135
  ...this.pluginState.authOptions.keycloakInitOptions
130
136
  });
131
- if (authenticated) {
132
- console.debug("authenticated keycloak");
133
- }
137
+ AuthLogger.debug(authenticated ? "Authenticated keycloak" : "Not authenticated keycloak");
134
138
  return authenticated;
135
139
  }
136
140
  async onTokenExpired() {
@@ -138,25 +142,25 @@ class KeycloakPlugin {
138
142
  await this.refresh(-1);
139
143
  }
140
144
  async login(redirectUri, idp) {
141
- console.debug("login navigating to keycloak with returnUrl", redirectUri);
145
+ AuthLogger.debug("Login navigating to keycloak with returnUrl", redirectUri);
142
146
  if (!this.pluginState || !this.pluginState?.keycloak)
143
147
  throw new Error("Init has to be called first");
144
148
  await this.pluginState.keycloak.login({ redirectUri, idpHint: idp });
145
149
  }
146
150
  createLoginUrl(redirectUri, idp) {
147
- console.debug("create loginUrl to keycloak with returnUrl", redirectUri);
151
+ AuthLogger.debug("Create loginUrl to keycloak with returnUrl", redirectUri);
148
152
  if (!this.pluginState || !this.pluginState.keycloak)
149
153
  throw new Error("Init has to be called first");
150
154
  return this.pluginState.keycloak.createLoginUrl({ redirectUri, idpHint: idp });
151
155
  }
152
156
  createLogoutUrl(redirectUri) {
153
- console.debug("create logoutUrl to keycloak with returnUrl", redirectUri);
157
+ AuthLogger.debug("Create logoutUrl to keycloak with returnUrl", redirectUri);
154
158
  if (!this.pluginState || !this.pluginState.keycloak)
155
159
  throw new Error("Init has to be called first");
156
160
  return this.pluginState.keycloak.createLogoutUrl({ redirectUri });
157
161
  }
158
162
  async logout(redirectUri) {
159
- console.debug("logout navigating to keycloak with returnUrl", redirectUri);
163
+ AuthLogger.debug("Logout navigating to keycloak with returnUrl", redirectUri);
160
164
  await this.clearStorage();
161
165
  if (!this.pluginState || !this.pluginState.keycloak)
162
166
  return;
@@ -182,11 +186,11 @@ class KeycloakPlugin {
182
186
  return;
183
187
  const successful = await this.pluginState.keycloak.updateToken(minValidity);
184
188
  if (successful) {
185
- console.debug("token refreshed");
189
+ AuthLogger.debug("Token refreshed");
186
190
  }
187
191
  }
188
192
  catch (error) {
189
- console.error("token refresh failed", error || "token may be empty");
193
+ AuthLogger.error("Token refresh failed", error || "token may be empty");
190
194
  }
191
195
  }
192
196
  // this method ensures that we have only one refresh call even when it gets executed multiple times in paralell
@@ -226,7 +230,10 @@ class GuestPlugin {
226
230
  let guestRoles = [];
227
231
  if (guestId) {
228
232
  guestRoles = authOptions.guestRoles || ["User"];
229
- console.debug("authenticated guest");
233
+ AuthLogger.debug("Authenticated guest");
234
+ }
235
+ else {
236
+ AuthLogger.debug("Not authenticated guest");
230
237
  }
231
238
  this.pluginState = {
232
239
  guestKey: guestKey,
@@ -236,7 +243,7 @@ class GuestPlugin {
236
243
  return this.authenticated;
237
244
  }
238
245
  async login(authOptions) {
239
- console.debug("login guest");
246
+ AuthLogger.debug("Login guest");
240
247
  if (!this.pluginState)
241
248
  throw new Error("Init has to be called first");
242
249
  this.pluginState.guestId = uuidv4();
@@ -245,7 +252,7 @@ class GuestPlugin {
245
252
  AuthEvents.emit(AuthEventNames.isGuestAuthenticated);
246
253
  }
247
254
  async logout() {
248
- console.debug("logout guest");
255
+ AuthLogger.debug("Logout guest");
249
256
  if (!this.pluginState)
250
257
  return;
251
258
  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
- console.warn("axios api request requires authentication", error.config?.url, error.message);
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
- console.warn("axios api request requires permission", error.config?.url, error.message);
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@samhammer/authentication-vue",
3
- "version": "2.4.3",
3
+ "version": "2.5.1",
4
4
  "description": "Keycloak authentication for VueJS projects",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",