Replace tpo git repository URL by gitlab
[stem.git] / stem / response / authchallenge.py
blob80a1c0f5173374d59f796b2e45492565946f57fe
1 # Copyright 2012-2020, Damian Johnson and The Tor Project
2 # See LICENSE for licensing information
4 import binascii
6 import stem.response
7 import stem.socket
8 import stem.util.str_tools
9 import stem.util.tor_tools
12 class AuthChallengeResponse(stem.response.ControlMessage):
13 """
14 AUTHCHALLENGE query response.
16 :var str server_hash: server hash provided by tor
17 :var str server_nonce: server nonce provided by tor
18 """
20 def _parse_message(self) -> None:
21 # Example:
22 # 250 AUTHCHALLENGE SERVERHASH=680A73C9836C4F557314EA1C4EDE54C285DB9DC89C83627401AEF9D7D27A95D5 SERVERNONCE=F8EA4B1F2C8B40EF1AF68860171605B910E3BBCABADF6FC3DB1FA064F4690E85
24 self.server_hash = None
25 self.server_nonce = None
27 if not self.is_ok():
28 raise stem.ProtocolError("AUTHCHALLENGE response didn't have an OK status:\n%s" % self)
29 elif len(self) > 1:
30 raise stem.ProtocolError('Received multiline AUTHCHALLENGE response:\n%s' % self)
32 line = self[0]
34 # sanity check that we're a AUTHCHALLENGE response
35 if not line.pop() == 'AUTHCHALLENGE':
36 raise stem.ProtocolError('Message is not an AUTHCHALLENGE response (%s)' % self)
38 if line.is_next_mapping('SERVERHASH'):
39 value = line.pop_mapping()[1]
41 if not stem.util.tor_tools.is_hex_digits(value, 64):
42 raise stem.ProtocolError('SERVERHASH has an invalid value: %s' % value)
44 self.server_hash = binascii.unhexlify(stem.util.str_tools._to_bytes(value))
45 else:
46 raise stem.ProtocolError('Missing SERVERHASH mapping: %s' % line)
48 if line.is_next_mapping('SERVERNONCE'):
49 value = line.pop_mapping()[1]
51 if not stem.util.tor_tools.is_hex_digits(value, 64):
52 raise stem.ProtocolError('SERVERNONCE has an invalid value: %s' % value)
54 self.server_nonce = binascii.unhexlify(stem.util.str_tools._to_bytes(value))
55 else:
56 raise stem.ProtocolError('Missing SERVERNONCE mapping: %s' % line)