18

Amazon は、高レベルの認証ユーザー操作を提供する iOS、Android、および Javascript Cognito SDK を提供します。

たとえば、こちらのユース ケース 4 を参照してください。

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

ただし、python/boto3 を使用している場合は、プリミティブのペアcognito.initiate_authcognito.respond_to_auth_challenge.

これらのプリミティブをpysrplib 認証と一緒に使用しようとしていますUSER_SRP_AUTHが、私が持っているものは機能していません。

「RespondToAuthChallenge操作を呼び出すときにエラーが発生しました(NotAuthorizedException):ユーザー名またはパスワードが正しくありません」で常に失敗します。(ユーザー名とパスワードのペアは、JS SDK で機能します。)

私の疑いは、チャレンジ応答を間違って作成していること (ステップ 3)、および/または base64 が必要なときに Congito 16 進文字列を渡していること、またはその逆であることです。

誰かがこれを機能させましたか?誰かが私が間違っていることを見ていますか?

authenticateUserJavascript SDK にある呼び出しの動作をコピーしようとしています。

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)
4

2 に答える 2

16

実装には多くのエラーがあります。例えば:

  1. pysrpデフォルトで SHA1 アルゴリズムを使用します。SHA256 に設定する必要があります。
  2. _ng_const長さは 3072 ビットである必要があり、コピーする必要があります。amazon-cognito-identity-js
  3. にはhkdf関数はありませんpysrp
  4. 応答にはsecret_block_b64ではなく、が含まれている必要がありsecret_block_hexます。
  5. タイムスタンプの形式が正しくありません。 %H:%m:%Sは「時:月:秒」を意味+0000し、に置き換える必要がありますUTC

誰かがこれを機能させましたか?

はい。モジュールに実装されてい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ドキュメントを参照してください。

于 2017-03-27T12:28:16.297 に答える