Last active
March 12, 2020 00:47
-
-
Save youngsoul/597f7ec0b949459ff2e2fb6af39095e4 to your computer and use it in GitHub Desktop.
Python Lambda function receiving a Cognito token and getting the claims from it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import json | |
from jose import jwt | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
def lambda_handler(event, context): | |
logger.debug("Add Note Lambda") | |
logger.debug(f"Event: {event}") | |
try: | |
if 'headers' not in event: | |
raise Exception("event is missing 'headers' section") | |
if 'token' not in event['headers']: | |
raise Exception("event['headers'] is missing 'token' section") | |
if 'body' not in event: | |
raise Exception("event is missing 'body' section") | |
token = event['headers']['token'] | |
body = event['body'] | |
claims = jwt.get_unverified_claims(token) | |
email = claims['email'] | |
logger.debug(f"Add Note: {body} for email: {email}") | |
return { | |
"statusCode": 200, | |
"body": json.dumps(f"Add Note for email: {email}. Success") | |
} | |
except Exception as exc: | |
return { | |
"statusCode": 500, | |
"body": json.dumps(f"Error in processing request. {exc}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment