@arsedizioni/ars-utils 20.2.34 → 20.3.3

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.
@@ -13,9 +13,18 @@ const EvolutionMessages = {
13
13
  LOGIN_CHANGED: '§evo-login-changed',
14
14
  LOGIN_COMPLETED: '§evo-login-completed',
15
15
  LOGOUT_COMPLETED: '§evo-logout-completed',
16
+ LOGIN_PENDING: '§evo-login-pending',
16
17
  LOGOUT: '§evo-logout'
17
18
  };
18
19
 
20
+ var EvolutionLoginFlags;
21
+ (function (EvolutionLoginFlags) {
22
+ EvolutionLoginFlags[EvolutionLoginFlags["None"] = 0] = "None";
23
+ EvolutionLoginFlags[EvolutionLoginFlags["RememberCredentials"] = 1] = "RememberCredentials";
24
+ EvolutionLoginFlags[EvolutionLoginFlags["DisableMfa"] = 2] = "DisableMfa";
25
+ EvolutionLoginFlags[EvolutionLoginFlags["ExtendMfaValidity"] = 4] = "ExtendMfaValidity";
26
+ EvolutionLoginFlags[EvolutionLoginFlags["ExtendRememberCredentialsValidity"] = 8] = "ExtendRememberCredentialsValidity";
27
+ })(EvolutionLoginFlags || (EvolutionLoginFlags = {}));
19
28
  var EvolutionServiceFlags;
