class ChallengeRequestState(ServerSessionState): """Authenticating ourselves with the server. """ accepted_messages = ServerSessionState.accepted_messages + \ ['CHALLENGE_REQUEST_RESPONSE', 'ERROR', 'UNKNOWN_CLIENT_ID_ERROR'] def __init__(self, session): ServerSessionState.__init__(self, session) self.crypto = CryptoUtil() def _handle_message_CHALLENGE_REQUEST_RESPONSE(self, message): """The server has agreed to start the authentication and has sent us a challange to sign. """ challenge = str(message.getParameter('challenge')) # Sign challenge with private key signed = self.crypto.challenge_sign( challenge, open(self._context.priv_key, 'r').read()) # Create CHALLENGE_RESPONSE with signed challenge challenge_response_msg = CHALLENGE_RESPONSE( 'CHALLENGE_RESPONSE', { 'client_id': self._context.client_id, 'response': signed }) if self._context.disconnect_other_client: key = "force_other_client_disconnection" challenge_response_msg.parameters[key] = True # challenge_response_msg.set_force_other_client_disconnection() self._context.disconnect_other_client = False self._context.output_message_queue.put(challenge_response_msg) self._set_next_state(StateRegister.get('ChallengeResponseState')) def _handle_message_UNKNOWN_CLIENT_ID_ERROR(self, message): """The server didn't recognize our client, go relinking. """ self.logger.error(u'Server refused the client id: %s' % self._context.client_id) relink_user(self) def _handle_message_ERROR(self, message): """Received an error from the server. Was it because of an invalid username? """ error_code = message.getParameter('error_code') reason = message.getParameter('reason') if error_code != UNEXPECTED_DATA \ or not reason.startswith("Invalid username provided"): ServerSessionState._handle_message_ERROR(self, message) return self.logger.error('Server refused the username: %s' % self._context.username) relink_user(self)
class ChallengeRequestState(ServerSessionState): """Authenticating ourselves with the server. """ accepted_messages = ServerSessionState.accepted_messages + [ "CHALLENGE_REQUEST_RESPONSE", "ERROR", "UNKNOWN_CLIENT_ID_ERROR", ] def __init__(self, session): ServerSessionState.__init__(self, session) self.crypto = CryptoUtil() def _handle_message_CHALLENGE_REQUEST_RESPONSE(self, message): """The server has agreed to start the authentication and has sent us a challange to sign. """ challenge = str(message.getParameter("challenge")) # Sign challenge with private key signed = self.crypto.challenge_sign(challenge, open(self._context.priv_key, "r").read()) # Create CHALLENGE_RESPONSE with signed challenge challenge_response_msg = CHALLENGE_RESPONSE( "CHALLENGE_RESPONSE", {"client_id": self._context.client_id, "response": signed} ) if self._context.disconnect_other_client: key = "force_other_client_disconnection" challenge_response_msg.parameters[key] = True # challenge_response_msg.set_force_other_client_disconnection() self._context.disconnect_other_client = False self._context.output_message_queue.put(challenge_response_msg) self._set_next_state(StateRegister.get("ChallengeResponseState")) def _handle_message_UNKNOWN_CLIENT_ID_ERROR(self, message): """The server didn't recognize our client, go relinking. """ self.logger.error(u"Server refused the client id: %s" % self._context.client_id) relink_user(self) def _handle_message_ERROR(self, message): """Received an error from the server. Was it because of an invalid username? """ error_code = message.getParameter("error_code") reason = message.getParameter("reason") if error_code != UNEXPECTED_DATA or not reason.startswith("Invalid username provided"): ServerSessionState._handle_message_ERROR(self, message) return self.logger.error("Server refused the username: %s" % self._context.username) relink_user(self)
def __init__(self, session): ServerSessionState.__init__(self, session) self.crypto = CryptoUtil()