@juniyadi/cognito 0.5.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -21,6 +21,17 @@ interface ICognitoInviteUser {
21
21
  attributes?: ICognitoAttributes[];
22
22
  group?: string;
23
23
  }
24
+ interface ICognitoAdminLoginResponse {
25
+ TokenType: string;
26
+ ExpiresIn: number;
27
+ AccessToken: string;
28
+ IdToken: string;
29
+ RefreshToken: string;
30
+ NewDeviceMetadata: {
31
+ DeviceKey: string | null;
32
+ DeviceGroupKey: null;
33
+ };
34
+ }
24
35
  declare class Cognito {
25
36
  region: string;
26
37
  userPoolId: string;
@@ -121,6 +132,27 @@ declare class Cognito {
121
132
  * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminAddUserToGroup.html
122
133
  */
123
134
  addUserToGroup: (username: string, groupName: string) => Promise<AdminAddUserToGroupCommandOutput>;
135
+ /**
136
+ * Login as an admin to Cognito User Pool with username and password
137
+ *
138
+ * @param username string
139
+ * @param password string
140
+ * @returns Promise<ICognitoAdminLoginResponse>
141
+ *
142
+ * @example
143
+ * const cognito = new Cognito();
144
+ * const response = await cognito.adminLogin("username", "password");
145
+ * console.log(response);
146
+ */
147
+ adminLogin: (username: string, password: string) => Promise<ICognitoAdminLoginResponse>;
148
+ /**
149
+ * Refresh Token for Admin User
150
+ *
151
+ * @param refreshToken string
152
+ * @param deviceKey string
153
+ * @returns Promise<AdminInitiateAuthCommandOutput>
154
+ */
155
+ adminRefreshToken: (refreshToken: string, deviceKey: string) => Promise<_aws_sdk_client_cognito_identity_provider.AdminInitiateAuthCommandOutput>;
124
156
  }
125
157
 
126
- export { Cognito, ICognito, ICognitoAttributes, ICognitoInviteUser };
158
+ export { Cognito, type ICognito, type ICognitoAdminLoginResponse, type ICognitoAttributes, type ICognitoInviteUser };
package/dist/index.js CHANGED
@@ -1,8 +1,14 @@
1
1
  'use strict';
2
2
 
3
3
  var clientCognitoIdentityProvider = require('@aws-sdk/client-cognito-identity-provider');
4
+ var amazonCognitoIdentityJs = require('amazon-cognito-identity-js');
4
5
  var randomString = require('@juniyadi/random-string');
6
+ var m = require('dayjs');
5
7
 
6
- var s=class{constructor(e){this.randomPassword=(e=12)=>randomString.generate(e,{lowercase:!0,uppercase:!0,numbers:!0});this.inviteUser=async e=>{let r=e.password||this.randomPassword(10),i=e.passwordTemporary||this.randomPassword(12),t={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:i};e.phoneNumber&&t.UserAttributes&&t.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&t.UserAttributes&&(t.UserAttributes=t.UserAttributes.concat(e.attributes));let o=new clientCognitoIdentityProvider.AdminCreateUserCommand(t);try{let n=await this.cognitoIdentityProvider.send(o);return e.autoConfirm&&n?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,r),e.group&&await this.addUserToGroup(e.email,e.group),n}catch(n){throw n}};this.changePassword=async(e,r)=>{let i=new clientCognitoIdentityProvider.AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new clientCognitoIdentityProvider.AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.enableUser=async e=>{let r=new clientCognitoIdentityProvider.AdminEnableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new clientCognitoIdentityProvider.AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new clientCognitoIdentityProvider.ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};this.addUserToGroup=async(e,r)=>{let i=new clientCognitoIdentityProvider.AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(i)};if(this.region=e?.region||process.env.AWS_REGION||"",this.userPoolId=e?.userPoolId||process.env.COGNITO_USER_POOL_ID||"",this.clientId=e?.clientId||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new clientCognitoIdentityProvider.CognitoIdentityProviderClient({region:this.region});}};
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
9
 
8
- exports.Cognito = s;
10
+ var m__default = /*#__PURE__*/_interopDefault(m);
11
+
12
+ var u=class{constructor(e){this.randomPassword=(e=12)=>randomString.generate(e,{lowercase:!0,uppercase:!0,numbers:!0});this.inviteUser=async e=>{let n=e.password||this.randomPassword(10),r=e.passwordTemporary||this.randomPassword(12),t={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:r};e.phoneNumber&&t.UserAttributes&&t.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&t.UserAttributes&&(t.UserAttributes=t.UserAttributes.concat(e.attributes));let s=new clientCognitoIdentityProvider.AdminCreateUserCommand(t);try{let i=await this.cognitoIdentityProvider.send(s);return e.autoConfirm&&i?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,n),e.group&&await this.addUserToGroup(e.email,e.group),i}catch(i){throw i}};this.changePassword=async(e,n)=>{let r=new clientCognitoIdentityProvider.AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:n,Permanent:!0});return this.cognitoIdentityProvider.send(r)};this.disableUser=async e=>{let n=new clientCognitoIdentityProvider.AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(n)};this.enableUser=async e=>{let n=new clientCognitoIdentityProvider.AdminEnableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(n)};this.deleteUser=async e=>{await this.disableUser(e);let n=new clientCognitoIdentityProvider.AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(n)};this.listUsers=async e=>{let n=new clientCognitoIdentityProvider.ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(n)};this.addUserToGroup=async(e,n)=>{let r=new clientCognitoIdentityProvider.AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:n});return this.cognitoIdentityProvider.send(r)};this.adminLogin=async(e,n)=>new Promise((r,t)=>{let s=new amazonCognitoIdentityJs.AuthenticationDetails({Username:e,Password:n}),i=new amazonCognitoIdentityJs.CognitoUserPool({UserPoolId:this.userPoolId,ClientId:this.clientId}),a=new amazonCognitoIdentityJs.CognitoUser({Username:e,Pool:i});a.authenticateUser(s,{onSuccess:o=>{a.getDevice({onSuccess:d=>{let c=o.getIdToken().getExpiration(),l=m__default.default.unix(c).diff(m__default.default(),"second")+1;r({TokenType:"Bearer",ExpiresIn:l,AccessToken:o.getAccessToken().getJwtToken(),IdToken:o.getIdToken().getJwtToken(),RefreshToken:o.getRefreshToken().getToken(),NewDeviceMetadata:{DeviceKey:d?.Device?.DeviceKey,DeviceGroupKey:null}});},onFailure:d=>{t(d);}});},onFailure:o=>{t(o);}});});this.adminRefreshToken=async(e,n)=>{let r=new clientCognitoIdentityProvider.AdminInitiateAuthCommand({UserPoolId:this.userPoolId,ClientId:this.clientId,AuthFlow:"REFRESH_TOKEN_AUTH",AuthParameters:{REFRESH_TOKEN:e,DEVICE_KEY:n}});return this.cognitoIdentityProvider.send(r)};if(this.region=e?.region||process.env.AWS_REGION||"",this.userPoolId=e?.userPoolId||process.env.COGNITO_USER_POOL_ID||"",this.clientId=e?.clientId||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new clientCognitoIdentityProvider.CognitoIdentityProviderClient({region:this.region});}};
13
+
14
+ exports.Cognito = u;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juniyadi/cognito",
3
- "version": "0.5.1",
3
+ "version": "1.0.1",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
@@ -28,18 +28,20 @@
28
28
  "devDependencies": {
29
29
  "@aws-sdk/client-cognito-identity": "^3.388.0",
30
30
  "@aws-sdk/client-cognito-identity-provider": "^3.388.0",
31
- "@types/jest": "^29.5.3",
32
- "@types/node": "^20.5.0",
31
+ "@types/jest": "^29.5.12",
32
+ "@types/node": "^20.12.8",
33
+ "amazon-cognito-identity-js": "^6.3.12",
33
34
  "aws-sdk-client-mock": "^3.0.0",
34
- "eslint": "^8.47.0",
35
- "jest": "^29.6.2",
36
- "ts-jest": "^29.1.1",
37
- "tsup": "^7.2.0",
38
- "typedoc": "^0.24.8",
39
- "typescript": "^5.1.6",
35
+ "dayjs": "^1.11.11",
36
+ "eslint": "^8.57.0",
37
+ "jest": "^29.7.0",
38
+ "ts-jest": "^29.1.2",
39
+ "tsup": "^8.0.2",
40
+ "typedoc": "^0.25.13",
41
+ "typescript": "^5.4.5",
40
42
  "@juniyadi/config-typedoc": "0.0.0",
41
- "@juniyadi/random-string": "0.1.0",
42
43
  "@juniyadi/tsconfig": "0.0.0",
44
+ "@juniyadi/random-string": "0.2.0",
43
45
  "eslint-config-juniyadi": "0.0.1"
44
46
  },
