Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Created December 11, 2019 13:50
Show Gist options
  • Save chespinoza/d7dffc19b45f189f2a120117eaf29ef5 to your computer and use it in GitHub Desktop.
Save chespinoza/d7dffc19b45f189f2a120117eaf29ef5 to your computer and use it in GitHub Desktop.
import json
import boto3
import os
client = boto3.client('cognito-idp')
def lambda_handler(event, context):
if (event['triggerSource'] == 'UserMigration_Authentication'):
user = client.admin_initiate_auth(
UserPoolId=os.environ["USER_POOL_ID"],
ClientId=os.environ["CLIENT_ID"],
AuthFlow='ADMIN_NO_SRP_AUTH',
AuthParameters={
'USERNAME': event['userName'],
'PASSWORD': event['request']['password']
}
)
if (user):
userAttributes = client.get_user(
AccessToken=user['AuthenticationResult']['AccessToken']
)
for userAttribute in userAttributes['UserAttributes']:
if userAttribute['Name'] == 'email':
userEmail = userAttribute['Value']
#print(userEmail)
event['response']['userAttributes'] = {
"email": userEmail,
"email_verified": "true",
"name": "default-name"
}
event['response']['messageAction'] = "SUPPRESS"
print (event)
return (event)
else:
return('Bad Password')
elif (event["triggerSource"] == "UserMigration_ForgotPassword"):
user = client.admin_get_user(
UserPoolId=os.environ["USER_POOL_ID"],
Username=event['userName']
)
if (user):
for userAttribute in user['UserAttributes']:
if userAttribute['Name'] == 'email':
userEmail = userAttribute['Value']
print(userEmail)
event['response']['userAttributes'] = {
"email": userEmail,
"email_verified": "true",
"name": "default-name"
}
event['response']['messageAction'] = "SUPPRESS"
print (event)
return (event)
else:
return('Bad Password')
else:
return('there was an error')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment