@samhammer/authentication-vue 2.3.0 → 2.4.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.
Files changed (2) hide show
  1. package/lib/AuthPlugin.js +25 -18
  2. package/package.json +1 -1
package/lib/AuthPlugin.js CHANGED
@@ -73,7 +73,7 @@ class KeycloakPlugin {
73
73
  hasRole(authOptions, roleName, apiClientId) {
74
74
  if (!authOptions)
75
75
  return false;
76
- if (this.pluginState) {
76
+ if (this.pluginState && this.pluginState.keycloak) {
77
77
  const resource = !apiClientId ? authOptions.apiClientId : apiClientId;
78
78
  return this.pluginState.keycloak.hasResourceRole(roleName, resource);
79
79
  }
@@ -97,21 +97,12 @@ class KeycloakPlugin {
97
97
  if (!authOptions.appClientId || !authOptions.authUrl || !authOptions.realm) {
98
98
  return false;
99
99
  }
100
- const keycloak = new Keycloak({
101
- url: authOptions.authUrl,
102
- realm: authOptions.realm,
103
- clientId: authOptions.appClientId
104
- });
105
100
  this.pluginState = {
106
- keycloak: keycloak,
107
- authOptios: authOptions,
101
+ authOptions: authOptions,
108
102
  accessTokenKey: `${authOptions.appClientId}-accessToken`,
109
103
  refreshTokenKey: `${authOptions.appClientId}-refreshToken`,
110
104
  idTokenKey: `${authOptions.appClientId}-idToken`
111
105
  };
112
- keycloak.onAuthSuccess = this.setStorage.bind(this);
113
- keycloak.onAuthRefreshSuccess = this.setStorage.bind(this);
114
- keycloak.onTokenExpired = this.onTokenExpired.bind(this);
115
106
  return this.initKeycloak();
116
107
  }
117
108
  catch (error) {
@@ -123,6 +114,11 @@ class KeycloakPlugin {
123
114
  async initKeycloak() {
124
115
  if (!this.pluginState)
125
116
  throw new Error("Init has to be called first");
117
+ delete this.pluginState.keycloak;
118
+ this.pluginState.keycloak = this.getKeycloakInstance();
119
+ this.pluginState.keycloak.onAuthSuccess = this.setStorage.bind(this);
120
+ this.pluginState.keycloak.onAuthRefreshSuccess = this.setStorage.bind(this);
121
+ this.pluginState.keycloak.onTokenExpired = this.onTokenExpired.bind(this);
126
122
  const authenticated = await this.pluginState.keycloak.init({
127
123
  flow: "standard",
128
124
  timeSkew: 0,
@@ -130,7 +126,7 @@ class KeycloakPlugin {
130
126
  refreshToken: await StoreProvider.store.getItem(this.pluginState.refreshTokenKey),
131
127
  token: await StoreProvider.store.getItem(this.pluginState.accessTokenKey),
132
128
  idToken: await StoreProvider.store.getItem(this.pluginState.idTokenKey),
133
- ...this.pluginState.authOptios.keycloakInitOptions
129
+ ...this.pluginState.authOptions.keycloakInitOptions
134
130
  });
135
131
  if (authenticated) {
136
132
  console.debug("authenticated keycloak");
@@ -143,31 +139,31 @@ class KeycloakPlugin {
143
139
  }
144
140
  async login(redirectUri, idp) {
145
141
  console.debug("login navigating to keycloak with returnUrl", redirectUri);
146
- if (!this.pluginState)
142
+ if (!this.pluginState || !this.pluginState?.keycloak)
147
143
  throw new Error("Init has to be called first");
148
144
  await this.pluginState.keycloak.login({ redirectUri, idpHint: idp });
149
145
  }
150
146
  createLoginUrl(redirectUri, idp) {
151
147
  console.debug("create loginUrl to keycloak with returnUrl", redirectUri);
152
- if (!this.pluginState)
148
+ if (!this.pluginState || !this.pluginState.keycloak)
153
149
  throw new Error("Init has to be called first");
154
150
  return this.pluginState.keycloak.createLoginUrl({ redirectUri, idpHint: idp });
155
151
  }
156
152
  createLogoutUrl(redirectUri) {
157
153
  console.debug("create logoutUrl to keycloak with returnUrl", redirectUri);
158
- if (!this.pluginState)
154
+ if (!this.pluginState || !this.pluginState.keycloak)
159
155
  throw new Error("Init has to be called first");
160
156
  return this.pluginState.keycloak.createLogoutUrl({ redirectUri });
161
157
  }
162
158
  async logout(redirectUri) {
163
159
  console.debug("logout navigating to keycloak with returnUrl", redirectUri);
164
160
  await this.clearStorage();
165
- if (!this.pluginState)
161
+ if (!this.pluginState || !this.pluginState.keycloak)
166
162
  return;
167
163
  await this.pluginState.keycloak.logout({ redirectUri });
168
164
  }
169
165
  async setStorage() {
170
- if (!this.pluginState)
166
+ if (!this.pluginState || !this.pluginState.keycloak)
171
167
  return;
172
168
  await StoreProvider.store.setItem(this.pluginState.refreshTokenKey, this.pluginState.keycloak.refreshToken ?? "");
173
169
  await StoreProvider.store.setItem(this.pluginState.accessTokenKey, this.pluginState.keycloak.token ?? "");
@@ -182,7 +178,7 @@ class KeycloakPlugin {
182
178
  }
183
179
  async refresh(minValidity) {
184
180
  try {
185
- if (!this.pluginState)
181
+ if (!this.pluginState || !this.pluginState.keycloak)
186
182
  return;
187
183
  const successful = await this.pluginState.keycloak.updateToken(minValidity);
188
184
  if (successful) {
@@ -196,6 +192,17 @@ class KeycloakPlugin {
196
192
  // this method ensures that we have only one refresh call even when it gets executed multiple times in paralell
197
193
  // NOTE: time for 'throttle wait' (ms) must be lower then 'token minValidity' (sec)
198
194
  refreshSingle = throttle(() => this.refresh(10), 5000, { trailing: false });
195
+ getKeycloakInstance() {
196
+ const state = this.pluginState;
197
+ if (!state || !state.authOptions.appClientId || !state.authOptions.authUrl || !state.authOptions.realm) {
198
+ throw new Error("AuthOptions need to be set correctly");
199
+ }
200
+ return new Keycloak({
201
+ url: state.authOptions.authUrl,
202
+ realm: state.authOptions.realm,
203
+ clientId: state.authOptions.appClientId
204
+ });
205
+ }
199
206
  }
200
207
  class GuestPlugin {
201
208
  pluginState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samhammer/authentication-vue",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "Keycloak authentication for VueJS projects",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",