"""Auth0 client.""" from auth0.v3.authentication import GetToken from auth0.v3.exceptions import Auth0Error from auth0.v3.management.users import Users from auth0.v3.management.users_by_email import UsersByEmail from oto import response from requests.exceptions import ConnectionError def get_auth0_management_token( auth0_domain, auth0_api_url, auth0_machine_client_id, auth0_machine_client_secret ): """Retreive an auth0 management token. 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. Returns: response.Response: Response contains token in message. """ get_token = GetToken(auth0_domain) try: token = get_token.client_credentials( auth0_machine_client_id, auth0_machine_client_secret, auth0_api_url) except Auth0Error as auth0_error: return response.create_error_response( code=auth0_error.error_code, message=auth0_error.message, status=auth0_error.status_code ) except ConnectionError: return response.create_error_response( code='connection_error', message='Error with Auth0 domain', status=400 ) return response.Response(message=token) def get_auth0_users_paginated( auth0_domain, auth0_access_token, auth0_connection, page=0, per_page=100 ): """Get a page of auth0 users. Params: auth0_domain: (str) Auth0 domain. auth0_access_token: (str) Auth0 management token. auth0_connection: (str) Auth0 db connection. page: Zero-indexed page to fetch per_page: Number of items per page (max 100) Returns: response.Response: contains the page of users. """ users_obj = Users(auth0_domain, auth0_access_token) result = users_obj.list( page=page, per_page=per_page, search_engine='v3', q='identities.connection:"{}"'.format(auth0_connection)) return response.Response(result['users']) def delete_auth0_user( auth0_domain, auth0_access_token, user_id ): """Delete Auth0 user. Params: auth0_domain: (str) Auth0 domain. auth0_access_token: (str) Auth0 management token. user_id: (str) Auth0 user id. Returns: response.Response """ users_obj = Users(auth0_domain, auth0_access_token) users_obj.delete(user_id) return response.Response() def create_auth0_user( auth0_domain, auth0_access_token, user ): """Create an Auth0 user. Params: auth0_domain: (str) Auth0 domain. auth0_access_token: (str) Auth0 management token. user: (dict) User object as per Auth0 create user endpoint. Returns: response.Response: contains the new user details. """ users_obj = Users(auth0_domain, auth0_access_token) try: result = users_obj.create(user) except Auth0Error as auth0_error: return response.create_error_response( code=auth0_error.error_code, message=auth0_error.message, status=auth0_error.status_code ) return response.Response(message=result) def get_auth0_user( auth0_domain, auth0_access_token, auth0_user_id ): """Get an auth0 user. Params: auth0_domain: (str) Auth0 domain. auth0_access_token: (str) Auth0 management token. auth0_user_id: (str) Auth0 user id to fetch Returns: response.Response: contains the new user details. """ users_obj = Users(auth0_domain, auth0_access_token) try: result = users_obj.get(auth0_user_id) except Auth0Error as auth0_error: return response.create_error_response( code=auth0_error.error_code, message=auth0_error.message, status=auth0_error.status_code ) return response.Response(message=result) def search_auth0_users_by_email( auth0_domain, auth0_access_token, email ): """Search auth0 users by email. Params: auth0_domain: (str) Auth0 domain. auth0_access_token: (str) Auth0 management token. email: (str) Email address to search Returns: response.Response: contains the new user details. """ users_obj = UsersByEmail(auth0_domain, auth0_access_token) try: result = users_obj.search_users_by_email(email) except Auth0Error as auth0_error: return response.create_error_response( code=auth0_error.error_code, message=auth0_error.message, status=auth0_error.status_code ) return response.Response(message=result)