Sunday, January 27, 2019

AWS Cognito - Get token with ADMIN_NO_SRP_AUTH (Python)



try:
return boto3.Session(profile_name=’[PROFILE_NAME]’)
.client('cognito-idp', [REGION]).admin_initiate_auth(
UserPoolId=[USER_POOL_ID],
ClientId=[CLIENT_ID],
AuthFlow='ADMIN_NO_SRP_AUTH',
AuthParameters={
'USERNAME': [USER_NAME],
'PASSWORD': [PASSWORD],
'SECRET_HASH': get_secret_hash([UserName])
}
)
except botocore.exceptions.ClientError as e:
return e.response

# creating secret hash
def get_secret_hash(self, username):
message = username + self.client_id
dig = hmac.new(bytes(self.client_secret, encoding='utf-8'), msg=message.encode('UTF-8'),
digestmod=hashlib.sha256).digest()
hash_is = base64.b64encode(dig).decode()
return hash_is