如何在Python中对AWS Cognito用户池进行身份验证?

如何在Python中对AWS Cognito用户池进行身份验证?,python,amazon-web-services,amazon-cognito,Python,Amazon Web Services,Amazon Cognito,我有一个静态的无服务器网站,它允许使用Javascript进行身份验证 现在我正在尝试启用一些编程访问,因此我需要通过Python脚本执行相同的身份验证。这可能吗?这些文档没有为Python提供任何支持 我只是想为Python找到一些方法,对AWS URL发出GET或POST请求,向其传递用户名和登录名,并返回验证身份验证 我找到的最接近的例子是,它引用了。我已修改为: import boto3 client_id = '<my_app_client_id>' region_name

我有一个静态的无服务器网站,它允许使用Javascript进行身份验证

现在我正在尝试启用一些编程访问,因此我需要通过Python脚本执行相同的身份验证。这可能吗?这些文档没有为Python提供任何支持

我只是想为Python找到一些方法,对AWS URL发出GET或POST请求,向其传递用户名和登录名,并返回验证身份验证

我找到的最接近的例子是,它引用了。我已修改为:

import boto3
client_id = '<my_app_client_id>'
region_name = 'us-east-1'
auth_data = { 'USERNAME':'myusername' , 'PASSWORD':'mypassword' }
provider_client = boto3.client('cognito-idp', region_name=region_name)
resp = provider_client.initiate_auth(AuthFlow='USER_PASSWORD_AUTH', AuthParameters=auth_data, ClientId=client_id)
print('resp:', resp)

这是与Javascript Cognito API相同的正确Python等价物?

像这样将访问和密钥传递给boto3

provider_client = boto3.client('cognito-idp', region_name=region_name, aws_access_key_id=AWS_ACCESS_KEY_ID,
                              aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

像这样将访问权限和密钥传递给boto3

provider_client = boto3.client('cognito-idp', region_name=region_name, aws_access_key_id=AWS_ACCESS_KEY_ID,
                              aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

下面的代码片段显示了使用boto3的Cognito的完整身份验证工作流

def get_secret_散列(用户名):
msg=用户名+客户端ID
dig=hmac.new(
str(客户端密码)。编码('utf-8'),
msg=str(msg).encode('utf-8'),
digestmod=hashlib.sha256
).摘要(
d2=base64.b64编码(dig).decode()
返回d2
def initiate_auth(客户端、用户名、密码):
secret\u hash=get\u secret\u hash(用户名)
尝试:
resp=client.admin\u initiate\u auth(
UserPoolId=USER\u POOL\u ID,
ClientId=客户端ID,
AuthFlow='ADMIN\u NO\u SRP\u AUTH',
AuthParameters={
“用户名”:用户名,
“秘密散列”:秘密散列,
“密码”:密码,
},
客户端元数据={
“用户名”:用户名,
“密码”:密码,})
除client.exceptions.NotAuthorizedException外:
返回None,“用户名或密码不正确”
除client.exceptions.UserNotConfirmedException外:
返回无,“用户未确认”
例外情况除外,如e:
返回None,e.\u str\u()
返回resp,无
@app.route('/auth/login',methods=['POST'])
def login():
event=auth.current\u request.json\u body
client=bot3.client('cognito-idp'))
用户名=事件['username']
密码=事件['password']
对于[“用户名”、“密码”]中的字段:
如果event.get(字段)为无:
返回{“error”:True,
“成功”:错误,
“消息”:f“{field}是必需的”,
“数据”:无}
resp,msg=initiate_auth(客户端、用户名、密码)
如果是味精!=无:
返回{'message':msg,
“error”:True,“success”:False,“data”:None}
如果分别获取(“AuthenticationResult”):
返回{'message':“success”,
“错误”:错误,
“成功”:没错,
“数据”:{
“id_令牌”:resp[“AuthenticationResult”][“IdToken”],
“刷新令牌”:resp[“AuthenticationResult”][“刷新令牌”],
“访问令牌”:resp[“AuthenticationResult”][“AccessToken”],
“expires_in”:resp[“AuthenticationResult”][“ExpiresIn”],
“令牌类型”:resp[“AuthenticationResult”][“令牌类型”]
}}
else:#此代码块仅在启用MFA时相关
返回{“error”:True,
“成功”:错误,
“数据”:无,“消息”:无}
下面是从函数中解析的重要部分

resp=client.admin\u initiate\u auth(
UserPoolId=USER\u POOL\u ID,
ClientId=客户端ID,
AuthFlow='ADMIN\u NO\u SRP\u AUTH',
AuthParameters={
“用户名”:用户名,
“秘密散列”:秘密散列,
“密码”:密码,
},
客户端元数据={
“用户名”:用户名,
“密码”:密码,})
示例取自一个由四部分组成的教程,不幸的是,该教程没有帮助我将其与Chalice CognitoUserPoolAuthorizer集成,但在其他方面似乎效果良好。如果找不到更好的代码示例,下面是教程


    • 以下代码片段显示了使用boto3的Cognito的完整身份验证工作流

      def get_secret_散列(用户名):
      msg=用户名+客户端ID
      dig=hmac.new(
      str(客户端密码)。编码('utf-8'),
      msg=str(msg).encode('utf-8'),
      digestmod=hashlib.sha256
      ).摘要(
      d2=base64.b64编码(dig).decode()
      返回d2
      def initiate_auth(客户端、用户名、密码):
      secret\u hash=get\u secret\u hash(用户名)
      尝试:
      resp=client.admin\u initiate\u auth(
      UserPoolId=USER\u POOL\u ID,
      ClientId=客户端ID,
      AuthFlow='ADMIN\u NO\u SRP\u AUTH',
      AuthParameters={
      “用户名”:用户名,
      “秘密散列”:秘密散列,
      “密码”:密码,
      },
      客户端元数据={
      “用户名”:用户名,
      “密码”:密码,})
      除client.exceptions.NotAuthorizedException外:
      返回None,“用户名或密码不正确”
      除client.exceptions.UserNotConfirmedException外:
      返回无,“用户未确认”
      例外情况除外,如e:
      返回None,e.\u str\u()
      返回resp,无
      @app.route('/auth/login',methods=['POST'])
      def login():
      event=auth.current\u request.json\u body
      client=bot3.client('cognito-idp'))
      用户名=事件['username']
      密码=事件['password']
      对于[“用户名”、“密码”]中的字段:
      如果event.get(字段)为无:
      返回{“error”:True,
      “成功”:错误,
      “消息”:f“{field}是必需的”,
      “数据”:无}
      resp,msg=initiate_auth(客户端、用户名、密码)
      如果是味精!=无:
      返回{'message':msg,
      “error”:True,“success”:False,“data”:None}
      如果得到