Я нашел решение, которое по сути входит в систему как пользователь, чтобы получить Cognito Identity ID:
import { config, CognitoIdentityServiceProvider, CognitoIdentity } from 'aws-sdk';
config.update({
region: process.env.REGION,
apiVersions: {
cognitoidentityserviceprovider: '2016-04-18',
cognitoidentity: '2014-06-30',
// other service API versions
}
});
export async function createUser(name: string, given_name: string, family_name: string, phone_number: string) {
var cognitoidentityserviceprovider = new CognitoIdentityServiceProvider();
var params = {
UserPoolId: process.env.USER_POOL_ID,
Username: phone_number, /* required */
MessageAction: "SUPPRESS",
UserAttributes: [
{
Name: 'name', /* required */
Value: name
},
{
Name: 'given_name', /* required */
Value: given_name
},
{
Name: 'family_name', /* required */
Value: family_name
},
{
Name: 'phone_number', /* required */
Value: phone_number
},
{
Name: "phone_number_verified",
Value: "true"
}
]
};
console.log("adminCreateUser")
let adminCreateUserResonse: CognitoIdentityServiceProvider.AdminCreateUserResponse = await new Promise(resolve => {
cognitoidentityserviceprovider.adminCreateUser(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
console.log("adminCreateUser complete")
resolve(data)
}
})
});
let identity: CognitoIdentity.GetIdResponse = await new Promise(resolve => {
setPassword(phone_number, resolve)
});
let user_attributes = adminCreateUserResonse.User.Attributes
let sub = user_attributes.find(a => a.Name == "sub")
}
function setPassword(phone_number: string, resolve: any) {
var cognitoidentityserviceprovider = new CognitoIdentityServiceProvider();
let password = generator.generate({
length: 32,
numbers: true,
symbols: true,
lowercase: true,
uppercase: true
});
var setPasswordParams = {
Password: password, /* required */
UserPoolId: process.env.USER_POOL_ID, /* required */
Username: phone_number, /* required */
Permanent: true
};
console.log("adminSetUserPassword")
cognitoidentityserviceprovider.adminSetUserPassword(setPasswordParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
console.log("adminSetUserPassword complete")
initiateAuth(phone_number, password, resolve)
}
})
}
function initiateAuth(phone_number: string, password: string, resolve: any) {
var cognitoidentityserviceprovider = new CognitoIdentityServiceProvider();
let initiateAuthParams = {
AuthFlow: "ADMIN_USER_PASSWORD_AUTH",
AuthParameters: {
"USERNAME" : phone_number,
"PASSWORD" : password
},
UserPoolId: process.env.USER_POOL_ID, /* required */
ClientId: process.env.CLIENT_ID
}
console.log("adminInitiateAuth")
cognitoidentityserviceprovider.adminInitiateAuth(initiateAuthParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
console.log("adminInitiateAuth complete")
getId(data, resolve)
}
})
}
function getId(data: CognitoIdentityServiceProvider.AdminInitiateAuthResponse, resolve: any) {
const cognitoidentity = new CognitoIdentity()
let login_provider = "cognito-idp." + process.env.REGION + ".amazonaws.com/" + process.env.USER_POOL_ID
console.log("login_provider: " + login_provider)
let getIdParams = {
"AccountId": process.env.AWS_ACCOUNT_ID,
"IdentityPoolId": process.env.IDENTITY_POOL_ID,
"Logins": {}
}
getIdParams.Logins[login_provider] = data.AuthenticationResult.IdToken
console.log("getId")
cognitoidentity.getId(getIdParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
console.log("getId complete")
resolve(data)
}
})
}