@vc-shell/framework 1.0.81 → 1.0.83

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,4 @@
1
- import { computed, Ref, ref, ComputedRef, onUnmounted, getCurrentInstance, inject, watch } from "vue";
1
+ import { computed, Ref, ref, ComputedRef } from "vue";
2
2
  import ClientOAuth2 from "client-oauth2";
3
3
  import {
4
4
  UserDetail,
@@ -7,10 +7,12 @@ import {
7
7
  SecurityResult,
8
8
  ValidatePasswordResetTokenRequest,
9
9
  IdentityResult,
10
+ ExternalSignInClient,
11
+ ChangePasswordRequest,
12
+ LoginType,
13
+ ExternalSignInProviderInfo,
10
14
  } from "./../../api";
11
15
  import { AuthData, RequestPasswordResult, SignInResults } from "./../../types";
12
- import * as _ from "lodash-es";
13
- import fetchIntercept from "fetch-intercept";
14
16
  import { useLocalStorage } from "@vueuse/core";
15
17
  //The Platform Manager uses the same key to store authorization data in the
16
18
  //local storage, so we can exchange authorization data between the Platform Manager
@@ -24,24 +26,12 @@ const authClient = new ClientOAuth2({
24
26
  accessTokenUri: `/connect/token`,
25
27
  scopes: ["offline_access"],
26
28
  });
27
- const activeAuthenticationType = ref();
28
29
  const securityClient = new SecurityClient();
29
-
30
- export interface LoginTypes {
31
- enabled?: boolean;
32
- hasLoginForm?: boolean;
33
- authenticationType?: string;
34
- priority?: number;
35
- }
36
- export interface LoginProviders {
37
- authenticationType?: string;
38
- displayName?: string;
39
- }
30
+ const externalSecurityClient = new ExternalSignInClient();
40
31
 
41
32
  interface IUseUser {
42
33
  user: ComputedRef<UserDetail | null>;
43
34
  loading: ComputedRef<boolean>;
44
- isExternalSignedIn: Ref<boolean>;
45
35
  getAccessToken: () => Promise<string | null>;
46
36
  loadUser: () => Promise<UserDetail>;
47
37
  signIn: (username: string, password: string) => Promise<SignInResults>;
@@ -51,29 +41,18 @@ interface IUseUser {
51
41
  resetPasswordByToken: (userId: string, password: string, token: string) => Promise<SecurityResult>;
52
42
  requestPasswordReset: (loginOrEmail: string) => Promise<RequestPasswordResult>;
53
43
  changeUserPassword: (oldPassword: string, newPassword: string) => Promise<SecurityResult>;
54
- getExternalLoginProviders: () => Promise<LoginProviders[]>;
44
+ getExternalLoginProviders: () => Promise<ExternalSignInProviderInfo[]>;
55
45
  externalSignIn: (authenticationType?: string | undefined, returnUrl?: string | undefined) => void;
56
- externalSignOut: (authenticationType?: string | undefined) => void;
57
- getLoginTypes: () => Promise<LoginTypes[]>;
58
- isAzureAdAuthAvailable: () => Promise<boolean>;
59
- getAzureAdAuthCaption: () => Promise<string>;
46
+ getLoginType: () => Promise<LoginType[]>;
47
+ isAuthenticated: () => Promise<boolean>;
60
48
  }
61
49
 
62
50
  export function useUser(): IUseUser {
63
- const base = inject("platformUrl");
64
- const isExternalSignedIn = useLocalStorage("VC_EXTERNAL_LOGIN", false);
65
-
66
- watch(
67
- () => isExternalSignedIn,
68
- (newVal) => {
69
- if (newVal.value) {
70
- initInterceptor();
71
- } else {
72
- fetchIntercept.clear();
73
- }
74
- },
75
- { immediate: true }
76
- );
51
+ const externalSignInStorage = useLocalStorage<{ providerType: string }>("externalSignIn", { providerType: null });
52
+
53
+ const isAuthenticated = async () => {
54
+ return !!((externalSignInStorage.value && externalSignInStorage.value.providerType) ?? (await getAccessToken()));
55
+ };
77
56
 
78
57
  async function validateToken(userId: string, token: string): Promise<boolean> {
79
58
  let result = false;
@@ -124,7 +103,6 @@ export function useUser(): IUseUser {
124
103
  console.log("[useUser]: an access token has been obtained successfully", authData.value);
125
104
 
126
105
  storeAuthData(authData.value);
127
- isExternalSignedIn.value = false;
128
106
  }
129
107
  await loadUser();
130
108
  return { succeeded: true };
@@ -133,13 +111,13 @@ export function useUser(): IUseUser {
133
111
  async function signOut(): Promise<void> {
134
112
  console.debug(`[@vc-shell/framework#useUser:signOut] - Entry point`);
135
113
 
136
- if (!isExternalSignedIn.value) {
114
+ if (externalSignInStorage.value && externalSignInStorage.value.providerType) {
115
+ externalSignOut(externalSignInStorage.value.providerType);
116
+ externalSignInStorage.value = null;
117
+ } else {
137
118
  user.value = undefined;
138
119
  authData.value = undefined;
139
120
  storeAuthData({});
140
- } else {
141
- externalSignOut(activeAuthenticationType.value);
142
- activeAuthenticationType.value = undefined;
143
121
  }
144
122
  }
145
123
 
@@ -201,6 +179,7 @@ export function useUser(): IUseUser {
201
179
  function storeAuthData(authData: AuthData) {
202
180
  localStorage.setItem(VC_AUTH_DATA_KEY, JSON.stringify({ ...(authData || {}) }));
203
181
  }
182
+
204
183
  async function readAuthData(): Promise<AuthData> {
205
184
  return (await JSON.parse(localStorage.getItem(VC_AUTH_DATA_KEY) || "{}")) as AuthData;
206
185
  }
@@ -226,27 +205,15 @@ export function useUser(): IUseUser {
226
205
  const token = await getAccessToken();
227
206
  let result;
228
207
 
229
- // TODO it's temporary workaround to get valid errors
230
208
  if (token) {
231
209
  try {
232
210
  loading.value = true;
233
- const res = await fetch("/api/platform/security/currentuser/changepassword", {
234
- method: "POST",
235
- headers: {
236
- "Content-Type": "application/json-patch+json",
237
- Accept: "text/plain",
238
- authorization: `Bearer ${token}`,
239
- },
240
- body: JSON.stringify({
241
- oldPassword,
242
- newPassword,
243
- }),
211
+ const command = new ChangePasswordRequest({
212
+ oldPassword,
213
+ newPassword,
244
214
  });
245
- if (res.status !== 500) {
246
- result = await res.text().then((response) => {
247
- return JSON.parse(response);
248
- });
249
- }
215
+
216
+ result = await securityClient.changeCurrentUserPassword(command);
250
217
  } catch (e) {
251
218
  return { succeeded: false, errors: [e.message] } as SecurityResult;
252
219
  } finally {
@@ -254,54 +221,42 @@ export function useUser(): IUseUser {
254
221
  }
255
222
  }
256
223
 
257
- return result as SecurityResult;
224
+ return result;
258
225
  }
259
226
 
260
- async function getLoginTypes(): Promise<LoginTypes[]> {
261
- let result = null as LoginTypes[];
227
+ async function getLoginType(): Promise<LoginType[]> {
228
+ let result = null as LoginType[];
262
229
  try {
263
- const fetchResult = await fetch("/api/platform/security/logintypes", {
264
- method: "GET",
265
- headers: {},
266
- });
267
-
268
- const response = await fetchResult.text();
269
- if (response && response.trim()) {
270
- result = JSON.parse(response) as LoginTypes[];
271
- }
230
+ result = await securityClient.getLoginTypes();
272
231
  } catch (e) {
273
232
  console.error(e);
274
-
275
233
  throw e;
276
234
  }
277
235
 
278
236
  return result;
279
237
  }
280
238
 
281
- async function getExternalLoginProviders(): Promise<LoginProviders[]> {
282
- let result = null as LoginProviders[];
239
+ async function getExternalLoginProviders(): Promise<ExternalSignInProviderInfo[]> {
240
+ let result = null as ExternalSignInProviderInfo[];
283
241
  try {
284
- const fetchResult = await fetch(base + "externalsignin/providers", {
285
- method: "GET",
286
- });
287
-
288
- const response = await fetchResult.text();
289
- if (response && response.trim()) {
290
- result = JSON.parse(response) as LoginProviders[];
291
- }
242
+ result = await externalSecurityClient.getExternalLoginProviders();
292
243
  } catch (e) {
293
244
  console.error(e);
294
-
295
- throw e;
296
245
  }
297
246
 
298
247
  return result;
299
248
  }
300
249
 
301
- async function externalSignIn(authenticationType?: string | undefined, returnUrl?: string | undefined) {
302
- activeAuthenticationType.value = authenticationType;
250
+ async function externalSignIn(authenticationType: string | undefined, returnUrl: string | undefined) {
303
251
  try {
304
- let url_ = base + "externalsignin?";
252
+ let url_ = "externalsignin?";
253
+
254
+ const signInData = {
255
+ providerType: authenticationType,
256
+ };
257
+
258
+ externalSignInStorage.value = signInData;
259
+
305
260
  if (authenticationType === null) throw new Error("The parameter 'authenticationType' cannot be null.");
306
261
  else {
307
262
  if (authenticationType !== undefined)
@@ -309,78 +264,29 @@ export function useUser(): IUseUser {
309
264
  if (returnUrl !== undefined) url_ += "returnUrl=" + encodeURIComponent("" + returnUrl) + "&";
310
265
  }
311
266
  url_ = url_.replace(/[?&]$/, "");
312
- isExternalSignedIn.value = true;
313
267
 
314
268
  window.location.href = url_;
315
269
  } catch (e) {
316
- isExternalSignedIn.value = false;
317
270
  console.error(e);
318
271
 
319
272
  throw e;
320
273
  }
321
274
  }
322
275
 
323
- async function externalSignOut(authenticationType?: string | undefined): Promise<void> {
276
+ async function externalSignOut(authenticationType: string | undefined): Promise<void> {
324
277
  try {
325
- let url_ = base + "externalsignin/signout?";
326
- if (authenticationType === null) throw new Error("The parameter 'authenticationType' cannot be null.");
327
- else {
328
- if (authenticationType !== undefined)
329
- url_ += "authenticationType=" + encodeURIComponent("" + authenticationType) + "&";
330
- }
331
- url_ = url_.replace(/[?&]$/, "");
332
-
333
- await fetch(url_, {
334
- method: "GET",
335
- headers: {},
336
- });
278
+ const url = "externalsignin/signout?authenticationType=" + authenticationType;
279
+ window.location.href = url;
337
280
  } catch (e) {
338
281
  console.error(e);
339
-
340
282
  throw e;
341
283
  }
342
- isExternalSignedIn.value = false;
343
- }
344
-
345
- async function isAzureAdAuthAvailable(): Promise<boolean> {
346
- const loginTypes = await getLoginTypes();
347
- if (loginTypes) {
348
- return (
349
- loginTypes.filter((x) => x.authenticationType === "AzureAD").length > 0 &&
350
- loginTypes.find((x) => x.authenticationType === "AzureAD").enabled
351
- );
352
- }
353
- return false;
354
- }
355
-
356
- async function getAzureAdAuthCaption(): Promise<string> {
357
- const loginProviders = await getExternalLoginProviders();
358
- if (loginProviders) {
359
- return loginProviders.find((x) => x.authenticationType === "AzureAD").displayName;
360
- }
361
- return null;
362
- }
363
-
364
- /* Intercepting requests to explicitly remove auth token when we use AzureAd authentication */
365
- function initInterceptor() {
366
- console.log("[@vc-shell/framework#useUser:initInterceptor]: Entry point");
367
- // store external login in localStorage
368
- return fetchIntercept.register({
369
- request: function (_url, config) {
370
- if (isExternalSignedIn.value) {
371
- const edited = _.omit(config.headers, "authorization");
372
- config.headers = edited;
373
- }
374
-
375
- return [_url, config];
376
- },
377
- });
378
284
  }
379
285
 
380
286
  return {
381
287
  user: computed(() => user.value),
382
288
  loading: computed(() => loading.value),
383
- isExternalSignedIn,
289
+ isAuthenticated,
384
290
  getAccessToken,
385
291
  loadUser,
386
292
  signIn,
@@ -392,9 +298,6 @@ export function useUser(): IUseUser {
392
298
  changeUserPassword,
393
299
  getExternalLoginProviders,
394
300
  externalSignIn,
395
- externalSignOut,
396
- getLoginTypes,
397
- isAzureAdAuthAvailable,
398
- getAzureAdAuthCaption,
301
+ getLoginType,
399
302
  };
400
303
  }
@@ -27,9 +27,10 @@ export declare class ExternalSignInClient extends AuthApiBase {
27
27
  });
28
28
  /**
29
29
  * @param authenticationType (optional)
30
+ * @param returnUrl (optional)
30
31
  * @return Success
31
32
  */
32
- signIn(authenticationType?: string | undefined): Promise<void>;
33
+ signIn(authenticationType?: string | undefined, returnUrl?: string | undefined): Promise<void>;
33
34
  protected processSignIn(response: Response): Promise<void>;
34
35
  /**
35
36
  * @param authenticationType (optional)
@@ -46,8 +47,8 @@ export declare class ExternalSignInClient extends AuthApiBase {
46
47
  /**
47
48
  * @return Success
48
49
  */
49
- getExternalLoginProviders(): Promise<void>;
50
- protected processGetExternalLoginProviders(response: Response): Promise<void>;
50
+ getExternalLoginProviders(): Promise<ExternalSignInProviderInfo[]>;
51
+ protected processGetExternalLoginProviders(response: Response): Promise<ExternalSignInProviderInfo[]>;
51
52
  }
52
53
  export declare class AppsClient extends AuthApiBase {
53
54
  private http;
@@ -503,8 +504,8 @@ export declare class SecurityClient extends AuthApiBase {
503
504
  /**
504
505
  * @return Success
505
506
  */
506
- getLoginTypes(): Promise<void>;
507
- protected processGetLoginTypes(response: Response): Promise<void>;
507
+ getLoginTypes(): Promise<LoginType[]>;
508
+ protected processGetLoginTypes(response: Response): Promise<LoginType[]>;
508
509
  /**
509
510
  * @return Success
510
511
  */
@@ -625,6 +626,7 @@ export declare class ApplicationUser implements IApplicationUser {
625
626
  passwordExpired?: boolean;
626
627
  lastPasswordChangedDate?: Date | undefined;
627
628
  lastPasswordChangeRequestDate?: Date | undefined;
629
+ lastLoginDate?: Date | undefined;
628
630
  id?: string | undefined;
629
631
  userName?: string | undefined;
630
632
  normalizedUserName?: string | undefined;
@@ -665,6 +667,7 @@ export interface IApplicationUser {
665
667
  passwordExpired?: boolean;
666
668
  lastPasswordChangedDate?: Date | undefined;
667
669
  lastPasswordChangeRequestDate?: Date | undefined;
670
+ lastLoginDate?: Date | undefined;
668
671
  id?: string | undefined;
669
672
  userName?: string | undefined;
670
673
  normalizedUserName?: string | undefined;
@@ -1109,6 +1112,18 @@ export declare enum EntryState {
1109
1112
  Deleted = "Deleted",
1110
1113
  Modified = "Modified"
1111
1114
  }
1115
+ export declare class ExternalSignInProviderInfo implements IExternalSignInProviderInfo {
1116
+ authenticationType?: string | undefined;
1117
+ displayName?: string | undefined;
1118
+ constructor(data?: IExternalSignInProviderInfo);
1119
+ init(_data?: any): void;
1120
+ static fromJS(data: any): ExternalSignInProviderInfo;
1121
+ toJSON(data?: any): any;
1122
+ }
1123
+ export interface IExternalSignInProviderInfo {
1124
+ authenticationType?: string | undefined;
1125
+ displayName?: string | undefined;
1126
+ }
1112
1127
  export declare class IdentityError implements IIdentityError {
1113
1128
  code?: string | undefined;
1114
1129
  description?: string | undefined;
@@ -1211,6 +1226,22 @@ export interface ILoginRequest {
1211
1226
  password?: string | undefined;
1212
1227
  rememberMe?: boolean;
1213
1228
  }
1229
+ export declare class LoginType implements ILoginType {
1230
+ enabled?: boolean;
1231
+ hasLoginForm?: boolean;
1232
+ authenticationType?: string | undefined;
1233
+ priority?: number;
1234
+ constructor(data?: ILoginType);
1235
+ init(_data?: any): void;
1236
+ static fromJS(data: any): LoginType;
1237
+ toJSON(data?: any): any;
1238
+ }
1239
+ export interface ILoginType {
1240
+ enabled?: boolean;
1241
+ hasLoginForm?: boolean;
1242
+ authenticationType?: string | undefined;
1243
+ priority?: number;
1244
+ }
1214
1245
  export declare class ModuleAutoInstallPushNotification implements IModuleAutoInstallPushNotification {
1215
1246
  started?: Date | undefined;
1216
1247
  finished?: Date | undefined;