@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.
- package/{lib → cjs/src}/client/apiClient.d.ts +0 -0
- package/cjs/src/client/apiClient.js +149 -0
- package/cjs/src/client/apiClient.js.map +1 -0
- package/{lib → cjs/src}/core/api-key-names.d.ts +0 -0
- package/cjs/src/core/api-key-names.js +12 -0
- package/cjs/src/core/api-key-names.js.map +1 -0
- package/{lib → cjs/src}/core/auth-store.d.ts +0 -0
- package/cjs/src/core/auth-store.js +92 -0
- package/cjs/src/core/auth-store.js.map +1 -0
- package/{lib → cjs/src}/core/crypto.d.ts +0 -0
- package/cjs/src/core/crypto.js +3 -0
- package/cjs/src/core/crypto.js.map +1 -0
- package/{lib → cjs/src}/core/key-manager.d.ts +0 -0
- package/cjs/src/core/key-manager.js +113 -0
- package/cjs/src/core/key-manager.js.map +1 -0
- package/cjs/src/core/protected-store.d.ts +5 -0
- package/cjs/src/core/protected-store.js +3 -0
- package/cjs/src/core/protected-store.js.map +1 -0
- package/{lib → cjs/src}/core/publisher.d.ts +0 -0
- package/cjs/src/core/publisher.js +3 -0
- package/cjs/src/core/publisher.js.map +1 -0
- package/{lib → cjs/src}/core/sdk-config.d.ts +0 -0
- package/cjs/src/core/sdk-config.js +61 -0
- package/cjs/src/core/sdk-config.js.map +1 -0
- package/{lib → cjs/src}/core/subscriber.d.ts +0 -0
- package/cjs/src/core/subscriber.js +3 -0
- package/cjs/src/core/subscriber.js.map +1 -0
- package/{lib → cjs/src}/gen/graphql-types.d.ts +0 -0
- package/cjs/src/gen/graphql-types.js +105 -0
- package/cjs/src/gen/graphql-types.js.map +1 -0
- package/{lib → cjs/src}/index.d.ts +0 -0
- package/cjs/src/index.js +13 -0
- package/cjs/src/index.js.map +1 -0
- package/{lib/runtimes/node/node-crypto.d.ts → cjs/src/runtimes/browser/browser-crypto.d.ts} +0 -0
- package/cjs/src/runtimes/browser/browser-crypto.js +10 -0
- package/cjs/src/runtimes/browser/browser-crypto.js.map +1 -0
- package/cjs/src/runtimes/node/node-crypto.d.ts +2 -0
- package/cjs/src/runtimes/node/node-crypto.js +9 -0
- package/cjs/src/runtimes/node/node-crypto.js.map +1 -0
- package/{lib → cjs/src}/sdk.d.ts +0 -0
- package/cjs/src/sdk.js +9 -0
- package/cjs/src/sdk.js.map +1 -0
- package/{lib → cjs/src}/user/auth-provider.d.ts +0 -0
- package/cjs/src/user/auth-provider.js +116 -0
- package/cjs/src/user/auth-provider.js.map +1 -0
- package/{lib → cjs/src}/user/auth.d.ts +0 -0
- package/cjs/src/user/auth.js +136 -0
- package/cjs/src/user/auth.js.map +1 -0
- package/{lib → cjs/src}/user/error.d.ts +0 -0
- package/{lib → cjs/src}/user/error.js +16 -29
- package/cjs/src/user/error.js.map +1 -0
- package/{lib → cjs/src}/user/identity-provider.d.ts +0 -0
- package/cjs/src/user/identity-provider.js +245 -0
- package/cjs/src/user/identity-provider.js.map +1 -0
- package/{lib → cjs/src}/user/user-client-interface.d.ts +0 -0
- package/cjs/src/user/user-client-interface.js +3 -0
- package/cjs/src/user/user-client-interface.js.map +1 -0
- package/{lib → cjs/src}/user/user-client.d.ts +0 -0
- package/cjs/src/user/user-client.js +440 -0
- package/cjs/src/user/user-client.js.map +1 -0
- package/{lib → cjs/src}/user/user-key-names.d.ts +0 -0
- package/cjs/src/user/user-key-names.js +10 -0
- package/cjs/src/user/user-key-names.js.map +1 -0
- package/{lib → cjs/src}/utils/resolvable-promise.d.ts +0 -0
- package/cjs/src/utils/resolvable-promise.js +20 -0
- package/cjs/src/utils/resolvable-promise.js.map +1 -0
- package/cjs/test/integration/user-client-int.spec.d.ts +1 -0
- package/cjs/test/integration/user-client-int.spec.js +206 -0
- package/cjs/test/integration/user-client-int.spec.js.map +1 -0
- package/cjs/test/unit/core/sdk-config.spec.d.ts +1 -0
- package/cjs/test/unit/core/sdk-config.spec.js +51 -0
- package/cjs/test/unit/core/sdk-config.spec.js.map +1 -0
- package/cjs/test/unit/key-manager.spec.d.ts +1 -0
- package/cjs/test/unit/key-manager.spec.js +23 -0
- package/cjs/test/unit/key-manager.spec.js.map +1 -0
- package/cjs/test/unit/user-client.spec.d.ts +1 -0
- package/cjs/test/unit/user-client.spec.js +323 -0
- package/cjs/test/unit/user-client.spec.js.map +1 -0
- package/docs/classes/AlreadyRegisteredError.html +4 -4
- package/docs/classes/IdentityServiceConfigNotFoundError.html +4 -4
- package/lib/src/client/apiClient.d.ts +25 -0
- package/lib/src/client/apiClient.js +145 -0
- package/lib/src/client/apiClient.js.map +1 -0
- package/lib/src/core/api-key-names.d.ts +8 -0
- package/lib/src/core/api-key-names.js +9 -0
- package/lib/src/core/api-key-names.js.map +1 -0
- package/lib/src/core/auth-store.d.ts +51 -0
- package/lib/src/core/auth-store.js +88 -0
- package/lib/src/core/auth-store.js.map +1 -0
- package/lib/src/core/crypto.d.ts +3 -0
- package/lib/src/core/crypto.js +2 -0
- package/lib/src/core/crypto.js.map +1 -0
- package/lib/src/core/key-manager.d.ts +31 -0
- package/lib/src/core/key-manager.js +108 -0
- package/lib/src/core/key-manager.js.map +1 -0
- package/lib/src/core/protected-store.d.ts +5 -0
- package/lib/src/core/protected-store.js +2 -0
- package/lib/src/core/protected-store.js.map +1 -0
- package/lib/src/core/publisher.d.ts +6 -0
- package/lib/src/core/publisher.js +2 -0
- package/lib/src/core/publisher.js.map +1 -0
- package/lib/src/core/sdk-config.d.ts +52 -0
- package/lib/src/core/sdk-config.js +56 -0
- package/lib/src/core/sdk-config.js.map +1 -0
- package/lib/src/core/subscriber.d.ts +3 -0
- package/lib/src/core/subscriber.js +2 -0
- package/lib/src/core/subscriber.js.map +1 -0
- package/lib/src/gen/graphql-types.d.ts +101 -0
- package/lib/src/gen/graphql-types.js +102 -0
- package/lib/src/gen/graphql-types.js.map +1 -0
- package/lib/src/index.d.ts +4 -0
- package/lib/src/index.js +6 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/runtimes/browser/browser-crypto.d.ts +2 -0
- package/lib/src/runtimes/browser/browser-crypto.js +7 -0
- package/lib/src/runtimes/browser/browser-crypto.js.map +1 -0
- package/lib/src/runtimes/node/node-crypto.d.ts +2 -0
- package/lib/src/runtimes/node/node-crypto.js +6 -0
- package/lib/src/runtimes/node/node-crypto.js.map +1 -0
- package/lib/src/sdk.d.ts +1 -0
- package/lib/src/sdk.js +6 -0
- package/lib/src/sdk.js.map +1 -0
- package/lib/src/user/auth-provider.d.ts +97 -0
- package/lib/src/user/auth-provider.js +108 -0
- package/lib/src/user/auth-provider.js.map +1 -0
- package/lib/src/user/auth.d.ts +64 -0
- package/lib/src/user/auth.js +131 -0
- package/lib/src/user/auth.js.map +1 -0
- package/lib/src/user/error.d.ts +18 -0
- package/lib/src/user/error.js +28 -0
- package/lib/src/user/error.js.map +1 -0
- package/lib/src/user/identity-provider.d.ts +73 -0
- package/lib/src/user/identity-provider.js +241 -0
- package/lib/src/user/identity-provider.js.map +1 -0
- package/lib/src/user/user-client-interface.d.ts +168 -0
- package/lib/src/user/user-client-interface.js +2 -0
- package/lib/src/user/user-client-interface.js.map +1 -0
- package/lib/src/user/user-client.d.ts +82 -0
- package/lib/src/user/user-client.js +435 -0
- package/lib/src/user/user-client.js.map +1 -0
- package/lib/src/user/user-key-names.d.ts +6 -0
- package/lib/src/user/user-key-names.js +7 -0
- package/lib/src/user/user-key-names.js.map +1 -0
- package/lib/src/utils/resolvable-promise.d.ts +9 -0
- package/lib/src/utils/resolvable-promise.js +16 -0
- package/lib/src/utils/resolvable-promise.js.map +1 -0
- package/lib/test/integration/user-client-int.spec.d.ts +1 -0
- package/lib/test/integration/user-client-int.spec.js +203 -0
- package/lib/test/integration/user-client-int.spec.js.map +1 -0
- package/lib/test/unit/core/sdk-config.spec.d.ts +1 -0
- package/lib/test/unit/core/sdk-config.spec.js +49 -0
- package/lib/test/unit/core/sdk-config.spec.js.map +1 -0
- package/lib/test/unit/key-manager.spec.d.ts +1 -0
- package/lib/test/unit/key-manager.spec.js +21 -0
- package/lib/test/unit/key-manager.spec.js.map +1 -0
- package/lib/test/unit/user-client.spec.d.ts +1 -0
- package/lib/test/unit/user-client.spec.js +320 -0
- package/lib/test/unit/user-client.spec.js.map +1 -0
- package/package.json +23 -22
- package/lib/client/apiClient.js +0 -190
- package/lib/core/api-key-names.js +0 -15
- package/lib/core/auth-store.js +0 -144
- package/lib/core/crypto.js +0 -5
- package/lib/core/key-manager.js +0 -158
- package/lib/core/protected-store.js +0 -5
- package/lib/core/publisher.js +0 -5
- package/lib/core/sdk-config.js +0 -56
- package/lib/core/subscriber.js +0 -5
- package/lib/gen/graphql-types.js +0 -141
- package/lib/index.js +0 -67
- package/lib/runtimes/browser/browser-crypto.js +0 -15
- package/lib/runtimes/node/node-crypto.js +0 -12
- package/lib/sdk.js +0 -18
- package/lib/user/auth-provider.js +0 -158
- package/lib/user/auth.js +0 -183
- package/lib/user/identity-provider.js +0 -270
- package/lib/user/user-client-interface.js +0 -5
- package/lib/user/user-client.js +0 -522
- package/lib/user/user-key-names.js +0 -13
- package/lib/utils/resolvable-promise.js +0 -22
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Logger } from '@sudoplatform/sudo-common';
|
|
2
|
+
import { AuthenticationStore } from '../core/auth-store';
|
|
3
|
+
import { KeyManager } from '../core/key-manager';
|
|
4
|
+
import { FederatedSignInConfig } from '../core/sdk-config';
|
|
5
|
+
import { Subscriber } from '../core/subscriber';
|
|
6
|
+
import { AuthenticationTokens } from './user-client-interface';
|
|
7
|
+
export interface AuthenticationDetails {
|
|
8
|
+
authenticationTokens: AuthenticationTokens;
|
|
9
|
+
userId: string;
|
|
10
|
+
}
|
|
11
|
+
export interface AuthUI {
|
|
12
|
+
/**
|
|
13
|
+
* Presents the sign in UI for federated sign in using an external identity provider.
|
|
14
|
+
*/
|
|
15
|
+
presentFederatedSignInUI(): void;
|
|
16
|
+
/**
|
|
17
|
+
* Processes tokens from federated sign in that are returned in exchange
|
|
18
|
+
* for the authorization code that is returned to the specified URL.
|
|
19
|
+
*
|
|
20
|
+
* @param url callback URL containing the authorization code.
|
|
21
|
+
* @return Successful authentication result AuthenticationTokens.
|
|
22
|
+
*/
|
|
23
|
+
processFederatedSignInTokens(url: string): Promise<AuthenticationTokens>;
|
|
24
|
+
/**
|
|
25
|
+
* Resets any internal state.
|
|
26
|
+
*/
|
|
27
|
+
reset(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Presents the Cognito hosted UI signout endpoint.
|
|
30
|
+
* When the endpoint is invoked, the hosted web app's cookies
|
|
31
|
+
* will be invalidated, but the user is not logged out of Cognito.
|
|
32
|
+
*/
|
|
33
|
+
presentSignOutUI(): void;
|
|
34
|
+
}
|
|
35
|
+
export declare const DefaultRefreshTokenLifetime = 60;
|
|
36
|
+
export declare class CognitoAuthUI implements AuthUI, Subscriber {
|
|
37
|
+
private authenticationStore;
|
|
38
|
+
private federatedSignInConfig;
|
|
39
|
+
private keyManager;
|
|
40
|
+
private launchUriFn?;
|
|
41
|
+
private auth;
|
|
42
|
+
private tokensRefreshedPromise?;
|
|
43
|
+
private refreshTokenLifetime;
|
|
44
|
+
private logger;
|
|
45
|
+
constructor(authenticationStore: AuthenticationStore, federatedSignInConfig: FederatedSignInConfig, keyManager: KeyManager, logger: Logger, launchUriFn?: ((url: string) => void) | undefined);
|
|
46
|
+
private initCognitoAuthSDK;
|
|
47
|
+
presentFederatedSignInUI(): void;
|
|
48
|
+
processFederatedSignInTokens(url: string): Promise<AuthenticationTokens>;
|
|
49
|
+
/**
|
|
50
|
+
* This function is called when the authentication store notifies subscribers of an item update.
|
|
51
|
+
*
|
|
52
|
+
* @param itemName the name of the item being updated
|
|
53
|
+
*/
|
|
54
|
+
update(itemName: string): void;
|
|
55
|
+
reset(): void;
|
|
56
|
+
presentSignOutUI(): void;
|
|
57
|
+
private storeRefreshTokenLifetime;
|
|
58
|
+
private getAuthTokens;
|
|
59
|
+
private getTokenExpiry;
|
|
60
|
+
/**
|
|
61
|
+
* Store the authentication tokens and user id of the signed in user in the key manager.
|
|
62
|
+
*/
|
|
63
|
+
private storeTokensInKeyManager;
|
|
64
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { AuthenticationError } from '@sudoplatform/sudo-common';
|
|
2
|
+
import { CognitoAuth } from 'amazon-cognito-auth-js';
|
|
3
|
+
import { apiKeyNames } from '../core/api-key-names';
|
|
4
|
+
import { createResolvablePromise, } from '../utils/resolvable-promise';
|
|
5
|
+
import * as JWT from 'jsonwebtoken';
|
|
6
|
+
import { userKeyNames } from './user-key-names';
|
|
7
|
+
export const DefaultRefreshTokenLifetime = 60;
|
|
8
|
+
export class CognitoAuthUI {
|
|
9
|
+
constructor(authenticationStore, federatedSignInConfig, keyManager, logger, launchUriFn) {
|
|
10
|
+
this.authenticationStore = authenticationStore;
|
|
11
|
+
this.federatedSignInConfig = federatedSignInConfig;
|
|
12
|
+
this.keyManager = keyManager;
|
|
13
|
+
this.launchUriFn = launchUriFn;
|
|
14
|
+
this.authenticationStore.subscribe(this);
|
|
15
|
+
this.auth = this.initCognitoAuthSDK();
|
|
16
|
+
const refreshTokenLifetime = this.federatedSignInConfig.refreshTokenLifetime;
|
|
17
|
+
this.refreshTokenLifetime =
|
|
18
|
+
refreshTokenLifetime ?? DefaultRefreshTokenLifetime;
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
}
|
|
21
|
+
initCognitoAuthSDK() {
|
|
22
|
+
const cognitoAuthConfig = {
|
|
23
|
+
ClientId: this.federatedSignInConfig.appClientId,
|
|
24
|
+
AppWebDomain: this.federatedSignInConfig.webDomain,
|
|
25
|
+
IdentityProvider: this.federatedSignInConfig.identityProvider,
|
|
26
|
+
RedirectUriSignIn: this.federatedSignInConfig.signInRedirectUri,
|
|
27
|
+
RedirectUriSignOut: this.federatedSignInConfig.signOutRedirectUri,
|
|
28
|
+
TokenScopesArray: ['openid'],
|
|
29
|
+
Storage: this.authenticationStore,
|
|
30
|
+
LaunchUri: this.launchUriFn,
|
|
31
|
+
};
|
|
32
|
+
const auth = new CognitoAuth(cognitoAuthConfig);
|
|
33
|
+
auth.useCodeGrantFlow();
|
|
34
|
+
auth.userhandler = {
|
|
35
|
+
onSuccess: (result) => {
|
|
36
|
+
this.logger.info('Successfully signed in.', { result });
|
|
37
|
+
},
|
|
38
|
+
onFailure: (error) => {
|
|
39
|
+
this.logger.error(new AuthenticationError(error).message);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
return auth;
|
|
43
|
+
}
|
|
44
|
+
presentFederatedSignInUI() {
|
|
45
|
+
this.auth.getSession();
|
|
46
|
+
}
|
|
47
|
+
async processFederatedSignInTokens(url) {
|
|
48
|
+
this.tokensRefreshedPromise =
|
|
49
|
+
createResolvablePromise();
|
|
50
|
+
this.auth.parseCognitoWebResponse(url);
|
|
51
|
+
const authDetails = await this.tokensRefreshedPromise;
|
|
52
|
+
await this.storeTokensInKeyManager(authDetails);
|
|
53
|
+
return authDetails.authenticationTokens;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* This function is called when the authentication store notifies subscribers of an item update.
|
|
57
|
+
*
|
|
58
|
+
* @param itemName the name of the item being updated
|
|
59
|
+
*/
|
|
60
|
+
update(itemName) {
|
|
61
|
+
if (itemName === 'idToken') {
|
|
62
|
+
const authTokens = this.getAuthTokens();
|
|
63
|
+
const userId = this.authenticationStore.getItem(apiKeyNames.userId);
|
|
64
|
+
if (authTokens && userId) {
|
|
65
|
+
this.tokensRefreshedPromise?.resolve({
|
|
66
|
+
authenticationTokens: authTokens,
|
|
67
|
+
userId,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.logger.debug('Updated: ', { itemName });
|
|
72
|
+
}
|
|
73
|
+
reset() {
|
|
74
|
+
const cognitoAuthAny = this.auth;
|
|
75
|
+
cognitoAuthAny.clearCachedTokensScopes();
|
|
76
|
+
cognitoAuthAny.signInUserSession = null;
|
|
77
|
+
cognitoAuthAny.signInUserSession = cognitoAuthAny.getCachedSession();
|
|
78
|
+
}
|
|
79
|
+
presentSignOutUI() {
|
|
80
|
+
const signOutUrl = this.auth.getFQDNSignOut();
|
|
81
|
+
this.auth.launchUri(signOutUrl);
|
|
82
|
+
}
|
|
83
|
+
async storeRefreshTokenLifetime(refreshTokenLifetime) {
|
|
84
|
+
const tokenLifetime = refreshTokenLifetime * 24 * 60 * 60 * 1000 + new Date().getTime();
|
|
85
|
+
await this.keyManager.removeItem(apiKeyNames.refreshTokenExpiry);
|
|
86
|
+
await this.keyManager.addString(apiKeyNames.refreshTokenExpiry, tokenLifetime.toString());
|
|
87
|
+
}
|
|
88
|
+
getAuthTokens() {
|
|
89
|
+
const idToken = this.authenticationStore.getItem(apiKeyNames.idToken);
|
|
90
|
+
const accessToken = this.authenticationStore.getItem(apiKeyNames.accessToken);
|
|
91
|
+
const refreshToken = this.authenticationStore.getItem(apiKeyNames.refreshToken);
|
|
92
|
+
const tokenExpiry = this.getTokenExpiry();
|
|
93
|
+
if (idToken && accessToken && refreshToken && tokenExpiry) {
|
|
94
|
+
return {
|
|
95
|
+
idToken: idToken,
|
|
96
|
+
accessToken: accessToken,
|
|
97
|
+
refreshToken: refreshToken,
|
|
98
|
+
tokenExpiry: tokenExpiry.getTime(),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
getTokenExpiry() {
|
|
106
|
+
const expiry = this.authenticationStore.getItem(apiKeyNames.tokenExpiry);
|
|
107
|
+
return expiry ? new Date(Number(expiry) * 1000) : undefined;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Store the authentication tokens and user id of the signed in user in the key manager.
|
|
111
|
+
*/
|
|
112
|
+
async storeTokensInKeyManager(authDetails) {
|
|
113
|
+
await this.keyManager.removeItem(userKeyNames.userId);
|
|
114
|
+
await this.keyManager.removeItem(apiKeyNames.idToken);
|
|
115
|
+
await this.keyManager.removeItem(apiKeyNames.accessToken);
|
|
116
|
+
await this.keyManager.removeItem(apiKeyNames.refreshToken);
|
|
117
|
+
await this.keyManager.removeItem(apiKeyNames.tokenExpiry);
|
|
118
|
+
const authTokens = authDetails.authenticationTokens;
|
|
119
|
+
await this.keyManager.addString(userKeyNames.userId, authDetails.userId);
|
|
120
|
+
await this.keyManager.addString(apiKeyNames.idToken, authTokens.idToken);
|
|
121
|
+
await this.keyManager.addString(apiKeyNames.accessToken, authTokens.accessToken);
|
|
122
|
+
await this.keyManager.addString(apiKeyNames.refreshToken, authTokens.refreshToken);
|
|
123
|
+
const decoded = JWT.decode(authTokens.idToken, { complete: true });
|
|
124
|
+
if (decoded) {
|
|
125
|
+
const tokenExpiry = decoded.payload['exp'];
|
|
126
|
+
await this.keyManager.addString(apiKeyNames.tokenExpiry, tokenExpiry);
|
|
127
|
+
}
|
|
128
|
+
await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../src/user/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAU,MAAM,2BAA2B,CAAA;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAKnD,OAAO,EACL,uBAAuB,GAExB,MAAM,6BAA6B,CAAA;AAEpC,OAAO,KAAK,GAAG,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAgC/C,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE7C,MAAM,OAAO,aAAa;IAMxB,YACU,mBAAwC,EACxC,qBAA4C,EAC5C,UAAsB,EAC9B,MAAc,EACN,WAAmC;QAJnC,wBAAmB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CAAC,UAAU,CAAC,SAAS,CAAC,WAAW,CAAC,WAAW,EAAE,WAAW,CAAC,CAAA;SACtE;QACD,MAAM,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAA;IACjE,CAAC;CACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare class UserError extends Error {
|
|
2
|
+
constructor(msg?: string);
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Indicates the user associated with the client is already registered.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AlreadyRegisteredError extends UserError {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Indicates the configuration related to Identity Service is not found. This may indicate that Identity Service
|
|
12
|
+
* is not deployed into your runtime instance or the config file that
|
|
13
|
+
* you are using is invalid.
|
|
14
|
+
*/
|
|
15
|
+
export declare class IdentityServiceConfigNotFoundError extends UserError {
|
|
16
|
+
constructor();
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class UserError extends Error {
|
|
2
|
+
constructor(msg) {
|
|
3
|
+
super(msg);
|
|
4
|
+
this.name = this.constructor.name;
|
|
5
|
+
if (Error.captureStackTrace) {
|
|
6
|
+
Error.captureStackTrace(this, this.constructor);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Indicates the user associated with the client is already registered.
|
|
12
|
+
*/
|
|
13
|
+
export class AlreadyRegisteredError extends UserError {
|
|
14
|
+
constructor() {
|
|
15
|
+
super('User is already registered.');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Indicates the configuration related to Identity Service is not found. This may indicate that Identity Service
|
|
20
|
+
* is not deployed into your runtime instance or the config file that
|
|
21
|
+
* you are using is invalid.
|
|
22
|
+
*/
|
|
23
|
+
export class IdentityServiceConfigNotFoundError extends UserError {
|
|
24
|
+
constructor() {
|
|
25
|
+
super('Identity service configuration not found.');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../src/user/error.ts"],"names":[],"mappings":"AAAA,MAAM,SAAU,SAAQ,KAAK;IAC3B,YAAY,GAAY;QACtB,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;QACjC,IAAI,KAAK,CAAC,iBAAiB,EAAE;YAC3B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAA;SAChD;IACH,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,sBAAuB,SAAQ,SAAS;IACnD;QACE,KAAK,CAAC,6BAA6B,CAAC,CAAA;IACtC,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,kCAAmC,SAAQ,SAAS;IAC/D;QACE,KAAK,CAAC,2CAA2C,CAAC,CAAA;IACpD,CAAC;CACF"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Config } from '../core/sdk-config';
|
|
2
|
+
import { AuthenticationTokens } from './user-client-interface';
|
|
3
|
+
import { KeyManager } from '../core/key-manager';
|
|
4
|
+
import { Logger } from '@sudoplatform/sudo-common';
|
|
5
|
+
export interface IdentityProvider {
|
|
6
|
+
/**
|
|
7
|
+
* Signs out the user from all devices.
|
|
8
|
+
*
|
|
9
|
+
* @param accessToken access token used to authorize the request.
|
|
10
|
+
*/
|
|
11
|
+
globalSignOut(accessToken: string): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Registers a new user against the identity provider.
|
|
14
|
+
*
|
|
15
|
+
* @param uid user ID.
|
|
16
|
+
* @param validationData registration parameters.
|
|
17
|
+
* @return user ID
|
|
18
|
+
*/
|
|
19
|
+
register(uid: string, validationData: {
|
|
20
|
+
Name: string;
|
|
21
|
+
Value: string;
|
|
22
|
+
}[]): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Sign into the identity provider using a signing key.
|
|
25
|
+
*
|
|
26
|
+
* @param uid user ID.
|
|
27
|
+
* @param keyId signing key id.
|
|
28
|
+
* @return Successful authentication result AuthenticationTokens
|
|
29
|
+
*/
|
|
30
|
+
signInWithKey(uid: string, keyId: string): Promise<AuthenticationTokens>;
|
|
31
|
+
/**
|
|
32
|
+
* Sign into the identity provider using a token issued by an external
|
|
33
|
+
* authentication provider.
|
|
34
|
+
*
|
|
35
|
+
* @param uid user ID.
|
|
36
|
+
* @param token authentication token.
|
|
37
|
+
* @param type token type.
|
|
38
|
+
* @return Successful authentication result AuthenticationTokens
|
|
39
|
+
*/
|
|
40
|
+
signInWithToken(uid: string, token: string, type: string): Promise<AuthenticationTokens>;
|
|
41
|
+
/**
|
|
42
|
+
* Refresh the access and ID tokens using the refresh token.
|
|
43
|
+
*
|
|
44
|
+
* @param refreshToken refresh token used to refresh the access and ID tokens.
|
|
45
|
+
* @return Successful authentication result AuthenticationTokens containing refreshed tokens
|
|
46
|
+
*/
|
|
47
|
+
refreshTokens(refreshToken: string): Promise<AuthenticationTokens>;
|
|
48
|
+
/**
|
|
49
|
+
* Signs out the user from this device.
|
|
50
|
+
*
|
|
51
|
+
* @param refreshToken refresh token to revoke to sign out this device.
|
|
52
|
+
*/
|
|
53
|
+
signOut(refreshToken: string): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export declare class CognitoUserPoolIdentityProvider implements IdentityProvider {
|
|
56
|
+
private keyManager;
|
|
57
|
+
private config;
|
|
58
|
+
private idpService;
|
|
59
|
+
private refreshTokenLifetime;
|
|
60
|
+
private logger;
|
|
61
|
+
constructor(keyManager: KeyManager, config: Config, logger: Logger);
|
|
62
|
+
private initCognitoIdpService;
|
|
63
|
+
globalSignOut(accessToken: string): Promise<void>;
|
|
64
|
+
register(uid: string, validationData: {
|
|
65
|
+
Name: string;
|
|
66
|
+
Value: string;
|
|
67
|
+
}[]): Promise<string>;
|
|
68
|
+
signInWithKey(userId: string, keyId: string): Promise<AuthenticationTokens>;
|
|
69
|
+
signInWithToken(userId: string, token: string, type: string): Promise<AuthenticationTokens>;
|
|
70
|
+
refreshTokens(refreshToken: string): Promise<AuthenticationTokens>;
|
|
71
|
+
signOut(refreshToken: string): Promise<void>;
|
|
72
|
+
private storeRefreshTokenLifetime;
|
|
73
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { CognitoIdentityProviderClient, GlobalSignOutCommand, InitiateAuthCommand, RespondToAuthChallengeCommand, RevokeTokenCommand, SignUpCommand, } from '@aws-sdk/client-cognito-identity-provider';
|
|
2
|
+
import { apiKeyNames } from '../core/api-key-names';
|
|
3
|
+
import { v4 } from 'uuid';
|
|
4
|
+
import { AuthenticationError, NotAuthorizedError, SignOutError, ServiceError, FatalError, UserNotConfirmedError, } from '@sudoplatform/sudo-common';
|
|
5
|
+
import { AlreadyRegisteredError } from './error';
|
|
6
|
+
export class CognitoUserPoolIdentityProvider {
|
|
7
|
+
constructor(keyManager, config, logger) {
|
|
8
|
+
this.keyManager = keyManager;
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.idpService = this.initCognitoIdpService();
|
|
11
|
+
const refreshTokenLifetime = this.config.identityService.refreshTokenLifetime;
|
|
12
|
+
this.refreshTokenLifetime = refreshTokenLifetime ?? 60;
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
}
|
|
15
|
+
initCognitoIdpService() {
|
|
16
|
+
const idpService = new CognitoIdentityProviderClient({
|
|
17
|
+
region: this.config.identityService.region,
|
|
18
|
+
});
|
|
19
|
+
return idpService;
|
|
20
|
+
}
|
|
21
|
+
async globalSignOut(accessToken) {
|
|
22
|
+
try {
|
|
23
|
+
const params = {
|
|
24
|
+
AccessToken: accessToken,
|
|
25
|
+
};
|
|
26
|
+
const globalSignOut = new GlobalSignOutCommand(params);
|
|
27
|
+
const response = await this.idpService.send(globalSignOut);
|
|
28
|
+
this.logger.info('User successfully signed out.', { response });
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
const error = err;
|
|
32
|
+
throw new SignOutError(error.message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async register(uid, validationData) {
|
|
36
|
+
try {
|
|
37
|
+
const signUp = new SignUpCommand({
|
|
38
|
+
ValidationData: validationData,
|
|
39
|
+
Username: uid,
|
|
40
|
+
Password: `@FF57&Z1)123!-${v4()}`,
|
|
41
|
+
ClientId: this.config.identityService.clientId,
|
|
42
|
+
});
|
|
43
|
+
const response = await this.idpService.send(signUp);
|
|
44
|
+
if (response.UserConfirmed) {
|
|
45
|
+
this.logger.info('User successfully signed up.', { uid });
|
|
46
|
+
return uid;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new UserNotConfirmedError();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
const error = err;
|
|
54
|
+
const errorMsg = error.message;
|
|
55
|
+
if (errorMsg.includes('sudoplatform.ServiceError')) {
|
|
56
|
+
throw new ServiceError(errorMsg);
|
|
57
|
+
}
|
|
58
|
+
else if (errorMsg.includes('sudoplatform.identity.UserValidationFailed') ||
|
|
59
|
+
errorMsg.includes('sudoplatform.identity.TestRegCheckFailed')) {
|
|
60
|
+
throw new NotAuthorizedError(errorMsg);
|
|
61
|
+
}
|
|
62
|
+
else if (errorMsg.includes('sudoplatform.identity.AlreadyRegistered')) {
|
|
63
|
+
throw new AlreadyRegisteredError();
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
throw new FatalError(errorMsg);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async signInWithKey(userId, keyId) {
|
|
71
|
+
try {
|
|
72
|
+
const initiateAuthCommand = new InitiateAuthCommand({
|
|
73
|
+
ClientId: this.config.identityService.clientId,
|
|
74
|
+
AuthFlow: 'CUSTOM_AUTH',
|
|
75
|
+
AuthParameters: {
|
|
76
|
+
USERNAME: userId,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
const initiateAuthResult = await this.idpService.send(initiateAuthCommand);
|
|
80
|
+
const challengeName = initiateAuthResult.ChallengeName;
|
|
81
|
+
const session = initiateAuthResult.Session;
|
|
82
|
+
const params = initiateAuthResult.ChallengeParameters;
|
|
83
|
+
if (params &&
|
|
84
|
+
params['nonce'] &&
|
|
85
|
+
params['audience'] &&
|
|
86
|
+
challengeName &&
|
|
87
|
+
session) {
|
|
88
|
+
const nonce = params['nonce'];
|
|
89
|
+
const audience = params['audience'];
|
|
90
|
+
const answer = await this.keyManager.signJWT(keyId, {
|
|
91
|
+
jti: nonce,
|
|
92
|
+
iss: userId,
|
|
93
|
+
aud: audience,
|
|
94
|
+
sub: userId,
|
|
95
|
+
exp: Math.floor(+new Date() / 1000) + 300,
|
|
96
|
+
});
|
|
97
|
+
const respondToAuthChallenge = new RespondToAuthChallengeCommand({
|
|
98
|
+
Session: session,
|
|
99
|
+
ClientId: this.config.identityService.clientId,
|
|
100
|
+
ChallengeName: challengeName,
|
|
101
|
+
ChallengeResponses: {
|
|
102
|
+
USERNAME: userId,
|
|
103
|
+
ANSWER: answer,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
const respondToAuthChallengeResult = await this.idpService.send(respondToAuthChallenge);
|
|
107
|
+
const authResult = respondToAuthChallengeResult.AuthenticationResult;
|
|
108
|
+
const idToken = authResult?.IdToken;
|
|
109
|
+
const accessToken = authResult?.AccessToken;
|
|
110
|
+
const refreshToken = authResult?.RefreshToken;
|
|
111
|
+
const tokenExpiry = authResult?.ExpiresIn;
|
|
112
|
+
if (idToken && accessToken && refreshToken && tokenExpiry) {
|
|
113
|
+
const authTokens = {
|
|
114
|
+
idToken: idToken,
|
|
115
|
+
accessToken: accessToken,
|
|
116
|
+
refreshToken: refreshToken,
|
|
117
|
+
tokenExpiry: tokenExpiry,
|
|
118
|
+
};
|
|
119
|
+
await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
|
|
120
|
+
return authTokens;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
throw new AuthenticationError('Authentication tokens not found.');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
throw new AuthenticationError('Invalid initiate auth result.');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
const error = err;
|
|
132
|
+
throw new AuthenticationError(error.message);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async signInWithToken(userId, token, type) {
|
|
136
|
+
try {
|
|
137
|
+
const initiateAuth = new InitiateAuthCommand({
|
|
138
|
+
ClientId: this.config.identityService.clientId,
|
|
139
|
+
AuthFlow: 'CUSTOM_AUTH',
|
|
140
|
+
AuthParameters: {
|
|
141
|
+
USERNAME: userId,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
const initiateAuthResult = await this.idpService.send(initiateAuth);
|
|
145
|
+
const challengeName = initiateAuthResult.ChallengeName;
|
|
146
|
+
const session = initiateAuthResult.Session;
|
|
147
|
+
if (challengeName && session) {
|
|
148
|
+
const respondToAuthChallenge = new RespondToAuthChallengeCommand({
|
|
149
|
+
Session: session,
|
|
150
|
+
ClientId: this.config.identityService.clientId,
|
|
151
|
+
ChallengeName: challengeName,
|
|
152
|
+
ChallengeResponses: {
|
|
153
|
+
USERNAME: userId,
|
|
154
|
+
ANSWER: token,
|
|
155
|
+
},
|
|
156
|
+
ClientMetadata: { challengeType: type },
|
|
157
|
+
});
|
|
158
|
+
const respondToAuthChallengeResult = await this.idpService.send(respondToAuthChallenge);
|
|
159
|
+
const authResult = respondToAuthChallengeResult.AuthenticationResult;
|
|
160
|
+
const idToken = authResult?.IdToken;
|
|
161
|
+
const accessToken = authResult?.AccessToken;
|
|
162
|
+
const refreshToken = authResult?.RefreshToken;
|
|
163
|
+
const tokenExpiry = authResult?.ExpiresIn;
|
|
164
|
+
if (idToken && accessToken && refreshToken && tokenExpiry) {
|
|
165
|
+
const authTokens = {
|
|
166
|
+
idToken: idToken,
|
|
167
|
+
accessToken: accessToken,
|
|
168
|
+
refreshToken: refreshToken,
|
|
169
|
+
tokenExpiry: tokenExpiry,
|
|
170
|
+
};
|
|
171
|
+
await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
|
|
172
|
+
return authTokens;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
throw new AuthenticationError('Authentication tokens not found.');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
throw new AuthenticationError('Invalid initiate auth result.');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
const error = err;
|
|
184
|
+
throw new AuthenticationError(error.message);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async refreshTokens(refreshToken) {
|
|
188
|
+
try {
|
|
189
|
+
const initiateAuth = new InitiateAuthCommand({
|
|
190
|
+
ClientId: this.config.identityService.clientId,
|
|
191
|
+
AuthFlow: 'REFRESH_TOKEN_AUTH',
|
|
192
|
+
AuthParameters: {
|
|
193
|
+
REFRESH_TOKEN: refreshToken,
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
const initiateAuthResult = await this.idpService.send(initiateAuth);
|
|
197
|
+
const idToken = initiateAuthResult.AuthenticationResult?.IdToken;
|
|
198
|
+
const accessToken = initiateAuthResult.AuthenticationResult?.AccessToken;
|
|
199
|
+
const tokenExpiry = initiateAuthResult.AuthenticationResult?.ExpiresIn;
|
|
200
|
+
if (idToken && accessToken && tokenExpiry) {
|
|
201
|
+
const authTokens = {
|
|
202
|
+
idToken: idToken,
|
|
203
|
+
accessToken: accessToken,
|
|
204
|
+
refreshToken: refreshToken,
|
|
205
|
+
tokenExpiry: tokenExpiry,
|
|
206
|
+
};
|
|
207
|
+
await this.storeRefreshTokenLifetime(this.refreshTokenLifetime);
|
|
208
|
+
return authTokens;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
throw new AuthenticationError('Authentication tokens not found.');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
const error = err;
|
|
216
|
+
throw new AuthenticationError(error.message);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async signOut(refreshToken) {
|
|
220
|
+
try {
|
|
221
|
+
const params = {
|
|
222
|
+
Token: refreshToken,
|
|
223
|
+
ClientId: this.config.identityService.clientId,
|
|
224
|
+
};
|
|
225
|
+
const revokeToken = new RevokeTokenCommand(params);
|
|
226
|
+
const response = await this.idpService.send(revokeToken);
|
|
227
|
+
this.logger.info('User successfully signed out.', { response });
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
const error = err;
|
|
231
|
+
this.logger.error(error.message);
|
|
232
|
+
throw new SignOutError(error.message);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
async storeRefreshTokenLifetime(refreshTokenLifetime) {
|
|
236
|
+
const tokenLifetime = refreshTokenLifetime * 24 * 60 * 60 * 1000 + new Date().getTime();
|
|
237
|
+
await this.keyManager.removeItem(apiKeyNames.refreshTokenExpiry);
|
|
238
|
+
await this.keyManager.addString(apiKeyNames.refreshTokenExpiry, tokenLifetime.toString());
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=identity-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity-provider.js","sourceRoot":"","sources":["../../../src/user/identity-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6BAA6B,EAC7B,oBAAoB,EACpB,mBAAmB,EACnB,6BAA6B,EAC7B,kBAAkB,EAClB,aAAa,GACd,MAAM,2CAA2C,CAAA;AAElD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGnD,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAA;AACzB,OAAO,EACL,mBA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