45
47
  "publishConfig": {
@@ -48,7 +50,9 @@
48
50
  "peerDependencies": {
49
51
  "@aws-sdk/client-cognito-identity": "^3.388.0",
50
52
  "@aws-sdk/client-cognito-identity-provider": "^3.388.0",
51
- "@juniyadi/random-string": "0.1.0"
53
+ "amazon-cognito-identity-js": "^6.3.12",
54
+ "dayjs": "^1.11.11",
55
+ "@juniyadi/random-string": "0.2.0"
52
56
  },
53
57
  "scripts": {
54
58
  "docs": "typedoc --out docs src/index.ts",
package/dist/index.d.mts DELETED
@@ -1,126 +0,0 @@
1
- import * as _aws_sdk_client_cognito_identity_provider from '@aws-sdk/client-cognito-identity-provider';
2
- import { CognitoIdentityProviderClient, AdminSetUserPasswordCommandOutput, AdminDisableUserCommandOutput, AdminEnableUserCommandOutput, AdminDeleteUserCommandOutput, AdminAddUserToGroupCommandOutput } from '@aws-sdk/client-cognito-identity-provider';
3
-
4
- interface ICognito {
5
- region?: string;
6
- userPoolId?: string;
7
- clientId?: string;
8
- }
9
- interface ICognitoAttributes {
10
- Name: string;
11
- Value: string;
12
- }
13
- interface ICognitoInviteUser {
14
- name: string;
15
- email: string;
16
- password?: string;
17
- passwordTemporary?: string;
18
- autoConfirm?: boolean;
19
- phoneNumber?: string;
20
- sendEmail?: boolean;
21
- attributes?: ICognitoAttributes[];
22
- group?: string;
23
- }
24
- declare class Cognito {
25
- region: string;
26
- userPoolId: string;
27
- clientId: string;
28
- cognitoIdentityProvider: CognitoIdentityProviderClient;
29
- /**
30
- * Create a new Cognito instance
31
- * @param opts
32
- * @example
33
- * const cognito = new Cognito({
34
- * region: "us-east-1",
35
- * userPoolId: "us-east-1_123456789",
36
- * clientId: "12345678901234567890",
37
- * });
38
- */
39
- constructor(opts?: ICognito);
40
- /**
41
- * Generate a random password of a given length
42
- * @param length
43
- * @returns string
44
- * @example
45
- * const password = randomPassword(12);
46
- * console.log(password);
47
- */
48
- randomPassword: (length?: number) => string;
49
- /**
50
- * Invite a user to the Cognito User Pool
51
- * @param opts
52
- */
53
- inviteUser: (opts: ICognitoInviteUser) => Promise<_aws_sdk_client_cognito_identity_provider.AdminCreateUserCommandOutput>;
54
- /**
55
- * Change user password in Cognito User Pool
56
- * @param username - Username of the user
57
- * @param password - New password for the user
58
- * @returns Promise<AdminSetUserPasswordCommandOutput>
59
- * @example
60
- * const cognito = new Cognito();
61
- * const users = await cognito.changePassword("johndoe@example.com", "newPassword");
62
- * console.log(users.Users);
63
- * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminSetUserPassword.html
64
- */
65
- changePassword: (username: string, password: string) => Promise<AdminSetUserPasswordCommandOutput>;
66
- /**
67
- * Disable a user in Cognito User Pool
68
- * @param username - Username of the user
69
- * @returns Promise<AdminDisableUserCommandOutput>
70
- * @example
71
- * const cognito = new Cognito();
72
- * const users = await cognito.disableUser("johndoe@example.com");
73
- * console.log(users.Users);
74
- * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDisableUser.html
75
- */
76
- disableUser: (username: string) => Promise<AdminDisableUserCommandOutput>;
77
- /**
78
- * Enable a user in Cognito User Pool
79
- * @param username - Username of the user
80
- * @returns Promise<AdminEnableUserCommandOutput>
81
- * @example
82
- * const cognito = new Cognito();
83
- * const users = await cognito.enableUser("johndoe@example.com");
84
- * console.log(users.Users);
85
- *
86
- * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminEnableUser.html
87
- */
88
- enableUser: (username: string) => Promise<AdminEnableUserCommandOutput>;
89
- /**
90
- * Delete a user in Cognito User Pool
91
- * @param username - Username of the user
92
- * @returns Promise<AdminDeleteUserCommandOutput>
93
- * @example
94
- * const cognito = new Cognito();
95
- * const users = await cognito.deleteUser("johndoe@example.com");
96
- * console.log(users.Users);
97
- * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDeleteUser.html
98
- */
99
- deleteUser: (username: string) => Promise<AdminDeleteUserCommandOutput>;
100
- /**
101
- * List for users in Cognito User Pool
102
- * @param opts
103
- * @returns Promise<ListUsersCommandOutput>
104
- * @example
105
- * const cognito = new Cognito();
106
- * const users = await cognito.listUsers({ email: "johndoe@example.com" });
107
- * console.log(users.Users);
108
- * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html
109
- */
110
- listUsers: (filter?: string) => Promise<_aws_sdk_client_cognito_identity_provider.ListUsersCommandOutput>;
111
- /**
112
- * Add a user to a group in Cognito User Pool
113
- * @param username - Username of the user
114
- * @param groupName - Name of the group
115
- * @returns Promise<AdminAddUserToGroupCommandOutput>
116
- * @example
117
- * const cognito = new Cognito();
118
- * const users = await cognito.addUserToGroup("johndoe@example.com", "Admin");
119
- * console.log(users.Users);
120
- *
121
- * @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminAddUserToGroup.html
122
- */
123
- addUserToGroup: (username: string, groupName: string) => Promise<AdminAddUserToGroupCommandOutput>;
124
- }
125
-
126
- export { Cognito, ICognito, ICognitoAttributes, ICognitoInviteUser };
package/dist/index.mjs DELETED
@@ -1,6 +0,0 @@
1
- import { AdminCreateUserCommand, AdminSetUserPasswordCommand, AdminDisableUserCommand, AdminEnableUserCommand, AdminDeleteUserCommand, ListUsersCommand, AdminAddUserToGroupCommand, CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
2
- import { generate } from '@juniyadi/random-string';
3
-
4
- var s=class{constructor(e){this.randomPassword=(e=12)=>generate(e,{lowercase:!0,uppercase:!0,numbers:!0});this.inviteUser=async e=>{let r=e.password||this.randomPassword(10),i=e.passwordTemporary||this.randomPassword(12),t={UserPoolId:this.userPoolId,Username:e.email,UserAttributes:[{Name:"email",Value:e.email},{Name:"name",Value:e.name},{Name:"email_verified",Value:"true"}],DesiredDeliveryMediums:["EMAIL"],ForceAliasCreation:!1,MessageAction:e.sendEmail?"RESEND":"SUPPRESS",TemporaryPassword:i};e.phoneNumber&&t.UserAttributes&&t.UserAttributes.push({Name:"phone_number",Value:e.phoneNumber}),e.attributes&&t.UserAttributes&&(t.UserAttributes=t.UserAttributes.concat(e.attributes));let o=new AdminCreateUserCommand(t);try{let n=await this.cognitoIdentityProvider.send(o);return e.autoConfirm&&n?.User?.UserStatus==="FORCE_CHANGE_PASSWORD"&&await this.changePassword(e.email,r),e.group&&await this.addUserToGroup(e.email,e.group),n}catch(n){throw n}};this.changePassword=async(e,r)=>{let i=new AdminSetUserPasswordCommand({UserPoolId:this.userPoolId,Username:e,Password:r,Permanent:!0});return this.cognitoIdentityProvider.send(i)};this.disableUser=async e=>{let r=new AdminDisableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.enableUser=async e=>{let r=new AdminEnableUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.deleteUser=async e=>{await this.disableUser(e);let r=new AdminDeleteUserCommand({UserPoolId:this.userPoolId,Username:e});return this.cognitoIdentityProvider.send(r)};this.listUsers=async e=>{let r=new ListUsersCommand({UserPoolId:this.userPoolId,Filter:e||void 0});return this.cognitoIdentityProvider.send(r)};this.addUserToGroup=async(e,r)=>{let i=new AdminAddUserToGroupCommand({UserPoolId:this.userPoolId,Username:e,GroupName:r});return this.cognitoIdentityProvider.send(i)};if(this.region=e?.region||process.env.AWS_REGION||"",this.userPoolId=e?.userPoolId||process.env.COGNITO_USER_POOL_ID||"",this.clientId=e?.clientId||process.env.COGNITO_CLIENT_ID||"",!this.region)throw new Error("AWS Region is required");if(!this.userPoolId)throw new Error("Cognito User Pool ID is required");if(!this.clientId)throw new Error("Cognito Client ID is required");this.cognitoIdentityProvider=new CognitoIdentityProviderClient({region:this.region});}};
5
-
6
- export { s as Cognito };