20
29
  (function (EvolutionServiceFlags) {
21
30
  EvolutionServiceFlags[EvolutionServiceFlags["None"] = 0] = "None";
@@ -487,7 +496,6 @@ class EvolutionService {
487
496
  this._flags = EvolutionServiceFlags.None;
488
497
  this.loggedIn = signal(false, ...(ngDevMode ? [{ debugName: "loggedIn" }] : []));
489
498
  this.loggingIn = signal(false, ...(ngDevMode ? [{ debugName: "loggingIn" }] : []));
490
- this.shouldRefreshToken = signal(false, ...(ngDevMode ? [{ debugName: "shouldRefreshToken" }] : []));
491
499
  this.keepAlive = 0;
492
500
  }
493
501
  get serviceUri() {
@@ -498,7 +506,7 @@ class EvolutionService {
498
506
  }
499
507
  get loginInfo() {
500
508
  if (!this._loginInfo) {
501
- const loginInfo = localStorage.getItem('evolution_login');
509
+ const loginInfo = localStorage.getItem('evolution_context');
502
510
  if (loginInfo) {
503
511
  try {
504
512
  this._loginInfo = JSON.parse(loginInfo);
@@ -574,8 +582,6 @@ class EvolutionService {
574
582
  if (!tokenExpired || this.loggedIn()) {
575
583
  // Auto login
576
584
  this.loggedIn.set(true);
577
- // Should refresh
578
- this.shouldRefreshToken.set(!!tokenExpirationDate);
579
585
  // Keep alive
580
586
  this.setKeepAlive();
581
587
  // Notify
@@ -586,7 +592,7 @@ class EvolutionService {
586
592
  * Get access token expiration date
587
593
  */
588
594
  getTokenExpirationDate() {
589
- const token = this.getToken();
595
+ const token = this.getAuthToken();
590
596
  if (!token)
591
597
  return undefined;
592
598
  // Parse json object from base64 encoded jwt token
@@ -594,14 +600,6 @@ class EvolutionService {
594
600
  // Set a timeout to refresh the token a minute before it expires
595
601
  return new Date(jwtToken.exp * 1000);
596
602
  }
597
- /**
598
- * Checks if access token in expired
599
- */
600
- isTokenExpired() {
601
- const expires = this.getTokenExpirationDate();
602
- const expired = !expires || expires.getTime() < Date.now();
603
- return expired;
604
- }
605
603
  /**
606
604
  * Set keep alive
607
605
  */
@@ -638,26 +636,41 @@ class EvolutionService {
638
636
  if (value.authToken ?? value.token) {
639
637
  sessionStorage.setItem('evolution_auth', value.authToken ?? value.token);
640
638
  }
641
- if (value.refreshToken) {
642
- sessionStorage.setItem('evolution_refresh', value.refreshToken ?? '');
639
+ if (value.rememberToken) {
640
+ localStorage.setItem('evolution_remember', value.rememberToken);
643
641
  }
644
642
  }
645
643
  /**
646
644
  * Return current JWT token
647
645
  * @param refresh: true to get the refresh token. Default is false.
648
646
  */
649
- getToken(refresh = false) {
650
- let token = sessionStorage.getItem(refresh ? 'evolution_refresh' : 'evolution_auth');
647
+ getAuthToken() {
648
+ let token = sessionStorage.getItem('evolution_auth');
651
649
  if (token && token[0] === '"') {
652
650
  return token.substring(1, token.length - 1);
653
651
  }
654
652
  return token;
655
653
  }
654
+ /**
655
+ * Get the two form factor authentication token
656
+ */
657
+ getRememberToken() {
658
+ return localStorage.getItem("myars_remember") ?? undefined;
659
+ }
656
660
  /**
657
661
  * Store login info
658
662
  */
659
- storeLogin() {
660
- localStorage.setItem('evolution_login', JSON.stringify(this._loginInfo));
663
+ storeContext() {
664
+ localStorage.setItem('evolution_context', JSON.stringify(this._loginInfo));
665
+ }
666
+ /**
667
+ * Update context
668
+ * @param result: the new context
669
+ */
670
+ updateContext(result) {
671
+ this._loginInfo = result;
672
+ this.setToken(result);
673
+ this.storeContext();
661
674
  }
662
675
  /**
663
676
  * Perform auto login using current link data
@@ -668,20 +681,11 @@ class EvolutionService {
668
681
  * @param oauthAccessToken: the optional OAuth2 access token
669
682
  * @param onSuccess: function to execute on seccess
670
683
  */
671
- autoLogin(email, password, remember, oauth, oauthAccessToken, onSuccess) {
672
- if (!oauth && !oauthAccessToken && (!email || !password)) {
673
- // Try to use link if exists
674
- const linkInfo = localStorage.getItem('clipper_link_evolution');
675
- if (linkInfo) {
676
- const link = JSON.parse(SystemUtils.cipher(linkInfo, 'evolution', true));
677
- if (link) {
678
- email = link.user;
679
- password = link.password;
680
- remember = true;
681
- }
682
- }
684
+ autoLogin(email, password, remember, oauth, oauthAccessToken, onSuccess, flags) {
685
+ if (remember) {
686
+ flags |= EvolutionLoginFlags.RememberCredentials;
683
687
  }
684
- this.login(email, password, remember, oauth, oauthAccessToken)
688
+ this.login(email ?? this.getRememberToken(), password, remember, oauth, oauthAccessToken, flags)
685
689
  .subscribe({
686
690
  next: r => {
687
691
  if (!r.success) {
@@ -718,6 +722,7 @@ class EvolutionService {
718
722
  }
719
723
  },
720
724
  complete: () => {
725
+ this.clear();
721
726
  if (onSuccess) {
722
727
  onSuccess();
723
728
  }
@@ -731,46 +736,80 @@ class EvolutionService {
731
736
  * @param remember: remember credentials
732
737
  * @param oauth: the optional open authentication supported
733
738
  * @param oauthAccessToken: the optional OAuth2 access token
739
+ * @param flags: the optional login flags
734
740
  * @returns: the login result
735
741
  */
736
- login(email, password, remember, oauth, oauthAccessToken) {
737
- this.loggingIn.set(true);
742
+ login(email, password, remember, oauth, oauthAccessToken, flags) {
743
+ if (remember) {
744
+ flags |= EvolutionLoginFlags.RememberCredentials;
745
+ }
738
746
  return this.httpClient
739
747
  .post(this._serviceUri + '/login2', {
748
+ clientId: localStorage.getItem("evolution_client_id"),
740
749
  user: oauth ? null : email,
741
750
  password: oauth ? null : password,
742
- clientId: localStorage.getItem("evolution_client_id"),
743
- OAUTH: oauth
751
+ remember: remember,
752
+ oauth: oauth,
753
+ flags: flags
744
754
  }, {
745
- headers: !oauth
755
+ headers: !oauth || !oauthAccessToken
746
756
  ? new HttpHeaders()
747
757
  : new HttpHeaders()
748
758
  .set("Authorization", oauthAccessToken ?? '')
749
759
  })
750
- .pipe(finalize(() => this.dialogService.clearBusy()), catchError(err => { this.handleLoginError(); return throwError(() => err); }), map((r) => {
760
+ .pipe(catchError(err => {
751
761
  this.loggingIn.set(false);
762
+ localStorage.removeItem('evolution_context');
763
+ localStorage.removeItem('evolution_remember');
764
+ return throwError(() => err);
765
+ }), map((r) => {
752
766
  if (r.success) {
753
- // Store access token
754
- this.setToken(r.value);
755
- const loginInfo = {
756
- context: r.value.context,
757
- OAUTH: oauth
758
- };
759
- if (!oauth) {
760
- loginInfo.userCredentials =
761
- SystemUtils.cipher(JSON.stringify({
762
- email: email,
763
- password: password,
764
- remember: remember,
765
- }), loginInfo.context.userId.toString());
767
+ if (!this._loginInfo)
768
+ this._loginInfo = r.value;
769
+ this._loginInfo.oauth = oauth;
770
+ if (!oauth && r.value.requiresMfa) {
771
+ // Notify login is pending
772
+ this.broadcastService.sendMessage(EvolutionMessages.LOGIN_PENDING, { flags: flags });
773
+ }
774
+ else {
775
+ // Complete login
776
+ this.completeLogin(r.value);
766
777
  }
767
- this._loginInfo = loginInfo.context;
768
- this.storeLogin();
769
- this.loggedIn.set(true);
770
- // Keep alive
771
- this.setKeepAlive();
772
- // Notify
773
- this.broadcastService.sendMessage(EvolutionMessages.LOGIN_COMPLETED);
778
+ }
779
+ return r;
780
+ }));
781
+ }
782
+ /**
783
+ * Complete login
784
+ * @param result : the login result
785
+ */
786
+ completeLogin(result) {
787
+ // Update context info
788
+ this.updateContext(result);
789
+ this.loggedIn.set(true);
790
+ this.loggingIn.set(false);
791
+ // Keep alive
792
+ this.setKeepAlive();
793
+ // Notify
794
+ this.broadcastService.sendMessage(EvolutionMessages.LOGIN_COMPLETED);
795
+ }
796
+ /**
797
+ * Confirm MFA procedure
798
+ * @param code: the confirm code
799
+ * @param flags: the login flags
800
+ */
801
+ confirmIdentity(code, flags = EvolutionLoginFlags.None) {
802
+ return this.httpClient
803
+ .post(this._serviceUri + '/login/confirm/' + code + '/?flags=' + flags, {})
804
+ .pipe(catchError((err) => {
805
+ localStorage.removeItem('evolution_context');
806
+ localStorage.removeItem('evolution_remember');
807
+ this.loggingIn.set(false);
808
+ return throwError(() => err);
809
+ }), map((r) => {
810
+ if (r.success) {
811
+ // Complete login
812
+ this.completeLogin(r.value);
774
813
  }
775
814
  return r;
776
815
  }));
@@ -782,6 +821,9 @@ class EvolutionService {
782
821
  return this.httpClient.post(this._serviceUri + '/logout', {}).pipe(finalize(() => {
783
822
  this.removeKeepAlive();
784
823
  this.clear();
824
+ // Clean up
825
+ localStorage.removeItem('evolution_context');
826
+ localStorage.removeItem('evolution_remember');
785
827
  }), catchError((_e) => {
786
828
  return of([]);
787
829
  }));
@@ -793,32 +835,22 @@ class EvolutionService {
793
835
  loginSwitch(id) {
794
836
  return this.httpClient
795
837
  .post(this._serviceUri + '/login-switch/', {
838
+ clientId: localStorage.getItem("evolution_client_id"),
796
839
  userId: id,
797
- clientId: localStorage.getItem("evolution_client_id")
798
840
  })
799
841
  .pipe(catchError((err) => {
800
- this.handleLoginError();
842
+ this.loggingIn.set(false);
843
+ localStorage.removeItem('evolution_context');
844
+ localStorage.removeItem('evolution_remember');
801
845
  return throwError(() => err);
802
846
  }), map((r) => {
803
- this.loggingIn.set(false);
804
847
  if (r.success) {
805
- // Store access token
806
- this.setToken(r.value);
807
- this._loginInfo = r.value.context;
808
- this.loggedIn.set(true);
809
- // Notify
810
- this.broadcastService.sendMessage(EvolutionMessages.LOGIN_COMPLETED);
848
+ // Update login
849
+ this.completeLogin(r.value);
811
850
  }
812
851
  return r;
813
852
  }));
814
853
  }
815
- /**
816
- * Handle login error
817
- */
818
- handleLoginError() {
819
- this.loggingIn.set(false);
820
- localStorage.removeItem('evolution_login');
821
- }
822
854
  /**
823
855
  * Reset login refresh timer and login state
824
856
  */
@@ -832,13 +864,17 @@ class EvolutionService {
832
864
  }
833
865
  /**
834
866
  * Clear login data
867
+ * @param clearOAuthToken: true to clear oauth token also
835
868
  */
836
- clear() {
869
+ clear(clearOAuthToken = false) {
837
870
  // Clear local storage
838
871
  sessionStorage.removeItem('evolution_auth');
839
872
  sessionStorage.removeItem('evolution_refresh');
873
+ localStorage.removeItem('evolution_context');
840
874
  sessionStorage.removeItem('evolution_oauth');
841
- localStorage.removeItem('evolution_login');
875
+ if (clearOAuthToken) {
876
+ sessionStorage.removeItem('evolution_oauth_token');
877
+ }
842
878
  // Reset login
843
879
  this.reset();
844
880
  }
@@ -846,7 +882,7 @@ class EvolutionService {
846
882
  * Perform token refresh
847
883
  */
848
884
  refresh() {
849
- return this.httpClient.get(this._serviceUri + '/refresh2/?token=' + this.getToken(true)).pipe(map((r) => {
885
+ return this.httpClient.get(this._serviceUri + '/refresh2').pipe(map((r) => {
850
886
  // Update token
851
887
  this.setToken(r.value);
852
888
  return r;
@@ -1057,7 +1093,7 @@ class EvolutionAuthInterceptor {
1057
1093
  if (request.url.startsWith(this.evolutionService.serviceUri ?? '')) {
1058
1094
  if (this.evolutionService.loggedIn()) {
1059
1095
  if (!token)
1060
- token = this.evolutionService.getToken();
1096
+ token = this.evolutionService.getAuthToken();
1061
1097
  if (token) {
1062
1098
  return request.clone({
1063
1099
  setHeaders: {
@@ -1094,5 +1130,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImpor
1094
1130
  * Generated bundle index. Do not edit.
1095
1131
  */
1096
1132
 
1097
- export { ERPComplianceActivityState, ERPComplianceLawOrigin, ERPComplianceLawState, ERPComplianceLawsSelectionType, ERPComplianceNotificationLimit, ERPComplianceProfileFlags, ERPComplianceProfileRole, ERPComplianceRegisterNotificationType, ERPComplianceRegisterSiteImportOptions, ERPComplianceScope, ERPExportFormat, ERPExportPart, ERPExportSource, ERPExportType, ERPModule, ERPPlace, ERPPlacePermission, ERPRecurrenceFrequencyType, EvolutionAuthInterceptor, EvolutionCommonModule, EvolutionComplianceActivityStates, EvolutionComplianceContextInfo, EvolutionComplianceLawChangeStates, EvolutionComplianceLawOrigins, EvolutionComplianceLawStates, EvolutionComplianceNotificationLimits, EvolutionComplianceNotifications, EvolutionComplianceObligationAuthorities, EvolutionComplianceObligationTypes, EvolutionComplianceProfileFlags, EvolutionComplianceProfileRoles, EvolutionComplianceScopes, EvolutionMessages, EvolutionRecurrenceFrequencyTypes, EvolutionService, EvolutionServiceFlags };
1133
+ export { ERPComplianceActivityState, ERPComplianceLawOrigin, ERPComplianceLawState, ERPComplianceLawsSelectionType, ERPComplianceNotificationLimit, ERPComplianceProfileFlags, ERPComplianceProfileRole, ERPComplianceRegisterNotificationType, ERPComplianceRegisterSiteImportOptions, ERPComplianceScope, ERPExportFormat, ERPExportPart, ERPExportSource, ERPExportType, ERPModule, ERPPlace, ERPPlacePermission, ERPRecurrenceFrequencyType, EvolutionAuthInterceptor, EvolutionCommonModule, EvolutionComplianceActivityStates, EvolutionComplianceContextInfo, EvolutionComplianceLawChangeStates, EvolutionComplianceLawOrigins, EvolutionComplianceLawStates, EvolutionComplianceNotificationLimits, EvolutionComplianceNotifications, EvolutionComplianceObligationAuthorities, EvolutionComplianceObligationTypes, EvolutionComplianceProfileFlags, EvolutionComplianceProfileRoles, EvolutionComplianceScopes, EvolutionLoginFlags, EvolutionMessages, EvolutionRecurrenceFrequencyTypes, EvolutionService, EvolutionServiceFlags };
1098
1134
  //# sourceMappingURL=arsedizioni-ars-utils-evolution.common.mjs.map