此客户端未启用AWS Cognito身份验证的USER_PASSWORD_AUTH流程。

61

我有一个带有用户池(用户名和密码)的移动应用。该应用程序使用aws-amplify sdk运行良好。但是,想要将代码移出到Lambdas。因此,我使用Boto3编写了以下Lambda。

这里是Lambda:

import boto3

def lambda_handler(event, context):
    client = boto3.client('cognito-idp')
    response = client.initiate_auth(
        ClientId='xxxxxxxxxxxxxx',
        AuthFlow='USER_PASSWORD_AUTH',
        AuthParameters={
            'USERNAME': 'xxxxxx',
            'PASSWORD': 'xxxxxx'
        }
    )
    return response

我也尝试使用admin_initiate_auth。

import boto3
def lambda_handler(event, context):
    client = boto3.client('cognito-idp')
    response = client.initiate_auth(
        UserPoolId='xxxxxxxxx',
        ClientId='xxxxxxxxxxxxxx',
        AuthFlow='USER_PASSWORD_AUTH',
        AuthParameters={
            'USERNAME': 'xxxxxx',
            'PASSWORD': 'xxxxxx'
        }
    )
    return response

这是我得到的错误信息:

在调用InitiateAuth操作时发生错误(InvalidParameterException): 用户密码验证流程没有为此客户端启用: InvalidParameterException 溯源 (最近的一次调用):
文件“/var/task/lambda_function.py”,第12行,在lambda_handler中 'PASSWORD': 'xxxxx' 文件“/var/runtime/botocore/client.py”,第317行,_api_call函数 return self._make_api_call(operation_name, kwargs) 文件“/var/runtime/botocore/client.py”,第615行,_make_api_call函数 raise error_class(parsed_response, operation_name) InvalidParameterException: 在调用InitiateAuth操作时发生错误(InvalidParameterException): 用户密码验证流程没有为此客户端启用

有什么想法吗?

7个回答

107

搞定了。我已经找到了用户池 -> 应用程序客户端 -> 显示详细信息 -> 为基于应用程序的身份验证启用用户名密码(非-SRP)流程(USER_PASSWORD_AUTH)。

问题解决了。


1
我遇到了同样的问题,你的问题出现在搜索的第一页。它解决了我的问题。谢谢! - KMC
我遇到了“Initiate Auth method not supported.”的问题,尝试使用boto3==1.7.30和awscli==1.16.3版本,你用哪个版本解决了这个问题? - Efren
1
在 AWS CDK 中,您需要在 appClient 构造函数中提供 enabledAuthFlows: [AuthFlow.USER_PASSWORD] - Townsheriff
3
也许这个已经移动了。现在它在“您的用户池”下的“常规设置”中的“应用客户端”部分->“显示详细信息”->启用基于用户名密码的身份验证(ALLOW_USER_PASSWORD_AUTH),而不是在“应用集成”->“应用客户端设置”下。 - nachbar
如果有人对更安全的方式感兴趣,请查看此链接:https://dev59.com/fVgR5IYBdhLWcg3wRLiD#43046495 - Shubham Jain
在不断搜索仪表板中的正确设置时,现在似乎是您的用户池->(用户池)->应用程序集成->应用程序客户端列表->(应用程序客户端名称)->应用程序客户端信息->编辑->身份验证流->选择身份验证流->ALLOW_USER_PASSWORD_AUTH。 - user1763510

18

明白了。我需要转到用户池 -> 应用程序客户端 -> 显示详细信息 -> 为管理员API身份验证启用用户名密码身份验证(ALLOW_ADMIN_USER_PASSWORD_AUTH)。


“Auth Flows Configuration” 部分的第一个选择:https://imgur.com/a/9G4WkN1 - MarkHu
在 Amplify 中以编程方式设置这个标志的任何想法吗? - isick
你有查看 Amplify 文档吗? - pedro.caicedo.dev

3

一张图片胜过千言万语

  • 前往Cognito服务

enter image description here

  • 然后,选择应用程序客户端

enter image description here

最后,编辑认证工作流程。

enter image description here


3

对我来说,我发现我的凭证需要一个hmac,在这里是类,如果有用的话。

import boto3
import boto3.session
import hmac, base64, hashlib
from botocore.client import ClientMeta

class AwsAuth(object):
    '''
    classdocs
    '''

    def gettoken(self):
        if self.token:
            return self.token
        else:
            return False

    def connect(self):

        if not self.username:
            self.username = raw_input("Username: ")

        if not self.password:
            self.password = raw_input("Password: ")

        digest = self.gethmacdigest(self.username)

        response = self.client.initiate_auth(
            ClientId=self.clientid,
            AuthFlow='USER_PASSWORD_AUTH',
            AuthParameters={
                'USERNAME': self.username,
                'PASSWORD': self.password,
                'SECRET_HASH': digest
            },
            ClientMetadata={
                'UserPoolId': self.userpoolid
            }
        )
        self.token = response
        return response

    def gethmacdigest(self, username):

        message = username + self.clientid
        dig = hmac.new(self.clientsecret, msg=message.encode('UTF-8'), digestmod=hashlib.sha256).digest()    
        return base64.b64encode(dig).decode()


    def __init__(self, path, url, fileout, filein, userpoolid, clientid, clientsecret, region, username = None, password = None):
        '''
        Constructor
        '''

        #boto3.set_stream_logger('botocore', level="DEBUG")

        self.path = path
        self.url = url
        self.fileout = fileout
        self.filein = filein
        self.userpoolid = userpoolid
        self.clientid = clientid
        self.clientsecret = clientsecret
        self.region = region
        self.token = ""

        boto3.setup_default_session(region_name=region) 

        self.client = boto3.client('cognito-idp')
        if username is not None:
            self.username = username
        else:
            self.username = None
        if password is not None:
            self.password = password
        else:
            self.password = None

0

经过多次尝试,我找到了解决方法。

  1. 从AWS控制台进入Cognito
  2. 然后单击所需池,以解决出现错误的问题 \
  3. 现在在左侧面板上单击应用程序客户端选项。它将向您显示可用的客户端
  4. 单击要使用的应用程序客户端的显示详细信息。然后,在身份验证流配置部分中,请确保勾选启用基于用户名密码的身份验证(ALLOW_USER_PASSWORD_AUTH)

有关API的更多详细信息,请参阅此链接InitiateAuth


0

前往Cognito服务 输入图像描述

然后,选择应用程序客户端

点击编辑,并选择USER_PASSWORD_AUTH

enter image description here

enter image description here

之后我尝试登录并获得了成功的响应:

enter image description here


-1
我已经弄清楚了。尽管 AuthFlow 通过 ExplicitAuthFlows,但它应该可以工作。
import boto3
def lambda_handler(event, context):
    client = boto3.client('cognito-idp')
    response = client.initiate_auth(
        UserPoolId='xxxxxxxxx',
        ClientId='xxxxxxxxxxxxxx',
        ExplicitAuthFlows='USER_PASSWORD_AUTH',
        AuthParameters={
            'USERNAME': 'xxxxxx',
            'PASSWORD': 'xxxxxx'
        }
    )
    return response

`


2
但是AuthFlow是一个必需参数。 - Kushan Gunasekera
是的,它是一个必需的参数。ExplicitAuthFlows 实际上只调用 AuthFlow。 - Afsheen. Nazish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接