"""Methods for operating against Auth0 environments.""" import json from auth0_sync_env import config from auth0_sync_env.connectors import logging from auth0_sync_env.models import auth0_client logger = logging.get_current_logger() def delete_all_users( auth0_domain, auth0_api_url, auth0_machine_client_id, auth0_machine_client_secret, auth0_connection ): """Delete all users in specified target Auth0 environment. Args: auth0_domain: (str) Auth0 domain (whatever.auth0.com). auth0_api_url: (str) Auth0 API url. auth0_machine_client_id: (str) Machine client id. auth0_machine_client_secret: (str) Machine client secret. auth0_connection: (str) Auth0 db connection (art-relations). """ # get a management token token_response = auth0_client.get_auth0_management_token( auth0_domain, auth0_api_url, auth0_machine_client_id, auth0_machine_client_secret ) if token_response.status != 200: return token_response page = 0 users_response = auth0_client.get_auth0_users_paginated( auth0_domain, token_response.message.get('access_token'), auth0_connection, page, config.USERS_PER_PAGE) if users_response.status != 200: raise Exception('Call to auth0 GET USERS resulted in {}'.format( users_response.status)) users = users_response.message while len(users): for user in users: logger.info(msg=json.dumps( {'action': 'delete_auth0_user', 'user': user})) delete_response = auth0_client.delete_auth0_user( auth0_domain, token_response.message.get('access_token'), user.get('user_id')) if delete_response.status != 200: logger.error( msg='Call to auth0 DELETE USER resulted in {}'.format( delete_response.status)) raise Exception( 'Call to auth0 DELETE USER resulted in {}'.format( delete_response.status)) page += 1 users_response = auth0_client.get_auth0_users_paginated( auth0_domain, token_response.message.get('access_token'), auth0_connection, page, config.USERS_PER_PAGE) if users_response.status != 200: raise Exception( 'Call to auth0 GET USERS resulted in {}'.format( users_response.status)) users = users_response.message def transform_user(user, auth0_connection): """Transform certain user metadata befor syncing. Params: user: (dict) The user dict to transform. auth0_connection: (string) The connection to add the user to. Returns: dict: The transformed user. """ email = config.SYNC_EMAIL_TEMPLATE.format( user.get('user_id')) user_metadata = user.get('user_metadata', {}) user_metadata['source_email'] = user.get('email') new_user = { 'connection': auth0_connection, 'name': email, 'email': email, 'password': config.SYNC_PASSWORD, 'user_metadata': user_metadata, 'email_verified': True, 'verify_email': False, 'app_metadata': user.get('app_metadata', {}) } return new_user def sync_users_across_env( source_auth0_domain, source_auth0_api_url, source_auth0_machine_client_id, source_auth0_machine_client_secret, source_auth0_connection, target_auth0_domain, target_auth0_api_url, target_auth0_machine_client_id, target_auth0_machine_client_secret, target_auth0_connection ): """Sync all users from source to target Auth0 env. Args: source_auth0_domain: (str) Auth0 domain (whatever.auth0.com). source_auth0_api_url: (str) Auth0 API url. source_auth0_machine_client_id: (str) Machine client id. source_auth0_machine_client_secret: (str) Machine client secret. source_auth0_connection: (str) Auth0 db connection (art-relations). target_auth0_domain: (str) Auth0 domain (whatever.auth0.com). target_auth0_api_url: (str) Auth0 API url. target_auth0_machine_client_id: (str) Machine client id. target_auth0_machine_client_secret: (str) Machine client secret. target_auth0_connection: (str) Auth0 db connection (art-relations). """ source_token_response = auth0_client.get_auth0_management_token( source_auth0_domain, source_auth0_api_url, source_auth0_machine_client_id, source_auth0_machine_client_secret ) if source_token_response.status != 200: logger.error( msg='Call to auth0 GET MANAGEMENT TOKEN resulted an error') return source_token_response target_token_response = auth0_client.get_auth0_management_token( target_auth0_domain, target_auth0_api_url, target_auth0_machine_client_id, target_auth0_machine_client_secret ) if target_token_response.status != 200: return target_token_response page = 0 users_response = auth0_client.get_auth0_users_paginated( source_auth0_domain, source_token_response.message.get('access_token'), source_auth0_connection, page, config.USERS_PER_PAGE) if users_response.status != 200: raise Exception('Call to auth0 GET USERS resulted in {}'.format( users_response.status)) users = users_response.message while len(users): for user in users: new_user = transform_user( user, target_auth0_connection) logger.info(msg=json.dumps( {'action': 'create_auth0_user', 'user': new_user})) create_response = auth0_client.create_auth0_user( target_auth0_domain, target_token_response.message.get('access_token'), new_user) if create_response.status != 200: logger.error( msg='Call to auth0 CREATE USER resulted in {}: {}'.format( create_response.status, create_response.errors.get('message'))) print( 'Call to auth0 CREATE USER resulted in {}: {}'.format( create_response.status, create_response.errors.get('message'))) page += 1 users_response = auth0_client.get_auth0_users_paginated( source_auth0_domain, source_token_response.message.get('access_token'), source_auth0_connection, page, config.USERS_PER_PAGE) if users_response.status != 200: raise Exception( 'Call to auth0 GET USERS resulted in {}'.format( users_response.status)) users = users_response.message