Untitled

Cognito admin
mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.3 kB
6
Indexable
Never
import boto3
from botocore.exceptions import *
import json
import os 

def retrieve_credentials() :
    s3_client = boto3.client("s3")
    response = s3_client.get_object(Bucket = "s3bucketchecksslvupham", Key = "data_serve_lambda_edge.json")

    return response['Body'].read().decode("utf-8")

def set_env_variables(data) :
    data = json.loads(data)

    os.environ['CLIENT_ID']              = data['cognito_user_pool_client_id']['value']
    os.environ['CLIENT_SECRET']          = data['cognito_user_pool_client_secret']['value']
    
    os.environ['USER_POOL_ID']           = data['cognito_user_pool_id']['value']
    os.environ['USER_POOL_DOMAIN_URL']   = "stylcoggoogle" + data['cognito_user_pool_domain_url']['value']
    os.environ['COGNITO_IDP']            = "cognito-idp.us-east-1.amazonaws.comcognito-idp.us-east-1.amazonaws.com"

    os.environ['CLOUDFRONT_DOMAIN']      = data['cloudfront_domain_name']['value']

def read_data() :
    file_path = os.path.join(f"/tmp", "data_serve_lambda_edge.json")
    while (True) :
        try :
            with open(file_path, "r") as file : 
                data = file.read()            
            
            print("READ TEMPORARY CREDETIALS")
            set_env_variables(data)
            break
        
        except :
            print("RETRIEVE CREDENTIALS FROM S3")
            with open(file_path, "w") as file : 
                file.write(retrieve_credentials())  

def Main() :
    client = boto3.client("cognito-idp")
    read_data()
    
    user_list = list_users(client)

    for user in user_list :
        print(user)
        # disable_user(user)
        # enable_user(user)

def list_users(client) :
    user_pool_id = os.getenv('USER_POOL_ID')
    response = client.list_users(
        UserPoolId = user_pool_id
    )

    return response['Users']

def disable_user(client, username) : 
    user_pool_id = os.getenv('USER_POOL_ID')
    response = client.admin_disable_user(
        UserPoolId = user_pool_id,
        Username = username
    )

    return response 

def enable_user(client, username) : 
    user_pool_id = os.getenv('USER_POOL_ID')
    response = client.admin_enable_user(
        UserPoolId = user_pool_id,
        Username = username
    )

    return response 


Main()