"""Client. This logic layer is in charge of verifying information related to the client (such as finding if the client exists and if the secret is valid). Both information are required to complete authentication on the behalf of a user. """ from sukimu.operations import Equal from auth.models import client def get_client(client_id): """Get a client by its id. Args: client_id (int): the client's id. Returns: Response: the client data (if any) """ return client.fetch_one(client_id=Equal(client_id)) def confirm(client_id, secret): """Confirm the identity of a client. Each client is assigned with a unique id and a secret. Those two information allows us to know who is trying to do what and on whom's behalf. This method verifies also the integrity of our system and throws exceptions whenever the information provided are not valid (this allows us to be immediately aware of issues that might be happening in our systems or security breaches). If we ever make our APIs public, those will have to be silenced by converting them into Response(s). Args: client_id (int): the client's id. secret (str): the client's secret. Returns: Response: the response, which, if valid, contains the client details. Raises: AssertionError: this is a security issue, someone is trying to perform a request without providing the right secret. We need to look more into this to make sure this is a valid request. Exception: this exception only happens when a client id is not valid or has not been provided. """ response = get_client(client_id) if not response.success: raise Exception( 'The client {} is performing a request, but does not exists in ' 'our codebase, please investigate'.format(client_id)) current_client = response.message assert current_client.secret == secret, ( 'The client {} is performing a request without providing the right ' 'secret, please investigate.'.format(client_id)) return response