[英]implementing USER_SRP_AUTH with python boto3 for AWS Cognito
Amazon 提供 iOS、Android 和 Javascript Cognito SDK,這些 SDK 提供高級身份驗證用戶操作。
例如,請參閱此處的用例 4:
https://github.com/aws/amazon-cognito-identity-js
然而,如果你使用 python/boto3,你得到的只是一對原語: cognito.initiate_auth
和cognito.respond_to_auth_challenge
。
我正在嘗試將這些原語與pysrp
lib 一起使用,並使用USER_SRP_AUTH
流進行身份驗證,但我所擁有的不起作用。
它總是失敗並顯示“調用 RespondToAuthChallenge 操作時發生錯誤 (NotAuthorizedException):用戶名或密碼不正確。” (用戶名/密碼對適用於 JS SDK。)
我的懷疑是我構建了錯誤的質詢響應(步驟 3),和/或在需要 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)
您的實施中有很多錯誤。 例如:
pysrp
默認使用 SHA1 算法。 它應該設置為 SHA256。_ng_const
長度應為 3072 位,應從amazon-cognito-identity-js
復制pysrp
中沒有hkdf函數。secret_block_b64
,而不是secret_block_hex
。%H:%m:%S
表示 "hour:month:second" 並且+0000
應替換為UTC
。有沒有人得到這個工作?
是的。 它在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_user
和boto3
文檔即可。
不幸的是,這是一個難題,因為您沒有從服務中獲得有關計算的任何提示(它主要表示您提到的未授權)。
當用戶嘗試使用我們沒有 SDK 的語言自行實施 SRP 時,我們正在努力改善開發人員體驗。 此外,我們正在嘗試添加更多 SDK。
聽起來令人生畏,但我建議采用 Javascript 或 Android SDK,修復輸入(SRP_A、SRP_B、TIMESTAMP)並在實現的各個點添加 console.log 語句,以確保您的計算相似。 然后你會在你的實現中運行這些計算並確保你得到相同的結果。 正如您所建議的,密碼聲明簽名需要作為 base64 編碼字符串傳遞給服務,這可能是問題之一。
我在實現這一點時遇到的一些問題與 BigInteger 庫的差異有關(它們進行字節填充並將負數轉換為字節數組的方式以及相反的方式)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.