为 AWS Cognito 实施 USER_SRP_AUTH 和 python boto3

implementing USER_SRP_AUTH with python boto3 for AWS Cognito

Amazon 提供 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 流程进行身份验证,但我所拥有的不起作用。

它总是失败并显示 "An error occurred (NotAuthorizedException) when calling the RespondToAuthChallenge operation: Incorrect username or password."(username/password 对可以使用 JS SDK 找到。)

我怀疑我构建的质询响应错误(第 3 步),and/or 在需要 base64 时传递 Congito 十六进制字符串,反之亦然。

有人成功了吗?有人看到我做错了什么了吗?

我正在尝试复制在 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)

不幸的是,这是一个难题,因为您没有从服务中获得任何关于计算的提示(它主要是说您提到的未授权)。

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

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

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

你的实现有很多错误。例如:

  1. pysrp默认使用SHA1算法。它应该设置为 SHA256。
  2. _ng_const 长度应为 3072 位,应从 amazon-cognito-identity-js
  3. 复制
  4. pysrp中没有hkdf函数。
  5. 响应应包含 secret_block_b64,而不是 secret_block_hex
  6. 时间戳格式错误。 %H:%m:%S 表示 "hour:month:second",+0000 应替换为 UTC

Has anyone gotten this working?

是的。它在 warrant.aws_srp 模块中实现。 https://github.com/capless/warrant/blob/develop/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']

请注意,aws_srp 模块尚未合并到 master 分支中。

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