@sudoplatform/sudo-user 11.0.0 → 12.0.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 (180) hide show
  1. package/{lib → cjs/src}/client/apiClient.d.ts +0 -0
  2. package/cjs/src/client/apiClient.js +149 -0
  3. package/cjs/src/client/apiClient.js.map +1 -0
  4. package/{lib → cjs/src}/core/api-key-names.d.ts +0 -0
  5. package/cjs/src/core/api-key-names.js +12 -0
  6. package/cjs/src/core/api-key-names.js.map +1 -0
  7. package/{lib → cjs/src}/core/auth-store.d.ts +0 -0
  8. package/cjs/src/core/auth-store.js +92 -0
  9. package/cjs/src/core/auth-store.js.map +1 -0
  10. package/{lib → cjs/src}/core/crypto.d.ts +0 -0
  11. package/cjs/src/core/crypto.js +3 -0
  12. package/cjs/src/core/crypto.js.map +1 -0
  13. package/{lib → cjs/src}/core/key-manager.d.ts +0 -0
  14. package/cjs/src/core/key-manager.js +113 -0
  15. package/cjs/src/core/key-manager.js.map +1 -0
  16. package/cjs/src/core/protected-store.d.ts +5 -0
  17. package/cjs/src/core/protected-store.js +3 -0
  18. package/cjs/src/core/protected-store.js.map +1 -0
  19. package/{lib → cjs/src}/core/publisher.d.ts +0 -0
  20. package/cjs/src/core/publisher.js +3 -0
  21. package/cjs/src/core/publisher.js.map +1 -0
  22. package/{lib → cjs/src}/core/sdk-config.d.ts +0 -0
  23. package/cjs/src/core/sdk-config.js +61 -0
  24. package/cjs/src/core/sdk-config.js.map +1 -0
  25. package/{lib → cjs/src}/core/subscriber.d.ts +0 -0
  26. package/cjs/src/core/subscriber.js +3 -0
  27. package/cjs/src/core/subscriber.js.map +1 -0
  28. package/{lib → cjs/src}/gen/graphql-types.d.ts +0 -0
  29. package/cjs/src/gen/graphql-types.js +105 -0
  30. package/cjs/src/gen/graphql-types.js.map +1 -0
  31. package/{lib → cjs/src}/index.d.ts +0 -0
  32. package/cjs/src/index.js +13 -0
  33. package/cjs/src/index.js.map +1 -0
  34. package/{lib/runtimes/node/node-crypto.d.ts → cjs/src/runtimes/browser/browser-crypto.d.ts} +0 -0
  35. package/cjs/src/runtimes/browser/browser-crypto.js +10 -0
  36. package/cjs/src/runtimes/browser/browser-crypto.js.map +1 -0
  37. package/cjs/src/runtimes/node/node-crypto.d.ts +2 -0
  38. package/cjs/src/runtimes/node/node-crypto.js +9 -0
  39. package/cjs/src/runtimes/node/node-crypto.js.map +1 -0
  40. package/{lib → cjs/src}/sdk.d.ts +0 -0
  41. package/cjs/src/sdk.js +9 -0
  42. package/cjs/src/sdk.js.map +1 -0
  43. package/{lib → cjs/src}/user/auth-provider.d.ts +0 -0
  44. package/cjs/src/user/auth-provider.js +116 -0
  45. package/cjs/src/user/auth-provider.js.map +1 -0
  46. package/{lib → cjs/src}/user/auth.d.ts +0 -0
  47. package/cjs/src/user/auth.js +136 -0
  48. package/cjs/src/user/auth.js.map +1 -0
  49. package/{lib → cjs/src}/user/error.d.ts +0 -0
  50. package/{lib → cjs/src}/user/error.js +16 -29
  51. package/cjs/src/user/error.js.map +1 -0
  52. package/{lib → cjs/src}/user/identity-provider.d.ts +0 -0
  53. package/cjs/src/user/identity-provider.js +245 -0
  54. package/cjs/src/user/identity-provider.js.map +1 -0
  55. package/{lib → cjs/src}/user/user-client-interface.d.ts +0 -0
  56. package/cjs/src/user/user-client-interface.js +3 -0
  57. package/cjs/src/user/user-client-interface.js.map +1 -0
  58. package/{lib → cjs/src}/user/user-client.d.ts +0 -0
  59. package/cjs/src/user/user-client.js +440 -0
  60. package/cjs/src/user/user-client.js.map +1 -0
  61. package/{lib → cjs/src}/user/user-key-names.d.ts +0 -0
  62. package/cjs/src/user/user-key-names.js +10 -0
  63. package/cjs/src/user/user-key-names.js.map +1 -0
  64. package/{lib → cjs/src}/utils/resolvable-promise.d.ts +0 -0
  65. package/cjs/src/utils/resolvable-promise.js +20 -0
  66. package/cjs/src/utils/resolvable-promise.js.map +1 -0
  67. package/cjs/test/integration/user-client-int.spec.d.ts +1 -0
  68. package/cjs/test/integration/user-client-int.spec.js +206 -0
  69. package/cjs/test/integration/user-client-int.spec.js.map +1 -0
  70. package/cjs/test/unit/core/sdk-config.spec.d.ts +1 -0
  71. package/cjs/test/unit/core/sdk-config.spec.js +51 -0
  72. package/cjs/test/unit/core/sdk-config.spec.js.map +1 -0
  73. package/cjs/test/unit/key-manager.spec.d.ts +1 -0
  74. package/cjs/test/unit/key-manager.spec.js +23 -0
  75. package/cjs/test/unit/key-manager.spec.js.map +1 -0
  76. package/cjs/test/unit/user-client.spec.d.ts +1 -0
  77. package/cjs/test/unit/user-client.spec.js +323 -0
  78. package/cjs/test/unit/user-client.spec.js.map +1 -0
  79. package/docs/classes/AlreadyRegisteredError.html +4 -4
  80. package/docs/classes/IdentityServiceConfigNotFoundError.html +4 -4
  81. package/lib/src/client/apiClient.d.ts +25 -0
  82. package/lib/src/client/apiClient.js +145 -0
  83. package/lib/src/client/apiClient.js.map +1 -0
  84. package/lib/src/core/api-key-names.d.ts +8 -0
  85. package/lib/src/core/api-key-names.js +9 -0
  86. package/lib/src/core/api-key-names.js.map +1 -0
  87. package/lib/src/core/auth-store.d.ts +51 -0
  88. package/lib/src/core/auth-store.js +88 -0
  89. package/lib/src/core/auth-store.js.map +1 -0
  90. package/lib/src/core/crypto.d.ts +3 -0
  91. package/lib/src/core/crypto.js +2 -0
  92. package/lib/src/core/crypto.js.map +1 -0
  93. package/lib/src/core/key-manager.d.ts +31 -0
  94. package/lib/src/core/key-manager.js +108 -0
  95. package/lib/src/core/key-manager.js.map +1 -0
  96. package/lib/src/core/protected-store.d.ts +5 -0
  97. package/lib/src/core/protected-store.js +2 -0
  98. package/lib/src/core/protected-store.js.map +1 -0
  99. package/lib/src/core/publisher.d.ts +6 -0
  100. package/lib/src/core/publisher.js +2 -0
  101. package/lib/src/core/publisher.js.map +1 -0
  102. package/lib/src/core/sdk-config.d.ts +52 -0
  103. package/lib/src/core/sdk-config.js +56 -0
  104. package/lib/src/core/sdk-config.js.map +1 -0
  105. package/lib/src/core/subscriber.d.ts +3 -0
  106. package/lib/src/core/subscriber.js +2 -0
  107. package/lib/src/core/subscriber.js.map +1 -0
  108. package/lib/src/gen/graphql-types.d.ts +101 -0
  109. package/lib/src/gen/graphql-types.js +102 -0
  110. package/lib/src/gen/graphql-types.js.map +1 -0
  111. package/lib/src/index.d.ts +4 -0
  112. package/lib/src/index.js +6 -0
  113. package/lib/src/index.js.map +1 -0
  114. package/lib/src/runtimes/browser/browser-crypto.d.ts +2 -0
  115. package/lib/src/runtimes/browser/browser-crypto.js +7 -0
  116. package/lib/src/runtimes/browser/browser-crypto.js.map +1 -0
  117. package/lib/src/runtimes/node/node-crypto.d.ts +2 -0
  118. package/lib/src/runtimes/node/node-crypto.js +6 -0
  119. package/lib/src/runtimes/node/node-crypto.js.map +1 -0
  120. package/lib/src/sdk.d.ts +1 -0
  121. package/lib/src/sdk.js +6 -0
  122. package/lib/src/sdk.js.map +1 -0
  123. package/lib/src/user/auth-provider.d.ts +97 -0
  124. package/lib/src/user/auth-provider.js +108 -0
  125. package/lib/src/user/auth-provider.js.map +1 -0
  126. package/lib/src/user/auth.d.ts +64 -0
  127. package/lib/src/user/auth.js +131 -0
  128. package/lib/src/user/auth.js.map +1 -0
  129. package/lib/src/user/error.d.ts +18 -0
  130. package/lib/src/user/error.js +28 -0
  131. package/lib/src/user/error.js.map +1 -0
  132. package/lib/src/user/identity-provider.d.ts +73 -0
  133. package/lib/src/user/identity-provider.js +241 -0
  134. package/lib/src/user/identity-provider.js.map +1 -0
  135. package/lib/src/user/user-client-interface.d.ts +168 -0
  136. package/lib/src/user/user-client-interface.js +2 -0
  137. package/lib/src/user/user-client-interface.js.map +1 -0
  138. package/lib/src/user/user-client.d.ts +82 -0
  139. package/lib/src/user/user-client.js +435 -0
  140. package/lib/src/user/user-client.js.map +1 -0
  141. package/lib/src/user/user-key-names.d.ts +6 -0
  142. package/lib/src/user/user-key-names.js +7 -0
  143. package/lib/src/user/user-key-names.js.map +1 -0
  144. package/lib/src/utils/resolvable-promise.d.ts +9 -0
  145. package/lib/src/utils/resolvable-promise.js +16 -0
  146. package/lib/src/utils/resolvable-promise.js.map +1 -0
  147. package/lib/test/integration/user-client-int.spec.d.ts +1 -0
  148. package/lib/test/integration/user-client-int.spec.js +203 -0
  149. package/lib/test/integration/user-client-int.spec.js.map +1 -0
  150. package/lib/test/unit/core/sdk-config.spec.d.ts +1 -0
  151. package/lib/test/unit/core/sdk-config.spec.js +49 -0
  152. package/lib/test/unit/core/sdk-config.spec.js.map +1 -0
  153. package/lib/test/unit/key-manager.spec.d.ts +1 -0
  154. package/lib/test/unit/key-manager.spec.js +21 -0
  155. package/lib/test/unit/key-manager.spec.js.map +1 -0
  156. package/lib/test/unit/user-client.spec.d.ts +1 -0
  157. package/lib/test/unit/user-client.spec.js +320 -0
  158. package/lib/test/unit/user-client.spec.js.map +1 -0
  159. package/package.json +23 -22
  160. package/lib/client/apiClient.js +0 -190
  161. package/lib/core/api-key-names.js +0 -15
  162. package/lib/core/auth-store.js +0 -144
  163. package/lib/core/crypto.js +0 -5
  164. package/lib/core/key-manager.js +0 -158
  165. package/lib/core/protected-store.js +0 -5
  166. package/lib/core/publisher.js +0 -5
  167. package/lib/core/sdk-config.js +0 -56
  168. package/lib/core/subscriber.js +0 -5
  169. package/lib/gen/graphql-types.js +0 -141
  170. package/lib/index.js +0 -67
  171. package/lib/runtimes/browser/browser-crypto.js +0 -15
  172. package/lib/runtimes/node/node-crypto.js +0 -12
  173. package/lib/sdk.js +0 -18
  174. package/lib/user/auth-provider.js +0 -158
  175. package/lib/user/auth.js +0 -183
  176. package/lib/user/identity-provider.js +0 -270
  177. package/lib/user/user-client-interface.js +0 -5
  178. package/lib/user/user-client.js +0 -522
  179. package/lib/user/user-key-names.js +0 -13
  180. package/lib/utils/resolvable-promise.js +0 -22
