我有一个带用户池的移动应用程序(用户名和密码)。该应用程序适用于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
这是我得到的错误。
调用时发生错误(InvalidParameterException) InitiateAuth操作:未启用USER_PASSWORD_AUTH流程 client:InvalidParameterException Traceback(最近一次调用last):
在lambda_handler中输入第12行的文件“/var/task/lambda_function.py” '密码':'xxxxx'文件“/var/runtime/botocore/client.py”,第317行,在_api_call 返回self._make_api_call(operation_name,kwargs)文件“/var/runtime/botocore/client.py”,第615行,在_make_api_call中 raise error_class(parsed_response,operation_name)InvalidParameterException:发生错误 (InvalidParameterException)调用InitiateAuth操作时: 此客户端未启用USER_PASSWORD_AUTH流程
有什么想法吗?
答案 0 :(得分:22)
想通了。我有转到用户池 - >应用客户 - >显示详细信息 - >为基于应用程序的身份验证启用用户名密码(非SRP)流程(USER_PASSWORD_AUTH)。
修正了它。
答案 1 :(得分:3)
想通了。我有转到用户池->应用程序客户端->显示详细信息->为管理API启用用户名密码身份验证以进行身份验证(ALLOW_ADMIN_USER_PASSWORD_AUTH)。
答案 2 :(得分:0)
对我来说,我发现我的凭据需要一个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
答案 3 :(得分:0)
我想通了。尽管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
`