import argparse import os import requests # don't look at anything here for good code # ENV variables that need to be set correctly for this: # AUTH0_DOMAIN - # AUTH0_CLI_MACHINE_CLIENT_ID - # AUTH0_CLI_MACHINE_CLIENT_SECRET - # trying getting them from the auth0 a0deploy-cli-client settings in auth0 parser = argparse.ArgumentParser( description='Check the user metadata and org membership for some emails.') parser.add_argument( 'emails', metavar='EMAIL', nargs='+', help='An email to find the user metadata and group membership for') args = parser.parse_args() # Get an Access Token from Auth0 base_url = f"https://{os.environ.get('AUTH0_DOMAIN')}" payload = { 'grant_type': 'client_credentials', 'client_id': os.environ.get('AUTH0_CLI_MACHINE_CLIENT_ID'), 'client_secret': os.environ.get('AUTH0_CLI_MACHINE_CLIENT_SECRET'), 'audience': f"https://{os.environ.get('AUTH0_DOMAIN')}/api/v2/" } response = requests.post(f'{base_url}/oauth/token', data=payload) # print(response) oauth = response.json() access_token = oauth.get('access_token') # print(access_token) # Add the token to the Authorization header of the request headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json; ; charset=utf-8' } # curl https://login.auth0.com/api/v2/users-by-email?email=amccown%40theorchard.com def get_user_by_email(email): res = requests.get( f'{base_url}/api/v2/users-by-email?email={email}', headers=headers) if res.status_code >= 300: return False return res.json() def get_organization_for_id(auth0id): """Return the organizations that ID is a member of. Results are full data for the orgs, or False if that ID is not found. Params: id (str): the auth0 id to get org membership for """ res = requests.get( f'{base_url}/api/v2/users/{auth0id}/organizations', headers=headers) if res.status_code >= 300: return False return res.json() def main(): """Excecute the script.""" for email in args.emails: print('Searching for user with email: ' + email) users = get_user_by_email(email) if users: for user in users: # print(user) auth0_id = user['user_id'] print("Auth0 id: " + auth0_id) print("User metadata: " + str(user['user_metadata'])) if 'last_login' in user: print("last login: " + user['last_login']) if 'logins_count' in user: print("logins count: " + str(user['logins_count'])) if 'blocked' in user: print("User is blocked!") orgs = get_organization_for_id(auth0_id) if orgs: for org in orgs: print('In org: ' + org['display_name']) else: print('Not a member of any orgs') print("-----------") else: print('email not found') if __name__ == '__main__': main()