Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. from __future__ import print_function
  2. import boto3
  3. import botocore.exceptions
  4. import hmac
  5. import hashlib
  6. import base64
  7. import json
  8. import uuid
  9. # MODIFY
  10. USER_POOL_ID = 'us-east-1_sbOEkUr8G'
  11. CLIENT_ID = 'vbdgh2714r67o938jmhpf2pha'
  12. CLIENT_SECRET = '14v847ogv3i2fcau4c8dp5k8tsjoc89jcr93b11hp9qrtngb9nfh'
  13. client = None
  14.  
  15. def get_secret_hash(username):
  16.     msg = username + CLIENT_ID
  17.     dig = hmac.new(str(CLIENT_SECRET).encode('utf-8'),
  18.         msg = str(msg).encode('utf-8'), digestmod=hashlib.sha256).digest()
  19.     d2 = base64.b64encode(dig).decode()
  20.     return d2
  21.    
  22. ERROR = 0
  23. SUCCESS = 1
  24. USER_EXISTS = 2
  25.  
  26. def sign_up(username, password):
  27.     try:
  28.         resp = client.sign_up(
  29.             ClientId=CLIENT_ID,
  30.             SecretHash=get_secret_hash(username),
  31.             Username=username,
  32.             Password=password)
  33.         print(resp)
  34.     except client.exceptions.UsernameExistsException as e:
  35.         return USER_EXISTS
  36.     except Exception as e:
  37.         print(e)
  38.         return ERROR
  39.     return SUCCESS
  40.  
  41. def initiate_auth(username, password):
  42.     try:
  43.         resp = client.admin_initiate_auth(
  44.             UserPoolId=USER_POOL_ID,
  45.             ClientId=CLIENT_ID,
  46.             AuthFlow='ADMIN_NO_SRP_AUTH',
  47.             AuthParameters={
  48.                 'USERNAME': username,
  49.                 'SECRET_HASH': get_secret_hash(username),
  50.                 'PASSWORD': password
  51.             },
  52.             ClientMetadata={
  53.                 'username': username,
  54.                 'password': password
  55.             })
  56.     except client.exceptions.NotAuthorizedException as e:
  57.         return None, "The username or password is incorrect"
  58.     except Exception as e:
  59.         print(e)
  60.         return None, "Unknown error"
  61.     return resp, None
  62.  
  63. def lambda_handler(event, context):
  64.     global client
  65.     if client == None:
  66.         client = boto3.client('cognito-idp')
  67.     print(event)
  68.     body = event
  69.     username = body['username']
  70.     password = body['password']
  71.     is_new = "false"
  72.     user_id = str(uuid.uuid4())
  73.     signed_up = sign_up(username, password)
  74.     if signed_up == ERROR:
  75.         return {'status': 'fail', 'msg': 'failed to sign up'}
  76.     if signed_up == SUCCESS:
  77.         is_new = "true"
  78.         #user_id = str(uuid.uuid4())
  79.     resp, msg = initiate_auth(username, password)
  80.     if msg != None:
  81.         return {'status': 'fail', 'msg': msg}
  82.     id_token = resp['AuthenticationResult']['IdToken']
  83.     print('id token: ' + id_token)
  84.     return {'status': 'success', 'id_token': id_token, 'user_id': user_id, 'is_new':
  85. is_new}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement