@annalib/anna-cognito-lib 0.2.1 → 0.2.4

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 (82) hide show
  1. package/.browserslistrc +16 -0
  2. package/README.md +25 -31
  3. package/karma.conf.js +44 -0
  4. package/ng-package.json +7 -0
  5. package/package.json +14 -37
  6. package/src/assets/.gitkeep +0 -0
  7. package/src/assets/images/Anna-Logo.png +0 -0
  8. package/src/lib/anna-cognito-lib.module.ts +51 -0
  9. package/src/lib/components/forgot-password/forgot-password.component.html +44 -0
  10. package/src/lib/components/forgot-password/forgot-password.component.scss +22 -0
  11. package/src/lib/components/forgot-password/forgot-password.component.spec.ts +25 -0
  12. package/src/lib/components/forgot-password/forgot-password.component.ts +75 -0
  13. package/src/lib/components/login/login.component.html +45 -0
  14. package/src/lib/components/login/login.component.scss +10 -0
  15. package/src/lib/components/login/login.component.spec.ts +25 -0
  16. package/src/lib/components/login/login.component.ts +47 -0
  17. package/src/lib/components/password-matching/password-matching.component.html +84 -0
  18. package/src/lib/components/password-matching/password-matching.component.scss +83 -0
  19. package/src/lib/components/password-matching/password-matching.component.spec.ts +25 -0
  20. package/src/lib/components/password-matching/password-matching.component.ts +92 -0
  21. package/src/lib/components/set-new-password/set-new-password.component.html +31 -0
  22. package/src/lib/components/set-new-password/set-new-password.component.scss +1 -0
  23. package/src/lib/components/set-new-password/set-new-password.component.spec.ts +25 -0
  24. package/src/lib/components/set-new-password/set-new-password.component.ts +54 -0
  25. package/src/lib/components/verify-and-set-new-password/verify-and-set-new-password.component.html +54 -0
  26. package/src/lib/components/verify-and-set-new-password/verify-and-set-new-password.component.scss +40 -0
  27. package/src/lib/components/verify-and-set-new-password/verify-and-set-new-password.component.spec.ts +25 -0
  28. package/src/lib/components/verify-and-set-new-password/verify-and-set-new-password.component.ts +70 -0
  29. package/src/lib/config/acl-service.token.ts +22 -0
  30. package/src/lib/config/auth-service.token.ts +15 -0
  31. package/src/lib/config/library.config.ts +8 -0
  32. package/src/lib/constants/loginConstant.ts +50 -0
  33. package/{lib/directives/spinner-button/index.d.ts → src/lib/directives/spinner-button/index.ts} +0 -0
  34. package/src/lib/directives/spinner-button/spinner-button.directive.spec.ts +40 -0
  35. package/src/lib/directives/spinner-button/spinner-button.directive.ts +36 -0
  36. package/src/lib/models/auth.model.ts +24 -0
  37. package/src/lib/scss/_colors.scss +107 -0
  38. package/src/lib/scss/_fonts.scss +86 -0
  39. package/src/lib/scss/_loginTemplate.scss +164 -0
  40. package/src/lib/services/acl.service.ts +24 -0
  41. package/src/lib/services/auth.service.ts +243 -0
  42. package/{public-api.d.ts → src/public-api.ts} +14 -0
  43. package/src/test.ts +27 -0
  44. package/tsconfig.lib.json +18 -0
  45. package/tsconfig.lib.prod.json +10 -0
  46. package/tsconfig.spec.json +17 -0
  47. package/annalib-anna-cognito-lib.d.ts +0 -5
  48. package/esm2020/annalib-anna-cognito-lib.mjs +0 -5
  49. package/esm2020/lib/anna-cognito-lib.module.mjs +0 -74
  50. package/esm2020/lib/components/forgot-password/forgot-password.component.mjs +0 -69
  51. package/esm2020/lib/components/login/login.component.mjs +0 -44
  52. package/esm2020/lib/components/password-matching/password-matching.component.mjs +0 -90
  53. package/esm2020/lib/components/set-new-password/set-new-password.component.mjs +0 -55
  54. package/esm2020/lib/components/verify-and-set-new-password/verify-and-set-new-password.component.mjs +0 -66
  55. package/esm2020/lib/config/acl-service.token.mjs +0 -4
  56. package/esm2020/lib/config/auth-service.token.mjs +0 -4
  57. package/esm2020/lib/config/library.config.mjs +0 -6
  58. package/esm2020/lib/constants/loginConstant.mjs +0 -46
  59. package/esm2020/lib/directives/spinner-button/index.mjs +0 -2
  60. package/esm2020/lib/directives/spinner-button/spinner-button.directive.mjs +0 -42
  61. package/esm2020/lib/models/auth.model.mjs +0 -18
  62. package/esm2020/lib/services/acl.service.mjs +0 -30
  63. package/esm2020/lib/services/auth.service.mjs +0 -218
  64. package/esm2020/public-api.mjs +0 -21
  65. package/fesm2015/annalib-anna-cognito-lib.mjs +0 -703
  66. package/fesm2015/annalib-anna-cognito-lib.mjs.map +0 -1
  67. package/fesm2020/annalib-anna-cognito-lib.mjs +0 -697
  68. package/fesm2020/annalib-anna-cognito-lib.mjs.map +0 -1
  69. package/lib/anna-cognito-lib.module.d.ts +0 -18
  70. package/lib/components/forgot-password/forgot-password.component.d.ts +0 -31
  71. package/lib/components/login/login.component.d.ts +0 -19
  72. package/lib/components/password-matching/password-matching.component.d.ts +0 -22
  73. package/lib/components/set-new-password/set-new-password.component.d.ts +0 -26
  74. package/lib/components/verify-and-set-new-password/verify-and-set-new-password.component.d.ts +0 -30
  75. package/lib/config/acl-service.token.d.ts +0 -19
  76. package/lib/config/auth-service.token.d.ts +0 -11
  77. package/lib/config/library.config.d.ts +0 -7
  78. package/lib/constants/loginConstant.d.ts +0 -40
  79. package/lib/directives/spinner-button/spinner-button.directive.d.ts +0 -15
  80. package/lib/models/auth.model.d.ts +0 -16
  81. package/lib/services/acl.service.d.ts +0 -12
  82. package/lib/services/auth.service.d.ts +0 -53
