Replace tpo git repository URL by gitlab
[stem.git] / stem / response / onion_client_auth.py
blob8735808caab8a9e638b5dbf6513c1a7ac5df81d7
1 # Copyright 2020, Damian Johnson and The Tor Project
2 # See LICENSE for licensing information
4 import stem.control
5 import stem.response
8 class OnionClientAuthViewResponse(stem.response.ControlMessage):
9 """
10 ONION_CLIENT_AUTH_VIEW response.
12 :var str requested: queried hidden service id, this is None if all
13 credentials were requested
14 :var dict credentials: mapping of hidden service ids to their
15 :class:`~stem.control.HiddenServiceCredential`
16 """
18 def _parse_message(self) -> None:
19 # Example:
20 # 250-ONION_CLIENT_AUTH_VIEW yvhz3ofkv7gwf5hpzqvhonpr3gbax2cc7dee3xcnt7dmtlx2gu7vyvid
21 # 250-CLIENT yvhz3ofkv7gwf5hpzqvhonpr3gbax2cc7dee3xcnt7dmtlx2gu7vyvid x25519:FCV0c0ELDKKDpSFgVIB8Yow8Evj5iD+GoiTtK878NkQ=
22 # 250 OK
24 self.requested = None
25 self.credentials = {}
27 if not self.is_ok():
28 raise stem.ProtocolError("ONION_CLIENT_AUTH_VIEW response didn't have an OK status: %s" % self)
30 # first line optionally contains the service id this request was for
32 first_line = list(self)[0]
34 if not first_line.startswith('ONION_CLIENT_AUTH_VIEW'):
35 raise stem.ProtocolError("Response should begin with 'ONION_CLIENT_AUTH_VIEW': %s" % self)
36 elif ' ' in first_line:
37 self.requested = first_line.split(' ')[1]
39 for credential_line in list(self)[1:-1]:
40 attributes = credential_line.split(' ')
42 if len(attributes) < 3:
43 raise stem.ProtocolError('ONION_CLIENT_AUTH_VIEW lines must contain an address and credential: %s' % self)
44 elif attributes[0] != 'CLIENT':
45 raise stem.ProtocolError("ONION_CLIENT_AUTH_VIEW lines should begin with 'CLIENT': %s" % self)
46 elif ':' not in attributes[2]:
47 raise stem.ProtocolError("ONION_CLIENT_AUTH_VIEW credentials must be of the form 'encryption_type:key': %s" % self)
49 service_id = attributes[1]
50 key_type, private_key = attributes[2].split(':', 1)
51 client_name = None
52 flags = []
54 for attr in attributes[2:]:
55 if '=' not in attr:
56 raise stem.ProtocolError("'%s' expected to be a 'key=value' mapping: %s" % (attr, self))
58 key, value = attr.split('=', 1)
60 if key == 'ClientName':
61 client_name = value
62 elif key == 'Flags':
63 flags = value.split(',')
65 self.credentials[service_id] = stem.control.HiddenServiceCredential(service_id, private_key, key_type, client_name, flags)