使用python boto3为AWS Cognito实现USER_SRP_AUTH



亚马逊提供iOS、Android和Javascript Cognito SDK,提供高级身份验证用户操作。

例如,请参阅此处的用例4:

https://github.com/aws/amazon-cognito-identity-js

然而,如果您使用的是python/boto3,那么您得到的只是一对基元:cognito.initiate_authcognito.respond_to_auth_challenge

我正试图将这些基元与pysrp库一起使用,以通过USER_SRP_AUTH流进行身份验证,但我所拥有的不起作用。

它总是以";调用RespondToAuthChallenge操作时发生错误(NotAuthorizedException):用户名或密码不正确"(用户名/密码对与JS SDK配合良好。)

我怀疑我构建的挑战-响应是错误的(步骤3),和/或在Congito需要base64时传递十六进制字符串,反之亦然。

有人让它工作了吗?有人看到我做错了什么吗?

我正在尝试复制在Javascript SDK中找到的authenticateUser调用的行为:

https://github.com/aws/amazon-cognito-identity-js/blob/master/src/CognitoUser.js#L138

但我做错了什么,搞不清楚是什么。

#!/usr/bin/env python
import base64
import binascii
import boto3
import datetime as dt
import hashlib
import hmac
# http://pythonhosted.org/srp/
# https://github.com/cocagne/pysrp
import srp
bytes_to_hex = lambda x: "".join("{:02x}".format(ord(c)) for c in x)
cognito = boto3.client('cognito-idp', region_name="us-east-1")
username = "foobar@foobar.com"
password = "123456"
user_pool_id = u"us-east-1_XXXXXXXXX"
client_id = u"XXXXXXXXXXXXXXXXXXXXXXXXXX"
# Step 1:
# Use SRP lib to construct a SRP_A value.
srp_user = srp.User(username, password)
_, srp_a_bytes = srp_user.start_authentication()
srp_a_hex = bytes_to_hex(srp_a_bytes)
# Step 2:
# Submit USERNAME & SRP_A to Cognito, get challenge.
response = cognito.initiate_auth(
AuthFlow='USER_SRP_AUTH',
AuthParameters={ 'USERNAME': username, 'SRP_A': srp_a_hex },
ClientId=client_id,
ClientMetadata={ 'UserPoolId': user_pool_id })
# Step 3:
# Use challenge parameters from Cognito to construct 
# challenge response.
salt_hex         = response['ChallengeParameters']['SALT']
srp_b_hex        = response['ChallengeParameters']['SRP_B']
secret_block_b64 = response['ChallengeParameters']['SECRET_BLOCK']
secret_block_bytes = base64.standard_b64decode(secret_block_b64)
secret_block_hex = bytes_to_hex(secret_block_bytes)
salt_bytes = binascii.unhexlify(salt_hex)
srp_b_bytes = binascii.unhexlify(srp_b_hex)
process_challenge_bytes = srp_user.process_challenge(salt_bytes,                          
srp_b_bytes)

timestamp = unicode(dt.datetime.utcnow().strftime("%a %b %d %H:%m:%S +0000 %Y"))
hmac_obj = hmac.new(process_challenge_bytes, digestmod=hashlib.sha256)
hmac_obj.update(user_pool_id.split('_')[1].encode('utf-8'))
hmac_obj.update(username.encode('utf-8'))
hmac_obj.update(secret_block_bytes)
hmac_obj.update(timestamp.encode('utf-8'))
challenge_responses = {
"TIMESTAMP": timestamp.encode('utf-8'),
"USERNAME": username.encode('utf-8'),
"PASSWORD_CLAIM_SECRET_BLOCK": secret_block_hex,
"PASSWORD_CLAIM_SIGNATURE": hmac_obj.hexdigest()
}
# Step 4:
# Submit challenge response to Cognito.
response = cognito.respond_to_auth_challenge(
ClientId=client_id,
ChallengeName='PASSWORD_VERIFIER',
ChallengeResponses=challenge_responses)

您的实现中有许多错误。例如:

  1. pysrp默认使用SHA1算法。应将其设置为SHA256
  2. _ng_const长度应为3072位,并且应从amazon-cognito-identity-js复制
  3. pysrp中没有hkdf函数
  4. 响应应包含secret_block_b64,而不是secret_block_hex
  5. 时间戳格式错误。CCD_ 12表示";小时:月:秒";并且CCD_ 13应该被CCD_

有人做到了吗?

是。它在warrant.aws_srp模块中实现。https://github.com/capless/warrant/blob/master/warrant/aws_srp.py

from warrant.aws_srp import AWSSRP

USERNAME='xxx'
PASSWORD='yyy'
POOL_ID='us-east-1_zzzzz'
CLIENT_ID = '12xxxxxxxxxxxxxxxxxxxxxxx'
aws = AWSSRP(username=USERNAME, password=PASSWORD, pool_id=POOL_ID,
client_id=CLIENT_ID)
tokens = aws.authenticate_user()
id_token = tokens['AuthenticationResult']['IdToken']
refresh_token = tokens['AuthenticationResult']['RefreshToken']
access_token = tokens['AuthenticationResult']['AccessToken']
token_type = tokens['AuthenticationResult']['TokenType']

authenticate_user方法仅支持PASSWORD_VERIFIER挑战。如果您想应对其他挑战,只需查看authenticate_userboto3文档即可。

不幸的是,这是一个棘手的问题,因为您没有从服务中得到任何关于计算的提示(正如您所提到的,它主要表示未经授权)。

当用户试图用我们没有SDK的语言自己实现SRP时,我们正在努力改善开发人员的体验。此外,我们正在尝试添加更多的SDK。

尽管听起来很吓人,但我建议使用Javascript或Android SDK,修复输入(SRP_A、SRP_B、TIMESTAMP),并在实现的各个点添加console.log语句,以确保您的计算结果相似。然后,您将在实现中运行这些计算,并确保得到相同的结果。正如您所建议的,密码声明签名需要作为base64编码的字符串传递给服务,因此这可能是问题之一。

我在实现这一点时遇到的一些问题与BigInteger库的差异有关(它们进行字节填充和将负数转换为字节数组的方式以及相反的方式)。

我有一个类似于@armicron的使用pycognito库的替代方案。

import boto3
from pycognito import AWSSRP
session = boto3.Session(profile_name="aws-profile")
cognito = session.client('cognito-idp')
cognito_user_pool_id = "xx-xxxx-x_xxxxxxxxx"
cognito_user_pool_client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
username = "xxxxxxxxxxxxx"
password = "xxxxxxxxxxxxx"
aws_srp = AWSSRP(
username=username,
password=password,
pool_id=cognito_user_pool_id,
client_id=cognito_user_pool_client_id,
client=cognito
)
# Initiate Auth
print("nInitiate Auth Started")
auth_params = aws_srp.get_auth_params()
resp = cognito.initiate_auth(
AuthFlow='USER_SRP_AUTH',
AuthParameters=auth_params,
ClientId=cognito_user_pool_client_id
)
print(resp)
# Respond to PASSWORD_VERIFIER challenge
print("nPASSWORD_VERIFIER Started")
assert resp["ChallengeName"] == "PASSWORD_VERIFIER"
challenge_response = aws_srp.process_challenge(resp["ChallengeParameters"], auth_params)
resp = cognito.respond_to_auth_challenge(
ClientId=cognito_user_pool_client_id,
ChallengeName="PASSWORD_VERIFIER",
ChallengeResponses=challenge_response
)
print(resp)

当您想要使用SRP密码测试CUSTOM_AUTH流时,这可能非常有用。

最新更新