【问题标题】:AWS Cognito Change user status to disableAWS Cognito 将用户状态更改为禁用
【发布时间】:2018-08-09 07:21:54
【问题描述】:

我想使用代码更改用户状态。

我尝试了很多代码,但没有一个对我有用。任何人都可以提供完整的工作示例。有时我收到此错误 CognitoIdentityCredentials is not authorized to perform: cognito-idp:AdminDisableUser on resource

var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();


AWS.config.update({
    region: 'us-west-2',
    credentials: new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'us-west-2:6afd2a7c-b3cd-472f-bead-fdbde8a84a26',
    })
});


var params = {
    UserPoolId: 'us-west-2_Klsadmic5', /* required */
    Username: 'alphagate6' /* required */
};
cognitoidentityserviceprovider.adminDisableUser(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
});

【问题讨论】:

  • 我不需要在 AWS.config 中设置 IdentityPoolId

标签: javascript amazon-web-services aws-sdk aws-cognito


【解决方案1】:

参数和调用似乎没问题。该错误意味着您的 lambda 函数的角色(我假设此代码 sn-p 来自您的 lambda 函数)没有执行 adminDisableUser 的权限。

您需要找到您的 lambda 函数的 IAM 角色并附加一个允许此操作的策略。例如:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowAdminDisableUser",
            "Effect": "Allow",
            "Action": "cognito-idp:AdminDisableUser",
            "Resource": "*"
        }
    ]
}

您可能还希望指定某些资源以不允许对每个用户池执行此操作。

【讨论】:

  • 这类似于我在 serverless.yml(无服务器框架)中设置它的方式
【解决方案2】:

我有这个功能可以在一个应用程序中启用/禁用用户,这里是我如何实现该功能

  1. 授予 lambda 执行启用/禁用所需的权限
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "cognito-idp:AdminEnableUser",
                "cognito-idp:AdminDisableUser"
            ],
            "Resource": [
                "arn:aws:cognito-idp:<region>:<account-id>:userpool/<user-pool-id>",
                "arn:aws:cognito-idp:<region>:<account-id>:userpool/<user-pool-id>"
            ]
        }
    ]
}
  1. 初始化 AWS 开发工具包
AWS.config.update({
  region: "us-east-1",
});
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({
  apiVersion: "2016-04-18",
});
const UserPoolId = "<pool-id>";

  1. 创建了一种方法来处理启用/禁用
const accountActions = (action, username) => {
  return new Promise((res, rej) => {
    const params = {
      UserPoolId /* required */,
      Username: username /* required */,
    };
    if (action == "disable") {
      cognitoidentityserviceprovider.adminDisableUser(params, function(err,data) {
        if (err) {
          rej(err);
        } else {
          res(data);
        }
      });
    } else {
      cognitoidentityserviceprovider.adminEnableUser(params,function(err,data) {
        if (err) {
          rej(err);
        } else {
          res(data);
        }
      });
    }
  });
};

【讨论】:

    【解决方案3】:

    对于任何试图通过 Amplify 执行此操作的人。

    编辑"AmplifyResourcesPolicy"(对于我的项目,它位于cloudformation-template.json

    "AmplifyResourcesPolicy": {
        "DependsOn": ["LambadExecutionRole"],
        "Type": "AWS::IAM::Policy",
        ...
        "PolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Action": [
                        "cognito-idp:AdminDisableUser",
                        "cognito-idp:AdminEnableUser",
                    ],
                    "Resource": [
                        "Fn:Join": [
                            "arn:aws:cognito-idp:",
                            {
                                "Ref": "AWS::Region"
                            },
                            ":",
                            {
                                "Ref": "AWS::AccountId"
                            },
                            ":userpool/",
                            {
                                "Ref": "<user-pool-id>"
                            }
                        ]
                    ]
                }
            ]
        }
    

    【讨论】:

      【解决方案4】:

      如果您的目标是作为管理员启用/禁用 cognito 用户,请忽略这一点。 请注意,管理员启用/禁用以管理员身份启用/禁用用户。 不禁用您可能从中得到错误的用户帐户(登录等)。

      【讨论】:

      • 这不成立。我可以使用具有相应权限的另一个用户(在 lambda 角色策略中设置)禁用用户池中的任何用户。禁用用户后,他无法登录并执行任何操作。
      猜你喜欢
      • 2021-11-28
      • 2016-04-09
      • 2022-10-07
      • 1970-01-01
      • 2020-07-31
      • 2019-10-11
      • 2022-11-07
      • 2021-10-22
      • 2019-01-03
      相关资源
      最近更新 更多