def write(self, writer: WriteStream) -> None: for name in self.names: name_str = String.build(name) name_str.write(writer) if self.active and name == self.active: writer.write(b' ACTIVE\r\n') else: writer.write(b'\r\n') super().write(writer)
def write(self, writer: WriteStream) -> None: writer.write(bytes(self.condition)) if self.code is not None: writer.write(b' (%b)' % bytes(self.code)) if self.text is not None: text_bytes = self.text.rstrip('\r\n').encode('utf-8', 'replace') text_str = String.build(text_bytes) writer.write(b' ') text_str.write(writer) writer.write(b'\r\n')
async def _do_authenticate(self, login: LoginProtocol, cmd: AuthenticateCommand) -> Response: mech = self.auth.get_server(cmd.mech_name) if not mech: return Response(Condition.NO, text='Invalid SASL mechanism.') responses: List[ServerChallenge] = [] if cmd.initial_data is not None: chal = ServerChallenge(b'') chal.set_response(b64decode(cmd.initial_data)) responses.append(chal) while True: try: creds, final = mech.server_attempt(responses) except ServerChallenge as chal: chal_bytes = b64encode(chal.get_challenge()) chal_str = String.build(chal_bytes) chal_str.write(self.writer) self.writer.write(b'\r\n') await self.writer.drain() resp_bytes = await self._read_data() resp_str, _ = String.parse(resp_bytes, self.params) chal.set_response(b64decode(resp_str.value)) if resp_str.value == b'*': raise AuthenticationError('Authentication cancelled.') responses.append(chal) except AuthenticationError as exc: return Response(Condition.NO, text=str(exc)) else: break if final is None: code: Optional[bytes] = None else: code = BytesFormat(b'SASL %b') % String.build(final) try: session = await login(creds, self.config) except InvalidAuth as exc: return Response(Condition.NO, text=str(exc)) else: if session.filter_set is None: return Response(Condition.NO, text='Filters not supported.') self._session = session return Response(Condition.OK, code=code)
async def _do_authenticate(self, cmd: AuthenticateCommand) -> Response: mech = self.auth.get_server(cmd.mech_name) if not mech: return Response(Condition.NO, text='Invalid SASL mechanism.') responses: List[ServerChallenge] = [] if cmd.initial_data is not None: chal = ServerChallenge(b'') chal.set_response(b64decode(cmd.initial_data)) responses.append(chal) while True: try: creds, final = mech.server_attempt(responses) except ServerChallenge as chal: chal_bytes = b64encode(chal.get_challenge()) chal_str = String.build(chal_bytes) chal_str.write(self.writer) self.writer.write(b'\r\n') await self.writer.drain() resp_bytes = await self._read_data() resp_str, _ = String.parse(resp_bytes, self.params) chal.set_response(b64decode(resp_str.value)) if resp_str.value == b'*': raise AuthenticationError('Authentication cancelled.') responses.append(chal) except AuthenticationError as exc: return Response(Condition.NO, text=str(exc)) else: break if final is None: code: Optional[bytes] = None else: code = BytesFormat(b'SASL %b') % String.build(final) try: session = await self._login(creds) except InvalidAuth as exc: return Response(Condition.NO, text=str(exc)) try: self._state = self._get_state(session) except ValueError as exc: return Response(Condition.NO, text=str(exc)) return Response(Condition.OK, code=code)
def test_build_binary(self): ret = String.build(b'\x00\x01', True) self.assertEqual(b'\x00\x01', ret.value) self.assertTrue(ret.binary) self.assertEqual(b'~{2}\r\n\x00\x01', bytes(ret))
def __init__(self, tag: Optional[bytes]) -> None: if tag is None: code: Optional[bytes] = None else: code = BytesFormat(b'TAG %b') % String.build(tag) super().__init__(Condition.OK, code=code)