triangle-utils 1.4.24 → 1.4.26
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/dist/src/UtilsCognito.d.ts +1 -0
- package/dist/src/UtilsCognito.js +27 -5
- package/package.json +1 -1
- package/src/UtilsCognito.ts +27 -5
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare class UtilsCognito {
|
|
2
2
|
private readonly cognito;
|
|
3
3
|
constructor(region: string);
|
|
4
|
+
get_username(token: string): Promise<string | undefined>;
|
|
4
5
|
admin_get_user(user_pool_id: string, username: string): Promise<import("@aws-sdk/client-cognito-identity-provider").AdminGetUserCommandOutput | undefined>;
|
|
5
6
|
admin_create_user(user_pool_id: string, username: string, user_id: string): Promise<Error | import("@aws-sdk/client-cognito-identity-provider").AdminCreateUserCommandOutput>;
|
|
6
7
|
admin_reset_password(user_pool_id: string, username: string): Promise<Error | import("@aws-sdk/client-cognito-identity-provider").AdminCreateUserCommandOutput>;
|
package/dist/src/UtilsCognito.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import { CognitoIdentityProvider
|
|
1
|
+
import { CognitoIdentityProvider } from "@aws-sdk/client-cognito-identity-provider";
|
|
2
2
|
export class UtilsCognito {
|
|
3
3
|
cognito;
|
|
4
4
|
constructor(region) {
|
|
5
5
|
this.cognito = new CognitoIdentityProvider({ region: region });
|
|
6
6
|
}
|
|
7
|
+
async get_username(token) {
|
|
8
|
+
try {
|
|
9
|
+
const user_info = await this.cognito.getUser({
|
|
10
|
+
AccessToken: token
|
|
11
|
+
});
|
|
12
|
+
const username = user_info.Username;
|
|
13
|
+
return username;
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
7
19
|
async admin_get_user(user_pool_id, username) {
|
|
8
20
|
try {
|
|
9
21
|
return await this.cognito.adminGetUser({
|
|
@@ -62,14 +74,24 @@ export class UtilsCognito {
|
|
|
62
74
|
return undefined;
|
|
63
75
|
}
|
|
64
76
|
const user_attribute = user_attributes.filter(attribute => attribute.Name === "preferred_username")[0];
|
|
77
|
+
if (user_attribute === undefined) {
|
|
78
|
+
const user_id = crypto.randomUUID();
|
|
79
|
+
await this.cognito.updateUserAttributes({
|
|
80
|
+
AccessToken: token,
|
|
81
|
+
UserAttributes: [
|
|
82
|
+
{
|
|
83
|
+
Name: "preferred_username",
|
|
84
|
+
Value: user_id
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
});
|
|
88
|
+
return user_id;
|
|
89
|
+
}
|
|
65
90
|
const user_id = user_attribute.Value;
|
|
66
91
|
return user_id;
|
|
67
92
|
}
|
|
68
93
|
catch (error) {
|
|
69
|
-
|
|
70
|
-
return undefined;
|
|
71
|
-
}
|
|
72
|
-
throw error;
|
|
94
|
+
return undefined;
|
|
73
95
|
}
|
|
74
96
|
}
|
|
75
97
|
async get_new_token(client_id, client_secret, refresh_token) {
|
package/package.json
CHANGED
package/src/UtilsCognito.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CognitoIdentityProvider, NotAuthorizedException } from "@aws-sdk/client-cognito-identity-provider"
|
|
1
|
+
import { CognitoIdentityProvider, NotAuthorizedException, UserNotFoundException } from "@aws-sdk/client-cognito-identity-provider"
|
|
2
2
|
|
|
3
3
|
export class UtilsCognito {
|
|
4
4
|
|
|
@@ -8,6 +8,18 @@ export class UtilsCognito {
|
|
|
8
8
|
this.cognito = new CognitoIdentityProvider({ region : region })
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
async get_username(token : string) {
|
|
12
|
+
try {
|
|
13
|
+
const user_info = await this.cognito.getUser({
|
|
14
|
+
AccessToken : token
|
|
15
|
+
})
|
|
16
|
+
const username = user_info.Username
|
|
17
|
+
return username
|
|
18
|
+
} catch (error) {
|
|
19
|
+
return undefined
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
11
23
|
async admin_get_user(user_pool_id : string, username : string) {
|
|
12
24
|
try {
|
|
13
25
|
return await this.cognito.adminGetUser({
|
|
@@ -66,13 +78,23 @@ export class UtilsCognito {
|
|
|
66
78
|
return undefined
|
|
67
79
|
}
|
|
68
80
|
const user_attribute = user_attributes.filter(attribute => attribute.Name === "preferred_username")[0]
|
|
81
|
+
if (user_attribute === undefined) {
|
|
82
|
+
const user_id = crypto.randomUUID()
|
|
83
|
+
await this.cognito.updateUserAttributes({
|
|
84
|
+
AccessToken : token,
|
|
85
|
+
UserAttributes : [
|
|
86
|
+
{
|
|
87
|
+
Name : "preferred_username",
|
|
88
|
+
Value : user_id
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
})
|
|
92
|
+
return user_id
|
|
93
|
+
}
|
|
69
94
|
const user_id = user_attribute.Value
|
|
70
95
|
return user_id
|
|
71
96
|
} catch (error) {
|
|
72
|
-
|
|
73
|
-
return undefined
|
|
74
|
-
}
|
|
75
|
-
throw error
|
|
97
|
+
return undefined
|
|
76
98
|
}
|
|
77
99
|
}
|
|
78
100
|
|