@@ -0,0 +1,36 @@
1
+ import { Directive, ElementRef, Input, Renderer2, SimpleChanges, OnChanges, OnInit } from "@angular/core";
2
+
3
+ @Directive({
4
+ selector: "[anna-cognito-lib-SpinnerButton]",
5
+ })
6
+ export class SpinnerButtonDirective implements OnInit, OnChanges {
7
+ @Input("anna-cognito-lib-SpinnerButton") loading: boolean;
8
+ @Input("spinnerButtonText") spinnerButtonText: string;
9
+ child: HTMLDivElement;
10
+ button: any;
11
+
12
+ constructor(
13
+ private el: ElementRef,
14
+ private renderer: Renderer2) {
15
+ this.button = this.el.nativeElement;
16
+ this.child = document.createElement("div");
17
+ }
18
+
19
+ ngOnInit() {}
20
+
21
+ ngOnChanges(changes: SimpleChanges) {
22
+ if (changes['spinnerButtonText'] && changes['spinnerButtonText'].currentValue) {
23
+ this.button.innerHTML = this.spinnerButtonText;
24
+ }
25
+ if (changes['loading'] && changes['loading'].currentValue) {
26
+ this.button.innerHTML = "";
27
+ this.button.setAttribute("disabled", "true");
28
+ this.child.classList.add("spinner-for-button-directive");
29
+ this.renderer.appendChild(this.button, this.child);
30
+ } else {
31
+ this.child.classList.remove("spinner-for-button-directive");
32
+ this.button.removeAttribute("disabled");
33
+ this.button.innerHTML = this.spinnerButtonText;
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,24 @@
1
+ export class AuthenticationData {
2
+ Username: string;
3
+ Password: string;
4
+ constructor(Username: string, Password: string) {
5
+ this.Username = Username;
6
+ this.Password = Password;
7
+ }
8
+ }
9
+
10
+ export class UserPoolInformation {
11
+ UserPoolId: string;
12
+ ClientId: string;
13
+ constructor(UserPoolId: string, ClientId: string) {
14
+ this.UserPoolId = UserPoolId;
15
+ this.ClientId = ClientId;
16
+ }
17
+ }
18
+
19
+ export class UsernameInfo {
20
+ isUsernameAvailable: boolean;
21
+ constructor(object: { isUsernameAvailable: boolean }) {
22
+ this.isUsernameAvailable = object.isUsernameAvailable;
23
+ }
24
+ }
@@ -0,0 +1,107 @@
1
+ $primary-color: #268bff;
2
+ $background-white: #ffffff;
3
+ $lightGray: #d3d3d3;
4
+ $lightGray-1: #f1f1f1;
5
+ $lightGray-2: #b3b3b3;
6
+ $lightGray-3: #ededed;
7
+ $lightGray-4: #d4d4d4;
8
+ $lightGray-5: #e6e6e6;
9
+ $lightGray-6: #c8c8c8;
10
+ $lightGray-7: #9b9b9b;
11
+ $lightGray-9: #d9e1e6;
12
+ $lightGray-8: #b7b7b7;
13
+ $dimGray: #747474;
14
+ $lightDimGray: #666666;
15
+ $coralRed: #ff4747;
16
+ $greenCyan: #00c853;
17
+ $bookedColor: #9b1b93;
18
+ $orderedColor: #d780d1;
19
+ $billableColor: #f2bdef;
20
+ $nationalColor: #f5dd31;
21
+ $localColor: #8db72e;
22
+ $lavenderBlue: #c7e1ff;
23
+ $aliceBlue: #e3f0ff;
24
+ $dodgerBlue: #1582ff;
25
+ $lightGrey: #d6d6d6;
26
+ $coldTurkey: #ceb6b6;
27
+ $lightSilver: #d8d8d8;
28
+ $GraythirtySeven: #5e5e5e;
29
+ $charcoal: #4a4a4a;
30
+ $sellerReviewColor: #f3bd18;
31
+ $buyerReviewColor: #1565c0;
32
+ $reviewCompleteColor: #00c853;
33
+ $recoveredPreemptionColor: #0071ce;
34
+ $deepSkyBlue: #00b0ff;
35
+ $licorice: #2e384d;
36
+ $navyBlue: #0071ce;
37
+ $nobelGray: #b7b6b6;
38
+ $whiteSmoke: #f7f6f6;
39
+ $gray: #979797;
40
+ $paleBlue: #c2cfd6;
41
+ $lightViolet: #9169fc;
42
+ $dimViolet: #e5dbff;
43
+ $darkViolet: #555099;
44
+ $whisper: #e9e9e9;
45
+ $lynxWhite: #f7f7f7;
46
+ $lightAliceBlue: #e5f1ff;
47
+ $veryLightGrey: #cfcfcf;
48
+ $darkCharcoal: #333333;
49
+ $noRevisionFound: #f47228;
50
+ $redOrange: #f44336;
51
+ $DRRBgColor: #edecec;
52
+ $darkYellowWarning: #f3c639;
53
+ $lightYellow: #fff8e3;
54
+ $filterRadioContainer: #f4f4f4;
55
+ $oliveGreen: #8bac2a;
56
+ $calendarLabel: #222b45;
57
+ $olivedrab: #6b8e23;
58
+ $redOrange: #fe3824;
59
+ $chineseSilver: #cccccc;
60
+ $grey: #767676;
61
+ $primary-success: #47d490;
62
+ $secondary-success: #ebf6f1;
63
+ $primary-warning: #f3c639;
64
+ $secondary-warning: #fff8e3;
65
+ $primary-dark: #90a4ae;
66
+ $secondary-dark: #ebedee;
67
+ $primary-danger: #fc8383;
68
+ $secondary-danger: #ffeded;
69
+ $Gray94: #f0f0f0;
70
+ $whisper: #ededed;
71
+ $whiteSmoke2: #f4f4f4;
72
+ $pinkSwan: #b5b5b5;
73
+ $matterhorn: #4f4f4f;
74
+ $lightGrey: #d4d4d4;
75
+ $gray74: #bdbdbd;
76
+ $gray86: #dbdbdb;
77
+ $darkBlueShade: #222b45;
78
+ $green: #8bac2a;
79
+ $darkPurpleShade: #555099;
80
+ $pattensBlue: #d8eaff;
81
+ $quartz: #dddceb;
82
+ $darkGray: #a7a7a7;
83
+ $darkGray1: #cbcbcb;
84
+ $awaitingSellerReview: #f7f2d1;
85
+ $awaitingSellerReviewBorder: #dec30b;
86
+ $amber: #ffc107;
87
+ $whiteSmoke3: #efefef;
88
+ $greyChateau: #a2a8ad;
89
+ $roseBud: #f9b3ae;
90
+ $paleRed: #fde4e3;
91
+ $redOrange3: #f44336;
92
+ $redOrange4: #fe3824;
93
+ $gray43: #6e6e6e;
94
+ $dodgerBlue2: #4099ff;
95
+ $fruitSalad: #43a047;
96
+ $nero: #212121;
97
+ $darkGray2: #a4a4a4;
98
+ $pinkSwan: #b7b7b7;
99
+ $blue: #d8eaff;
100
+ $lightBlue: #eff6ff;
101
+ $sunsetOrange: #ff5353;
102
+ $redShade: #ff5252;
103
+ $lightBlueShade: #d6dcff;
104
+ $blueBerry: #536dfe;
105
+ $darkPink: #be489d;
106
+ $lightPinkShade: #f4e7f2;
107
+ $sellerGroupChildRow: #F9F3F8;
@@ -0,0 +1,86 @@
1
+ // body font should be 16px
2
+ $fs-2: 0.125rem;
3
+ $fs-3: 0.188rem;
4
+ $fs-4: 0.25rem;
5
+ $fs-5: 0.3125rem;
6
+ $fs-6: 0.375rem;
7
+ $fs-7: 0.4375rem;
8
+ $fs-8: 0.5rem;
9
+ $fs-9: 0.5625rem;
10
+ $fs-10: 0.625rem;
11
+ $fs-11: 0.6875rem;
12
+ $fs-12: 0.75rem;
13
+ $fs-13: 0.8125rem;
14
+ $fs-14: 0.875rem;
15
+ $fs-15: 0.9375rem;
16
+ $fs-16: 1rem;
17
+ $fs-17: 1.0625rem;
18
+ $fs-18: 1.125rem;
19
+ $fs-19: 1.1875rem;
20
+ $fs-20: 1.25rem;
21
+ $fs-21: 1.3125rem;
22
+ $fs-22: 1.375rem;
23
+ $fs-23: 1.4375rem;
24
+ $fs-24: 1.5rem;
25
+ $fs-25: 1.5625rem;
26
+ $fs-26: 1.625rem;
27
+ $fs-27: 1.6875rem;
28
+ $fs-28: 1.75rem;
29
+ $fs-29: 1.8125rem;
30
+ $fs-30: 1.875rem;
31
+ $fs-31: 1.938rem;
32
+ $fs-32: 2rem;
33
+ $fs-33: 2.063rem;
34
+ $fs-34: 2.125rem;
35
+ $fs-35: 2.188rem;
36
+ $fs-36: 2.25rem;
37
+ $fs-37: 2.313rem;
38
+ $fs-38: 2.375rem;
39
+ $fs-39: 2.438rem;
40
+ $fs-40: 2.5rem;
41
+ $fs-41: 2.563rem;
42
+ $fs-42: 2.625rem;
43
+ $fs-43: 2.688rem;
44
+ $fs-44: 2.75rem;
45
+ $fs-45: 2.813rem;
46
+ $fs-46: 2.875rem;
47
+ $fs-47: 2.938rem;
48
+ $fs-48: 3rem;
49
+ $fs-49: 3.063rem;
50
+ $fs-50: 3.125rem;
51
+ $fs-51: 3.188rem;
52
+ $fs-52: 3.25rem;
53
+ $fs-53: 3.313rem;
54
+ $fs-54: 3.375rem;
55
+ $fs-55: 3.438rem;
56
+ $fs-56: 3.5rem;
57
+ $fs-57: 3.563rem;
58
+ $fs-58: 3.625rem;
59
+ $fs-59: 3.688rem;
60
+ $fs-60: 3.75rem;
61
+ $fs-65: 4.063rem;
62
+ $fs-68: 4.25rem;
63
+ $fs-80: 5rem;
64
+ $fs-70: 4.375rem;
65
+ $fs-90: 5.6rem;
66
+ $fs-115: 7.188rem;
67
+ $fs-120: 7.5rem;
68
+ $fs-128: 8rem;
69
+ $fs-135: 8.438rem;
70
+ $fs-144: 9rem;
71
+ $fs-146: 9.125rem;
72
+ $fs-164: 10.25rem;
73
+ $fs-170: 10.625rem;
74
+ $fs-200: 12.5rem;
75
+ $fs-250: 15.625rem;
76
+ $fs-600: 37.5rem;
77
+
78
+ @mixin fonts($fontFamily, $fontSize, $fontStretch, $fontWeight, $fontStyle, $lineHeight, $letterSpacing) {
79
+ font-family: $fontFamily;
80
+ font-size: $fontSize;
81
+ font-weight: $fontWeight;
82
+ font-stretch: $fontStretch;
83
+ font-style: $fontStyle;
84
+ line-height: $lineHeight;
85
+ letter-spacing: $letterSpacing;
86
+ }
@@ -0,0 +1,164 @@
1
+ @import "./colors", "./fonts";
2
+ @keyframes show {
3
+ 0% {
4
+ opacity: 0;
5
+ }
6
+ 50% {
7
+ opacity: 0.5;
8
+ }
9
+ 100% {
10
+ opacity: 1;
11
+ }
12
+ }
13
+
14
+ @-moz-keyframes show {
15
+ 0% {
16
+ opacity: 0;
17
+ }
18
+ 50% {
19
+ opacity: 0.5;
20
+ }
21
+ 100% {
22
+ opacity: 1;
23
+ }
24
+ }
25
+
26
+ html {
27
+ overflow-y: scroll;
28
+ }
29
+
30
+ @mixin disabled-btn-style() {
31
+ background-color: $chineseSilver;
32
+ color: $background-white;
33
+ cursor: not-allowed;
34
+ }
35
+
36
+ .main {
37
+ background-color: #ffffff;
38
+ background-size: cover;
39
+ font-family: Roboto;
40
+ height: 100vh;
41
+ }
42
+
43
+ section {
44
+ top: 45%;
45
+ left: 50%;
46
+ position: absolute;
47
+ transform: translate(-50%, -50%);
48
+ width: 20.875rem;
49
+ animation: show 0.5s 1;
50
+ -moz-animation: show 0.5s 1;
51
+ }
52
+
53
+ .container {
54
+ width: 100%;
55
+ height: 100%;
56
+ overflow: auto;
57
+ }
58
+
59
+ header {
60
+ height: auto;
61
+ width: 100%;
62
+ text-align: center;
63
+ }
64
+
65
+ article {
66
+ margin-top: 0.9375rem;
67
+ margin-bottom: 0.9375rem;
68
+ border: 1px solid $gray;
69
+ background-color: $whiteSmoke2;
70
+ padding: 2rem 1.375rem;
71
+ border-radius: 0.375rem;
72
+ box-shadow: 0 0.125rem 0.4375rem 0 rgba(0, 0, 0, 0.1);
73
+ & #heading {
74
+ color: $nero;
75
+ text-align: center;
76
+ margin-bottom: 1.5rem;
77
+ @include fonts(Roboto, 1.125rem, normal, normal, normal, normal, normal);
78
+ }
79
+ }
80
+
81
+ #alert-div {
82
+ padding: 0.25rem 0 0 0;
83
+ margin-bottom: 0.75rem;
84
+ border-radius: 0.25rem;
85
+ border: solid 1px $roseBud;
86
+ background-color: $paleRed;
87
+
88
+ & .material-icons {
89
+ color: $redOrange3;
90
+ font-size: 1rem;
91
+ margin-left: 0.5rem;
92
+ }
93
+
94
+ & p {
95
+ margin: -1.5625rem 0 0 1.8rem;
96
+ padding: 0.3125rem 0;
97
+ color: $charcoal;
98
+ @include fonts(Roboto, 0.75rem, normal, normal, normal, normal, normal);
99
+ }
100
+ }
101
+
102
+ footer {
103
+ align-items: baseline;
104
+ color: $gray43;
105
+ opacity: 0.9;
106
+ display: flex;
107
+ @include fonts(Ubuntu, 1rem, normal, normal, normal, normal, normal);
108
+ justify-content: center;
109
+ & span {
110
+ font-size: 1.3125rem;
111
+ font-weight: bold;
112
+ margin-left: 0.25rem;
113
+ }
114
+ }
115
+
116
+ input[type="password"],
117
+ input[type="text"],
118
+ input[type="number"] {
119
+ width: 100%;
120
+ height: 30px;
121
+ padding: 0.75rem 1rem;
122
+ border: 1px solid $gray;
123
+ margin-bottom: 0.625rem;
124
+ border-radius: 0.375rem;
125
+ }
126
+
127
+ .login-btn {
128
+ width: 100%;
129
+ height: 2.5rem;
130
+ border: 0;
131
+ color: #fff;
132
+ background-color: $primary-color;
133
+ border-radius: 0.375rem;
134
+ font-size: 1rem;
135
+ margin-top: 0.9375rem;
136
+ &:hover {
137
+ background-color: $dodgerBlue2;
138
+ cursor: pointer;
139
+ }
140
+ &:disabled {
141
+ @include disabled-btn-style;
142
+ }
143
+ }
144
+
145
+ .input-invalid {
146
+ border: 1px solid $redOrange4 !important;
147
+ }
148
+
149
+ input[type="password"].ng-dirty.ng-invalid,
150
+ input[type="text"].ng-dirty.ng-invalid {
151
+ border: 1px solid $redOrange4;
152
+ }
153
+
154
+ input[type="submit"]:focus,
155
+ input[type="password"]:focus,
156
+ input[type="text"]:focus,
157
+ input[type="checkbox"]:focus,
158
+ button:focus {
159
+ outline: none;
160
+ }
161
+
162
+ input::placeholder {
163
+ color: $darkGray2;
164
+ }
@@ -0,0 +1,24 @@
1
+ import { Inject, Injectable } from "@angular/core";
2
+ import { IAclService, ACL_SERVICE_TOKEN } from '../config/acl-service.token';
3
+
4
+ @Injectable({
5
+ providedIn: "root",
6
+ })
7
+ export class AnnaLibAclService {
8
+ constructor(@Inject(ACL_SERVICE_TOKEN) public consumingProjectAclService: IAclService) {}
9
+
10
+ userGroupsInCognitoJWT: string[];
11
+ isUserAuthorisedToAccessPage(url: string): boolean {
12
+ let privilegeName = this.consumingProjectAclService.urlToCognitoPrivilegeMap[url];
13
+ return this.userGroupsInCognitoJWT.includes(privilegeName);
14
+ }
15
+
16
+ isUserAuthorisedToAccessElement(privilege: string): boolean {
17
+ return this.userGroupsInCognitoJWT.includes(privilege);
18
+ }
19
+
20
+ isUserAuthoriseToAcessManageUser(): boolean {
21
+ return this.consumingProjectAclService.isUserAuthoriseToAcessManageUser();
22
+ }
23
+
24
+ }
@@ -0,0 +1,243 @@
1
+ // Angular import statements
2
+ import { Inject, Injectable } from "@angular/core";
3
+ import { Router } from "@angular/router";
4
+
5
+ // Third party import statements
6
+ import { CognitoUserPool, CognitoUser, AuthenticationDetails, CognitoUserSession } from "amazon-cognito-identity-js";
7
+
8
+ // User defined import statements
9
+ import { LoginConstant } from "../constants/loginConstant";
10
+ import { AuthenticationData, UserPoolInformation } from "../models/auth.model";
11
+ import { AnnaLibAclService } from './acl.service';
12
+ import { LibraryConfig } from '../config/library.config';
13
+ import { IAuthService, AUTH_SERVICE_TOKEN } from '../config/auth-service.token';
14
+
15
+ @Injectable({
16
+ providedIn: "root",
17
+ })
18
+ export class AnnaLibAuthService {
19
+ poolData: UserPoolInformation;
20
+ userPool: CognitoUserPool;
21
+ cognitoUser: CognitoUser;
22
+ authenticationDetails: AuthenticationDetails;
23
+ tokenValid: boolean;
24
+ accessToken: string;
25
+ userAttribute: any;
26
+ loginErrorMessage: string | null;
27
+ setNewPasswordErrorMessage: string;
28
+ setNewPasswordButtonMessage: string;
29
+ verifyAndSetNewPasswordErrorMessage: string;
30
+ verifyAndSetNewPasswordMessage: string;
31
+ verifyAndSetNewPasswordButton: string;
32
+ noOfAttempts = 0;
33
+ sessionLocked = false;
34
+ userName: any;
35
+ accessTokenTimerId: any;
36
+ IdToken: any;
37
+
38
+ constructor(
39
+ private router: Router,
40
+ public aclService: AnnaLibAclService,
41
+ public libraryConfig: LibraryConfig,
42
+ @Inject(AUTH_SERVICE_TOKEN) public consumingProjectAuthService: IAuthService) {
43
+ if(!consumingProjectAuthService) {
44
+ throw new Error('You must provide a authService');
45
+ }
46
+ }
47
+
48
+ ngOnInit() {}
49
+
50
+ /***
51
+ 1. takes user pool data (userPool ID and client ID)
52
+ 2. if current user is null then it means user already logged out then simply return Promise<false>
53
+ 3. if current user present check if token is valid for it.
54
+ */
55
+ isUserLoggedIn(): Promise<boolean> {
56
+ let currentUser = this.getCurrentUserDetails();
57
+ if (currentUser) {
58
+ return new Promise((resolve, reject) => {
59
+ currentUser && currentUser.getSession((error: Error, token: CognitoUserSession | null) => {
60
+ if (token) {
61
+ this.getAllCognitoTokenAndGroups(token);
62
+ resolve(true);
63
+ } else {
64
+ this.sessionTimeout();
65
+ resolve(false);
66
+ }
67
+ });
68
+ });
69
+ } else {
70
+ return new Promise((resolve) => {
71
+ clearInterval(this.accessTokenTimerId);
72
+ resolve(false);
73
+ });
74
+ }
75
+ }
76
+
77
+ sessionTimeout() {
78
+ localStorage.clear();
79
+ clearInterval(this.accessTokenTimerId);
80
+ this.router.navigate([LoginConstant.loginPageUrl]);
81
+ }
82
+
83
+ refreshAccessToken() {
84
+ let currentUser = this.getCurrentUserDetails();
85
+ let accessTokenExpiration = localStorage.getItem("accessTokenExpiration");
86
+ let expTime = accessTokenExpiration ? parseInt(accessTokenExpiration) : 0;
87
+ this.accessTokenTimerId = setInterval(() => {
88
+ let counter = 1000;
89
+ let delayFunction = setTimeout(() => {
90
+ currentUser && currentUser.getSession((error: any, token: CognitoUserSession | null) => {
91
+ if (token) {
92
+ this.getAllCognitoTokenAndGroups(token);
93
+ let newExpTime = this.computeTokenExpiration(this.IdToken);
94
+
95
+ if (newExpTime <= 0) {
96
+ newExpTime = 1000;
97
+ counter = 0;
98
+ if (delayFunction) {
99
+ clearInterval(delayFunction);
100
+ }
101
+ }
102
+ expTime = newExpTime;
103
+ localStorage.setItem("accessTokenExpiration", newExpTime.toString());
104
+ clearInterval(this.accessTokenTimerId);
105
+ this.refreshAccessToken();
106
+ } else if (error.code === "NotAuthorizedException") {
107
+ this.sessionTimeout();
108
+ }
109
+ });
110
+ }, counter);
111
+ }, expTime);
112
+ }
113
+
114
+ computeTokenExpiration(token: any) {
115
+ let currentTime = Math.floor(Date.now() / 1000);
116
+ let tokenTime = token.payload.exp;
117
+ let expTime = (tokenTime - currentTime) * 1000;
118
+ return expTime;
119
+ }
120
+
121
+
122
+ getCurrentUserDetails() {
123
+ let poolData = new UserPoolInformation(this.libraryConfig.cognito.userPoolId, this.libraryConfig.cognito.clientId);
124
+ let userPool = new CognitoUserPool(poolData);
125
+ return userPool.getCurrentUser();
126
+ }
127
+
128
+ getCognitoUserDetails(email: string): CognitoUser {
129
+ this.poolData = new UserPoolInformation(this.libraryConfig.cognito.userPoolId, this.libraryConfig.cognito.clientId);
130
+ this.userPool = new CognitoUserPool(this.poolData);
131
+
132
+ let userData = {
133
+ Username: email,
134
+ Pool: this.userPool,
135
+ };
136
+ let cognitoUser = new CognitoUser(userData);
137
+ return cognitoUser;
138
+ }
139
+
140
+ authenticate(email: string, password: string) {
141
+ this.loginErrorMessage = null;
142
+ let authData = new AuthenticationData(email, password);
143
+ this.authenticationDetails = new AuthenticationDetails(authData);
144
+
145
+ this.cognitoUser = this.getCognitoUserDetails(email);
146
+ this.CognitoAuthentication(this.authenticationDetails);
147
+ }
148
+
149
+ CognitoAuthentication(authenticationDetails: AuthenticationDetails) {
150
+ this.cognitoUser.authenticateUser(authenticationDetails, {
151
+ onSuccess: async (result) => {
152
+ this.getAllCognitoTokenAndGroups(result);
153
+ await this.consumingProjectAuthService.onSuccessfulAuthenticatingUser(result);
154
+ },
155
+ onFailure: (err) => {
156
+ this.consumingProjectAuthService.isLoggingIn = false;
157
+ if (err.message === "Password attempts exceeded") {
158
+ this.loginErrorMessage = LoginConstant.attemptLimitExceeded;
159
+ } else {
160
+ this.loginErrorMessage = LoginConstant.userNamePasswordIncorrect;
161
+ }
162
+ },
163
+ newPasswordRequired: (data) => {
164
+ this.consumingProjectAuthService.isLoggingIn = false;
165
+ this.userAttribute = data;
166
+ this.router.navigate([LoginConstant.setNewPasswordUrl]);
167
+ },
168
+ });
169
+ }
170
+
171
+ setNewPassword(newPassword: any) {
172
+ delete this.userAttribute.email;
173
+ delete this.userAttribute.email_verified;
174
+ delete this.userAttribute.phone_number_verified;
175
+ this.cognitoUser.completeNewPasswordChallenge(newPassword, this.userAttribute, {
176
+ onSuccess: (result) => {
177
+ this.router.navigate([LoginConstant.loginPageUrl]);
178
+ },
179
+ onFailure: (error) => {
180
+ this.setNewPasswordErrorMessage = LoginConstant.sessionExpired;
181
+ this.setNewPasswordButtonMessage = LoginConstant.loginAgain;
182
+ },
183
+ });
184
+ }
185
+
186
+ onForgotPasswordGenerateOTP(email: string) {
187
+ this.userName = email;
188
+ this.cognitoUser = this.getCognitoUserDetails(email);
189
+ this.cognitoUser.forgotPassword({
190
+ onSuccess: (result) => {
191
+ this.verifyAndSetNewPasswordMessage =
192
+ LoginConstant.verifyAndSetNewPasswordMessage +
193
+ result.CodeDeliveryDetails.Destination +
194
+ LoginConstant.pleaseEnterItBelow;
195
+ this.router.navigate([LoginConstant.verifyAndSetNewPasswordUrl]);
196
+ },
197
+ onFailure: (err) => {},
198
+ });
199
+ }
200
+
201
+ verifyCode(verificationCode: string, newPassword: string) {
202
+ this.cognitoUser.confirmPassword(verificationCode, newPassword, {
203
+ onFailure: (error) => {
204
+ this.noOfAttempts = this.noOfAttempts + 1;
205
+ this.verifyAndSetNewPasswordErrorMessage = this.forgotPasswordError(error, this.noOfAttempts);
206
+ },
207
+ onSuccess: () => {
208
+ this.router.navigate([LoginConstant.loginPageUrl]);
209
+ },
210
+ });
211
+ }
212
+
213
+ forgotPasswordError(err: any, noOfAttempts: number) {
214
+ let errorMessage;
215
+ switch (err.code) {
216
+ case "LimitExceededException":
217
+ this.sessionLocked = true;
218
+ errorMessage = LoginConstant.attemptLimitExceeded;
219
+ break;
220
+ case "ExpiredCodeException":
221
+ errorMessage = LoginConstant.sessionExpired;
222
+ this.verifyAndSetNewPasswordButton = LoginConstant.regenerateOtp;
223
+ break;
224
+ default:
225
+ errorMessage = LoginConstant.attempt + " " + noOfAttempts + LoginConstant.attemptMessage;
226
+ }
227
+ return errorMessage;
228
+ }
229
+
230
+ getAllCognitoTokenAndGroups(token: CognitoUserSession) {
231
+ this.accessToken = token.getAccessToken().getJwtToken();
232
+ this.IdToken = token.getIdToken();
233
+ if ("cognito:groups" in token.getAccessToken().payload) {
234
+ this.aclService.userGroupsInCognitoJWT = token.getAccessToken().payload["cognito:groups"];
235
+ } else {
236
+ this.aclService.userGroupsInCognitoJWT = [];
237
+ }
238
+ }
239
+
240
+
241
+
242
+ }
243
+