Skip to content

Instantly share code, notes, and snippets.

@fcavalcantirj
Last active September 11, 2019 13:37
Show Gist options
  • Save fcavalcantirj/dd0aae0bddd9871e3e65e0cc5852059e to your computer and use it in GitHub Desktop.
Save fcavalcantirj/dd0aae0bddd9871e3e65e0cc5852059e to your computer and use it in GitHub Desktop.
Lambda trigger to signOut users using accessToken and global_sign_out API
"""This is an function to signOut users.
Usage::
Just create an resource and a method on the API Gateway,
integrate the request with this lambda function via console, and test it;
json used to test;
{
"access_token":"something"
}
"""
from __future__ import print_function
import boto3
import botocore.exceptions
import json
import logging
USER_POOL_ID = 'us-east-2_blah-blah-blah'
CLIENT_ID = 'blah-blah-blah'
CLIENT_SECRET = 'blah-blah-blah-really-long-string'
client = None
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def initiate_logout(accessToken):
try:
resp = client.global_sign_out(
AccessToken=accessToken
)
except client.exceptions.NotAuthorizedException as e:
return None, "Unauthorized"
except client.exceptions.UserNotFoundException as e:
return None, "Unauthorized"
except Exception as e:
#print(e)
logger.error(e)
return None, "Unknown error"
return resp, None
def lambda_handler(event, context):
global client
if client == None:
client = boto3.client('cognito-idp')
print(event)
body = event
access_token = body['access_token']
resp, msg = initiate_logout(access_token)
if msg != None:
# return {'status': 'fail', 'msg': msg}
logger.info('failed to logout with accessToken={}'.format(access_token))
raise Exception(msg)
logger.info('successfull logout with accessToken={}'.format(access_token))
return {'message': 'Usuario deslogado com sucesso'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment