Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import base64
  3. import binascii
  4. import boto3
  5. import datetime as dt
  6. import hashlib
  7. import hmac
  8.  
  9. # http://pythonhosted.org/srp/
  10. # https://github.com/cocagne/pysrp
  11. import srp
  12.  
  13. bytes_to_hex = lambda x: "".join("{:02x}".format(ord(c)) for c in x)
  14.  
  15. cognito = boto3.client('cognito-idp', region_name="us-east-1")
  16.  
  17. username = "foobar@foobar.com"
  18. password = "123456"
  19.  
  20. user_pool_id = u"us-east-1_XXXXXXXXX"
  21. client_id = u"XXXXXXXXXXXXXXXXXXXXXXXXXX"
  22.  
  23. # Step 1:
  24. # Use SRP lib to construct a SRP_A value.
  25.  
  26. srp_user = srp.User(username, password)
  27. _, srp_a_bytes = srp_user.start_authentication()
  28.  
  29. srp_a_hex = bytes_to_hex(srp_a_bytes)
  30.  
  31. # Step 2:
  32. # Submit USERNAME & SRP_A to Cognito, get challenge.
  33.  
  34. response = cognito.initiate_auth(
  35. AuthFlow='USER_SRP_AUTH',
  36. AuthParameters={ 'USERNAME': username, 'SRP_A': srp_a_hex },
  37. ClientId=client_id,
  38. ClientMetadata={ 'UserPoolId': user_pool_id })
  39.  
  40. # Step 3:
  41. # Use challenge parameters from Cognito to construct
  42. # challenge response.
  43.  
  44. salt_hex = response['ChallengeParameters']['SALT']
  45. srp_b_hex = response['ChallengeParameters']['SRP_B']
  46. secret_block_b64 = response['ChallengeParameters']['SECRET_BLOCK']
  47.  
  48. secret_block_bytes = base64.standard_b64decode(secret_block_b64)
  49. secret_block_hex = bytes_to_hex(secret_block_bytes)
  50.  
  51. salt_bytes = binascii.unhexlify(salt_hex)
  52. srp_b_bytes = binascii.unhexlify(srp_b_hex)
  53.  
  54. process_challenge_bytes = srp_user.process_challenge(salt_bytes,
  55. srp_b_bytes)
  56.  
  57. timestamp = unicode(dt.datetime.utcnow().strftime("%a %b %d %H:%m:%S +0000 %Y"))
  58.  
  59. hmac_obj = hmac.new(process_challenge_bytes, digestmod=hashlib.sha256)
  60. hmac_obj.update(user_pool_id.split('_')[1].encode('utf-8'))
  61. hmac_obj.update(username.encode('utf-8'))
  62. hmac_obj.update(secret_block_bytes)
  63. hmac_obj.update(timestamp.encode('utf-8'))
  64.  
  65. challenge_responses = {
  66. "TIMESTAMP": timestamp.encode('utf-8'),
  67. "USERNAME": username.encode('utf-8'),
  68. "PASSWORD_CLAIM_SECRET_BLOCK": secret_block_hex,
  69. "PASSWORD_CLAIM_SIGNATURE": hmac_obj.hexdigest()
  70. }
  71.  
  72. # Step 4:
  73. # Submit challenge response to Cognito.
  74.  
  75. response = cognito.respond_to_auth_challenge(
  76. ClientId=client_id,
  77. ChallengeName='PASSWORD_VERIFIER',
  78. ChallengeResponses=challenge_responses)
  79.  
  80. from warrant.aws_srp import AWSSRP
  81.  
  82.  
  83. USERNAME='xxx'
  84. PASSWORD='yyy'
  85. POOL_ID='us-east-1_zzzzz'
  86. CLIENT_ID = '12xxxxxxxxxxxxxxxxxxxxxxx'
  87.  
  88. aws = AWSSRP(username=USERNAME, password=PASSWORD, pool_id=POOL_ID,
  89. client_id=CLIENT_ID)
  90. tokens = aws.authenticate_user()
  91. id_token = tokens['AuthenticationResult']['IdToken']
  92. refresh_token = tokens['AuthenticationResult']['RefreshToken']
  93. access_token = tokens['AuthenticationResult']['AccessToken']
  94. token_type = tokens['AuthenticationResult']['TokenType']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement