【问题标题】:AWS Cognito user migration lambda in python, how to return custom error messagepython中的AWS Cognito用户迁移lambda,如何返回自定义错误消息
【发布时间】:2018-12-17 18:32:28
【问题描述】:

在用户迁移期间,我想返回“用户名或密码不正确”。作为错误消息而不是“用户不存在”

已经在google上搜索了一段时间,找不到如何在本文档中复制以下JS示例

https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-migrate-user.html

exports.handler = (event, context, callback) => {

    var user;

    if ( event.triggerSource == "UserMigration_Authentication" ) {

        // authenticate the user with your existing user directory service
        user = authenticateUser(event.userName, event.request.password);
        if ( user ) {
            event.response.userAttributes = {
                "email": user.emailAddress,
                "email_verified": "true"
            };
            event.response.finalUserStatus = "CONFIRMED";
            event.response.messageAction = "SUPPRESS";
            context.succeed(event);
        }
        else {
            // Return error to Amazon Cognito
            callback("Bad password");
        }
    }
    else if ( event.triggerSource == "UserMigration_ForgotPassword" ) {

        // Lookup the user in your existing user directory service
        user = lookupUser(event.userName);
        if ( user ) {
            event.response.userAttributes = {
                "email": user.emailAddress,
                // required to enable password-reset code to be sent to user
                "email_verified": "true"  
            };
            event.response.messageAction = "SUPPRESS";
            context.succeed(event);
        }
        else {
            // Return error to Amazon Cognito
            callback("Bad password");
        }
    }
    else { 
        // Return error to Amazon Cognito
        callback("Bad triggerSource " + event.triggerSource);
    }
};

它在 nodejs 中使用callback('message'),但我不知道如何在 Python 中做到这一点。

突然想到这个问题

I can't find callback parameter in python lambda handler

尝试返回消息字符串,但得到“用户迁移期间的异常”

【问题讨论】:

  • 除了使用自定义 UI / 抛出异常之外,您有没有想过这个问题?我以与您拥有的默认示例类似的方式使用节点,并且回调将错误消息记录到 cloudwatch,但仍返回通用的“用户迁移期间的异常”。我想也许我可以做 context.fail('something'); ...但这也只是记录在日志中。
  • 是否有任何解决方案(用于托管 UI)
  • 没有托管 UI 的解决方案。
  • @james-lin 你能把你的代码放在 Python 中吗?
  • @hernan 我不再为那家公司工作,所以我没有代码。

标签: python amazon-web-services


【解决方案1】:

尝试引发 ValueError:

raise ValueError("My custom message")

对我来说,这改变了从返回给 boto 客户端的异常的措辞

An error occurred (UserNotFoundException) when calling the AdminInitiateAuth operation: Exception migrating user in app client aeiouffhfha

到更有用的地方

An error occurred (UserNotFoundException) when calling the AdminInitiateAuth operation: UserMigration failed with error My custom message.

【讨论】:

    【解决方案2】:

    所以我也尝试过引发异常':

    raise Exception('Incorrect username or password') 
    

    这仍然会在托管 UI 中给出“用户迁移期间的异常”,但这适用于尊重错误消息的自定义登录 UI。

    【讨论】:

    • 我刚试过这个,我从 Cognito 得到的只是{message: "Exception migrating user in app client 7qk...", __type: "UserNotFoundException"}。我也尝试只返回一个字符串值,结果相同。仅引发异常是否允许您自定义来自 Cognito 的响应?
    • 只要你做一个普​​通的UI(非托管),它就会正常显示。
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2021-04-22
    • 2023-03-27
    • 2019-10-22
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多