File: authchallenge.py

package info (click to toggle)
python-stem 1.2.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,568 kB
  • ctags: 2,036
  • sloc: python: 20,108; makefile: 127; sh: 3
file content (56 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 2012-2014, Damian Johnson and The Tor Project
# See LICENSE for licensing information

import binascii

import stem.response
import stem.socket
import stem.util.str_tools
import stem.util.tor_tools


class AuthChallengeResponse(stem.response.ControlMessage):
  """
  AUTHCHALLENGE query response.

  :var str server_hash: server hash provided by tor
  :var str server_nonce: server nonce provided by tor
  """

  def _parse_message(self):
    # Example:
    #   250 AUTHCHALLENGE SERVERHASH=680A73C9836C4F557314EA1C4EDE54C285DB9DC89C83627401AEF9D7D27A95D5 SERVERNONCE=F8EA4B1F2C8B40EF1AF68860171605B910E3BBCABADF6FC3DB1FA064F4690E85

    self.server_hash = None
    self.server_nonce = None

    if not self.is_ok():
      raise stem.ProtocolError("AUTHCHALLENGE response didn't have an OK status:\n%s" % self)
    elif len(self) > 1:
      raise stem.ProtocolError('Received multiline AUTHCHALLENGE response:\n%s' % self)

    line = self[0]

    # sanity check that we're a AUTHCHALLENGE response
    if not line.pop() == 'AUTHCHALLENGE':
      raise stem.ProtocolError('Message is not an AUTHCHALLENGE response (%s)' % self)

    if line.is_next_mapping('SERVERHASH'):
      value = line.pop_mapping()[1]

      if not stem.util.tor_tools.is_hex_digits(value, 64):
        raise stem.ProtocolError('SERVERHASH has an invalid value: %s' % value)

      self.server_hash = binascii.a2b_hex(stem.util.str_tools._to_bytes(value))
    else:
      raise stem.ProtocolError('Missing SERVERHASH mapping: %s' % line)

    if line.is_next_mapping('SERVERNONCE'):
      value = line.pop_mapping()[1]

      if not stem.util.tor_tools.is_hex_digits(value, 64):
        raise stem.ProtocolError('SERVERNONCE has an invalid value: %s' % value)

      self.server_nonce = binascii.a2b_hex(stem.util.str_tools._to_bytes(value))
    else:
      raise stem.ProtocolError('Missing SERVERNONCE mapping: %s' % line)