@@ -1,158 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.TESTAuthenticationProvider = exports.TESTAuthenticationInfo = exports.LocalAuthenticationProvider = exports.LocalAuthenticationInfo = void 0;
7
-
8
- var JWT = _interopRequireWildcard(require("jsonwebtoken"));
9
-
10
- var _uuid = require("uuid");
11
-
12
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
-
14
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
-
16
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
-
18
- /**
19
- * Authentication info consisting of a JWT signed using the TEST registration key.
20
- */
21
- class TESTAuthenticationInfo {
22
- constructor(jwt) {
23
- this.jwt = jwt;
24
-
25
- _defineProperty(this, "type", 'TEST');
26
- }
27
-
28
- isValid() {
29
- return true;
30
- }
31
-
32
- encode() {
33
- return this.jwt;
34
- }
35
-
36
- getUsername() {
37
- return '';
38
- }
39
-
40
- }
41
- /**
42
- * Authentication provider for generating authentication info using a TEST registration key.
43
- *
44
- * @param name provider name. This name will be prepended to the generated UUID in JWT sub.
45
- * @param privateKey PEM encoded RSA private key.
46
- * @param keyId key ID of the TEST registration key which is obtained from the admin console.
47
- * @param attributes additional attributes to be added to the issued authentication info.
48
- */
49
-
50
-
51
- exports.TESTAuthenticationInfo = TESTAuthenticationInfo;
52
-
53
- class TESTAuthenticationProvider {
54
- constructor(name, privateKey, keyId = 'register_key', attributes) {
55
- this.name = name;
56
- this.privateKey = privateKey;
57
- this.keyId = keyId;
58
- this.attributes = attributes;
59
-
60
- _defineProperty(this, "testRegistrationIssuer", 'testRegisterIssuer');
61
-
62
- _defineProperty(this, "testRegistrationAudience", 'testRegisterAudience');
63
- }
64
-
65
- async getAuthenticationInfo() {
66
- const jwt = JWT.sign(this.attributes ? this.attributes : {}, this.privateKey, {
67
- jwtid: (0, _uuid.v4)(),
68
- audience: this.testRegistrationAudience,
69
- expiresIn: '60s',
70
- notBefore: '0m',
71
- subject: `${this.name}-${(0, _uuid.v4)()}`,
72
- issuer: this.testRegistrationIssuer,
73
- header: {
74
- alg: 'RS256',
75
- kid: this.keyId
76
- },
77
- algorithm: 'RS256'
78
- });
79
- return new TESTAuthenticationInfo(jwt);
80
- }
81
-
82
- reset() {// Not implemented
83
- }
84
-
85
- }
86
- /**
87
- * Authentication info consisting of a JWT signed using the locally stored private key.
88
- */
89
-
90
-
91
- exports.TESTAuthenticationProvider = TESTAuthenticationProvider;
92
-
93
- class LocalAuthenticationInfo {
94
- constructor(jwt, username) {
95
- this.jwt = jwt;
96
- this.username = username;
97
-
98
- _defineProperty(this, "type", 'FSSO');
99
- }
100
-
101
- isValid() {
102
- return true;
103
- }
104
-
105
- encode() {
106
- return this.jwt;
107
- }
108
-
109
- getUsername() {
110
- return this.username;
111
- }
112
-
113
- }
114
- /**
115
- * Authentication info consisting of a JWT signed using the locally stored private key.
116
- *
117
- * @param name provider name. This name will be used to populate JWT iss (issuer).
118
- * @param privateKey PEM encoded RSA private key.
119
- * @param keyId key ID.
120
- * @param username username to be associated with the issued authentication info.
121
- * @param attributes additional attributes to be added to the issued authentication info.
122
- */
123
-
124
-
125
- exports.LocalAuthenticationInfo = LocalAuthenticationInfo;
126
-
127
- class LocalAuthenticationProvider {
128
- constructor(name, privateKey, keyId, username, attributes) {
129
- this.name = name;
130
- this.privateKey = privateKey;
131
- this.keyId = keyId;
132
- this.username = username;
133
- this.attributes = attributes;
134
- }
135
-
136
- async getAuthenticationInfo() {
137
- const jwt = JWT.sign(this.attributes ? this.attributes : {}, this.privateKey, {
138
- jwtid: (0, _uuid.v4)(),
139
- audience: 'identity-service',
140
- expiresIn: '60s',
141
- notBefore: '0m',
142
- subject: this.username,
143
- issuer: this.name,
144
- header: {
145
- alg: 'RS256',
146
- kid: this.keyId
147
- },
148
- algorithm: 'RS256'
149
- });
150
- return new LocalAuthenticationInfo(jwt, this.username);
151
- }
152
-
153
- reset() {// Not implemented
154
- }
155
-
156
- }
157
-
158
- exports.LocalAuthenticationProvider = LocalAuthenticationProvider;
package/lib/user/auth.js DELETED
@@ -1,183 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.DefaultRefreshTokenLifetime = exports.CognitoAuthUI = void 0;
7
-
8
- var _sudoCommon = require("@sudoplatform/sudo-common");
9
-
10
- var _amazonCognitoAuthJs = require("amazon-cognito-auth-js");
11
-
12
- var _apiKeyNames = require("../core/api-key-names");
13
-
14
- var _resolvablePromise = require("../utils/resolvable-promise");
15
-
16
- var JWT = _interopRequireWildcard(require("jsonwebtoken"));
17
-
18
- var _userKeyNames = require("./user-key-names");
19
-
20
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
-
22
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
-
24
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
-
26
- const DefaultRefreshTokenLifetime = 60;
27
- exports.DefaultRefreshTokenLifetime = DefaultRefreshTokenLifetime;
28
-
29
- class CognitoAuthUI {
30
- constructor(authenticationStore, federatedSignInConfig, keyManager, logger, launchUriFn) {
31
- this.authenticationStore = authenticationStore;
32
- this.federatedSignInConfig = federatedSignInConfig;
33
- this.keyManager = keyManager;
34
- this.launchUriFn = launchUriFn;
35
-
36
- _defineProperty(this, "auth", void 0);
37
-
38
- _defineProperty(this, "tokensRefreshedPromise", void 0);
39
-
40
- _defineProperty(this, "refreshTokenLifetime", void 0);
41
-
42
- _defineProperty(this, "logger", void 0);
43
-
44
- this.authenticationStore.subscribe(this);
45
- this.auth = this.initCognitoAuthSDK();
46
- const refreshTokenLifetime = this.federatedSignInConfig.refreshTokenLifetime;
47
- this.refreshTokenLifetime = refreshTokenLifetime ?? DefaultRefreshTokenLifetime;
48
- this.logger = logger;
49
- }
50
-
51
- initCognitoAuthSDK() {
52
- const cognitoAuthConfig = {
53
- ClientId: this.federatedSignInConfig.appClientId,
54
- AppWebDomain: this.federatedSignInConfig.webDomain,
55
- IdentityProvider: this.federatedSignInConfig.identityProvider,
56
- RedirectUriSignIn: this.federatedSignInConfig.signInRedirectUri,
57
- RedirectUriSignOut: this.federatedSignInConfig.signOutRedirectUri,
58
- TokenScopesArray: ['openid'],
59
- Storage: this.authenticationStore,
60
- LaunchUri: this.launchUriFn
61
- };
62
- const auth = new _amazonCognitoAuthJs.CognitoAuth(cognitoAuthConfig);
63
- auth.useCodeGrantFlow();
64
- auth.userhandler = {
65
- onSuccess: result => {
66
- this.logger.info('Successfully signed in.', {
67
- result
68
- });
69
- },
70
- onFailure: error => {
71
- this.logger.error(new _sudoCommon.AuthenticationError(error).message);
72
- }
73
- };
74
- return auth;
75
- }
76
-
77
- presentFederatedSignInUI() {
78
- this.auth.getSession();
79
- }
80
-
81
- async processFederatedSignInTokens(url) {
82
- this.tokensRefreshedPromise = (0, _resolvablePromise.createResolvablePromise)();
83
- this.auth.parseCognitoWebResponse(url);
84
- const authDetails = await this.tokensRefreshedPromise;
85
- await this.storeTokensInKeyManager(authDetails);
86
- return authDetails.authenticationTokens;
87
- }
88
- /**
89
- * This function is called when the authentication store notifies subscribers of an item update.
90
- *
91
- * @param itemName the name of the item being updated
92
- */
93
-
94
-
95
- update(itemName) {
96
- if (itemName === 'idToken') {
97
- const authTokens = this.getAuthTokens();
98
- const userId = this.authenticationStore.getItem(_apiKeyNames.apiKeyNames.userId);
99
-
100
- if (authTokens && userId) {
101
- this.tokensRefreshedPromise?.resolve({
102
- authenticationTokens: authTokens,
103
- userId
104
- });
105
- }
106
- }
107
-
108
- this.logger.debug('Updated: ', {
109
- itemName
110
- });
111
- }
112
-
113
- reset() {
114
- const cognitoAuthAny = this.auth;
115
- cognitoAuthAny.clearCachedTokensScopes();
116
- cognitoAuthAny.signInUserSession = null;
117
- cognitoAuthAny.signInUserSession = cognitoAuthAny.getCachedSession();
118
- }
119
-
120
- presentSignOutUI() {
121
- const signOutUrl = this.auth.getFQDNSignOut();
122
- this.auth.launchUri(signOutUrl);
123
- }
124
-
125
- async storeRefreshTokenLifetime(refreshTokenLifetime) {
126
- const tokenLifetime = refreshTokenLifetime * 24 * 60 * 60 * 1000 + new Date().getTime();
127
- await this.keyManager.removeItem(_apiKeyNames.apiKeyNames.refreshTokenExpiry);
128
- await this.keyManager.addString(_apiKeyNames.apiKeyNames.refreshTokenExpiry, tokenLifetime.toString());
129
- }
130
-
131
- getAuthTokens() {
132
- const idToken = this.authenticationStore.getItem(_apiKeyNames.apiKeyNames.idToken);
133
- const accessToken = this.authenticationStore.getItem(_apiKeyNames.apiKeyNames.accessToken);
134
- const refreshToken = this.authenticationStore.getItem(_apiKeyNames.apiKeyNames.refreshToken);
135
- const tokenExpiry = this.getTokenExpiry();
136
-
137
- if (idToken && accessToken && refreshToken && tokenExpiry) {
138
- return {
139
- idToken: idToken,
140
- accessToken: accessToken,
141
- refreshToken: refreshToken,
142
- tokenExpiry: tokenExpiry.getTime()
143
- };
144
- } else {
145
- return undefined;
146
- }
147
- }
148
-
149
- getTokenExpiry() {
150
- const expiry = this.authenticationStore.getItem(_apiKeyNames.apiKeyNames.tokenExpiry);
151
- return expiry ? new Date(Number(expiry) * 1000) : undefined;
152
- }
153
- /**
154
- * Store the authentication tokens and user id of the signed in user in the key manager.
155
- */
156
-
157
-
158
- async storeTokensInKeyManager(authDetails) {
159
- await this.keyManager.removeItem(_userKeyNames.userKeyNames.userId);
160
- await this.keyManager.removeItem(_apiKeyNames.apiKeyNames.idToken);
161
- await this.keyManager.removeItem(_apiKeyNames.apiKeyNames.accessToken);
162
- await this.keyManager.removeItem(_apiKeyNames.apiKeyNames.refreshToken);
163
- await this.keyManager.removeItem(_apiKeyNames.apiKeyNames.tokenExpiry);
164
- const authTokens = authDetails.authenticationTokens;
165
- await this.keyManager.addString(_userKeyNames.userKeyNames.userId, authDetails.userId);
166
- await this.keyManager.addString(_apiKeyNames.apiKeyNames.idToken, authTokens.idToken);
167
- await this.keyManager.addString(_apiKeyNames.apiKeyNames.accessToken, authTokens.accessToken);
168
- await this.keyManager.addString(_apiKeyNames.apiKeyNames.refreshToken, authTokens.refreshToken);
169
- const decoded = JWT.decode(authTokens.idToken, {
170
- complete: true
171
- });
172
-
173
- if (decoded) {
174
- const tokenExpiry = decoded.payload['exp'];
175
- await this.keyManager.addString(_apiKeyNames.apiKeyNames.tokenExpiry, tokenExpiry);
176
- }
177
-
178
- await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
179
- }
180
-
181
- }
182
-
183
- exports.CognitoAuthUI = CognitoAuthUI;
@@ -1,270 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.CognitoUserPoolIdentityProvider = void 0;
7
-
8
- var _clientCognitoIdentityProvider = require("@aws-sdk/client-cognito-identity-provider");
9
-
10
- var _apiKeyNames = require("../core/api-key-names");
11
-
12
- var _uuid = require("uuid");
13
-
14
- var _sudoCommon = require("@sudoplatform/sudo-common");
15
-
16
- var _error = require("./error");
17
-
18
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
-
20
- class CognitoUserPoolIdentityProvider {
21
- constructor(keyManager, config, logger) {
22
- this.keyManager = keyManager;
23
- this.config = config;
24
-
25
- _defineProperty(this, "idpService", void 0);
26
-
27
- _defineProperty(this, "refreshTokenLifetime", void 0);
28
-
29
- _defineProperty(this, "logger", void 0);
30
-
31
- this.idpService = this.initCognitoIdpService();
32
- const refreshTokenLifetime = this.config.identityService.refreshTokenLifetime;
33
- this.refreshTokenLifetime = refreshTokenLifetime ?? 60;
34
- this.logger = logger;
35
- }
36
-
37
- initCognitoIdpService() {
38
- const idpService = new _clientCognitoIdentityProvider.CognitoIdentityProviderClient({
39
- region: this.config.identityService.region
40
- });
41
- return idpService;
42
- }
43
-
44
- async globalSignOut(accessToken) {
45
- return new Promise((resolve, reject) => {
46
- const params = {
47
- AccessToken: accessToken
48
- };
49
- const globalSignOut = new _clientCognitoIdentityProvider.GlobalSignOutCommand(params);
50
- this.idpService.send(globalSignOut, (error, data) => {
51
- if (error) {
52
- this.logger.error(error.message);
53
- reject(new _sudoCommon.SignOutError(error.message));
54
- } else {
55
- this.logger.info('User successfully signed out.', {
56
- data
57
- });
58
- resolve();
59
- }
60
- });
61
- });
62
- }
63
-
64
- async register(uid, validationData) {
65
- try {
66
- const signUp = new _clientCognitoIdentityProvider.SignUpCommand({
67
- ValidationData: validationData,
68
- Username: uid,
69
- Password: `@FF57&Z1)123!-${(0, _uuid.v4)()}`,
70
- ClientId: this.config.identityService.clientId
71
- });
72
- const response = await this.idpService.send(signUp);
73
-
74
- if (response.UserConfirmed) {
75
- this.logger.info('User successfully signed up.', {
76
- uid
77
- });
78
- return uid;
79
- } else {
80
- throw new _sudoCommon.UserNotConfirmedError();
81
- }
82
- } catch (err) {
83
- const error = err;
84
- const errorMsg = error.message;
85
-
86
- if (errorMsg.includes('sudoplatform.ServiceError')) {
87
- throw new _sudoCommon.ServiceError(errorMsg);
88
- } else if (errorMsg.includes('sudoplatform.identity.UserValidationFailed') || errorMsg.includes('sudoplatform.identity.TestRegCheckFailed')) {
89
- throw new _sudoCommon.NotAuthorizedError(errorMsg);
90
- } else if (errorMsg.includes('sudoplatform.identity.AlreadyRegistered')) {
91
- throw new _error.AlreadyRegisteredError();
92
- } else {
93
- throw new _sudoCommon.FatalError(errorMsg);
94
- }
95
- }
96
- }
97
-
98
- async signInWithKey(userId, keyId) {
99
- return new Promise(async (resolve, reject) => {
100
- const initiateAuthCommand = new _clientCognitoIdentityProvider.InitiateAuthCommand({
101
- ClientId: this.config.identityService.clientId,
102
- AuthFlow: 'CUSTOM_AUTH',
103
- AuthParameters: {
104
- USERNAME: userId
105
- }
106
- });
107
- const initiateAuthResult = await this.idpService.send(initiateAuthCommand);
108
- const challengeName = initiateAuthResult.ChallengeName;
109
- const session = initiateAuthResult.Session;
110
- const params = initiateAuthResult.ChallengeParameters;
111
-
112
- if (params && params['nonce'] && params['audience'] && challengeName && session) {
113
- const nonce = params['nonce'];
114
- const audience = params['audience'];
115
- const answer = await this.keyManager.signJWT(keyId, {
116
- jti: nonce,
117
- iss: userId,
118
- aud: audience,
119
- sub: userId,
120
- exp: Math.floor(+new Date() / 1000) + 300
121
- });
122
- const respondToAuthChallenge = new _clientCognitoIdentityProvider.RespondToAuthChallengeCommand({
123
- Session: session,
124
- ClientId: this.config.identityService.clientId,
125
- ChallengeName: challengeName,
126
- ChallengeResponses: {
127
- USERNAME: userId,
128
- ANSWER: answer
129
- }
130
- });
131
- const respondToAuthChallengeResult = await this.idpService.send(respondToAuthChallenge);
132
- const authResult = respondToAuthChallengeResult.AuthenticationResult;
133
- const idToken = authResult?.IdToken;
134
- const accessToken = authResult?.AccessToken;
135
- const refreshToken = authResult?.RefreshToken;
136
- const tokenExpiry = authResult?.ExpiresIn;
137
-
138
- if (idToken && accessToken && refreshToken && tokenExpiry) {
139
- const authTokens = {
140
- idToken: idToken,
141
- accessToken: accessToken,
142
- refreshToken: refreshToken,
143
- tokenExpiry: tokenExpiry
144
- };
145
- await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
146
- resolve(authTokens);
147
- } else {
148
- reject(new _sudoCommon.AuthenticationError('Authentication tokens not found.'));
149
- }
150
- } else {
151
- reject(new _sudoCommon.AuthenticationError('Invalid initiate auth result.'));
152
- }
153
- });
154
- }
155
-
156
- async signInWithToken(userId, token, type) {
157
- return new Promise(async (resolve, reject) => {
158
- const initiateAuth = new _clientCognitoIdentityProvider.InitiateAuthCommand({
159
- ClientId: this.config.identityService.clientId,
160
- AuthFlow: 'CUSTOM_AUTH',
161
- AuthParameters: {
162
- USERNAME: userId
163
- }
164
- });
165
- const initiateAuthResult = await this.idpService.send(initiateAuth);
166
- const challengeName = initiateAuthResult.ChallengeName;
167
- const session = initiateAuthResult.Session;
168
-
169
- if (challengeName && session) {
170
- const respondToAuthChallenge = new _clientCognitoIdentityProvider.RespondToAuthChallengeCommand({
171
- Session: session,
172
- ClientId: this.config.identityService.clientId,
173
- ChallengeName: challengeName,
174
- ChallengeResponses: {
175
- USERNAME: userId,
176
- ANSWER: token
177
- },
178
- ClientMetadata: {
179
- challengeType: type
180
- }
181
- });
182
- const respondToAuthChallengeResult = await this.idpService.send(respondToAuthChallenge);
183
- const authResult = respondToAuthChallengeResult.AuthenticationResult;
184
- const idToken = authResult?.IdToken;
185
- const accessToken = authResult?.AccessToken;
186
- const refreshToken = authResult?.RefreshToken;
187
- const tokenExpiry = authResult?.ExpiresIn;
188
-
189
- if (idToken && accessToken && refreshToken && tokenExpiry) {
190
- const authTokens = {
191
- idToken: idToken,
192
- accessToken: accessToken,
193
- refreshToken: refreshToken,
194
- tokenExpiry: tokenExpiry
195
- };
196
- await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
197
- resolve(authTokens);
198
- } else {
199
- reject(new _sudoCommon.AuthenticationError('Authentication tokens not found.'));
200
- }
201
- } else {
202
- reject(new _sudoCommon.AuthenticationError('Invalid initiate auth result.'));
203
- }
204
- });
205
- }
206
-
207
- async refreshTokens(refreshToken) {
208
- return new Promise(async (resolve, reject) => {
209
- try {
210
- const initiateAuth = new _clientCognitoIdentityProvider.InitiateAuthCommand({
211
- ClientId: this.config.identityService.clientId,
212
- AuthFlow: 'REFRESH_TOKEN_AUTH',
213
- AuthParameters: {
214
- REFRESH_TOKEN: refreshToken
215
- }
216
- });
217
- const initiateAuthResult = await this.idpService.send(initiateAuth);
218
- const idToken = initiateAuthResult.AuthenticationResult?.IdToken;
219
- const accessToken = initiateAuthResult.AuthenticationResult?.AccessToken;
220
- const tokenExpiry = initiateAuthResult.AuthenticationResult?.ExpiresIn;
221
-
222
- if (idToken && accessToken && tokenExpiry) {
223
- const authTokens = {
224
- idToken: idToken,
225
- accessToken: accessToken,
226
- refreshToken: refreshToken,
227
- tokenExpiry: tokenExpiry
228
- };
229
- await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
230
- resolve(authTokens);
231
- } else {
232
- reject(new _sudoCommon.AuthenticationError('Authentication tokens not found.'));
233
- }
234
- } catch (err) {
235
- const error = err;
236
- reject(new _sudoCommon.AuthenticationError(error.message));
237
- }
238
- });
239
- }
240
-
241
- async signOut(refreshToken) {
242
- return new Promise((resolve, reject) => {
243
- const params = {
244
- Token: refreshToken,
245
- ClientId: this.config.identityService.clientId
246
- };
247
- const revokeToken = new _clientCognitoIdentityProvider.RevokeTokenCommand(params);
248
- this.idpService.send(revokeToken, (error, data) => {
249
- if (error) {
250
- this.logger.error(error.message);
251
- reject(new _sudoCommon.SignOutError(error.message));
252
- } else {
253
- this.logger.info('User successfully signed out.', {
254
- data
255
- });
256
- resolve();
257
- }
258
- });
259
- });
260
- }
261
-
262
- async storeRefreshTokenLifetime(refreshTokenLifetime) {
263
- const tokenLifetime = refreshTokenLifetime * 24 * 60 * 60 * 1000 + new Date().getTime();
264
- await this.keyManager.removeItem(_apiKeyNames.apiKeyNames.refreshTokenExpiry);
265
- await this.keyManager.addString(_apiKeyNames.apiKeyNames.refreshTokenExpiry, tokenLifetime.toString());
266
- }
267
-
268
- }
269
-
270
- exports.CognitoUserPoolIdentityProvider = CognitoUserPoolIdentityProvider;
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });