This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path__init__.py
917 lines (731 loc) · 28.7 KB
/
__init__.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
import configparser
import json
import os
import requests
from datetime import datetime
from requests.exceptions import ConnectTimeout, ReadTimeout
from subprocess import PIPE, Popen, TimeoutExpired
from typing import List, Tuple, Dict, Optional, Any
from urllib.parse import urljoin
from .sdlocalobjects import (
BaseError,
WrongUUIDError,
AuthError,
ReplyError,
Source,
Reply,
Submission,
)
DEFAULT_PROXY_VM_NAME = "sd-proxy"
DEFAULT_REQUEST_TIMEOUT = 20 # 20 seconds
DEFAULT_DOWNLOAD_TIMEOUT = 60 * 60 # 60 minutes
class RequestTimeoutError(Exception):
"""
Error raised if a request times out.
"""
def __init__(self) -> None:
super().__init__("The request timed out.")
def json_query(proxy_vm_name: str, data: str, timeout: Optional[int] = None) -> str:
"""
Takes a JSON based query and passes it to the network proxy.
Returns the JSON output from the proxy.
"""
p = Popen(
["/usr/lib/qubes/qrexec-client-vm", proxy_vm_name, "securedrop.Proxy"],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
)
p.stdin.write(data.encode("utf-8"))
try:
stdout, _ = p.communicate(timeout=timeout) # type: (bytes, bytes)
except TimeoutExpired:
try:
p.terminate()
except Exception:
pass
raise RequestTimeoutError
else:
output = stdout.decode("utf-8")
return output.strip()
class API:
"""
This class handles all network calls to the SecureDrop API server.
:param address: Server URL (http://localhost:8081/)
:param username: Journalist username
:param passphrase: Journalist passphrase
:param totp: Current TOTP value
:param proxy: Whether the API class should use the RPC proxy
:param default_request_timeout: Default timeout for a request (non-download) in seconds
:param default_download_timeout: Default timeout for a request (download only) in seconds
:returns: An object of API class.
"""
def __init__(
self,
address: str,
username: str,
passphrase: str,
totp: str,
proxy: bool = False,
default_request_timeout: Optional[int] = None,
default_download_timeout: Optional[int] = None,
) -> None:
"""
Primary API class, this is the only thing which will make network call.
"""
self.server = address
self.username = username
self.passphrase = passphrase
self.totp = totp
self.token = None # type: Optional[str]
self.token_expiration = None # type: Optional[datetime]
self.token_journalist_uuid = None # type: Optional[str]
self.journalist_first_name = None # type: Optional[str]
self.journalist_last_name = None # type: Optional[str]
self.req_headers = dict() # type: Dict[str, str]
self.proxy = proxy # type: bool
self.default_request_timeout = (
default_request_timeout or DEFAULT_REQUEST_TIMEOUT
)
self.default_download_timeout = (
default_download_timeout or DEFAULT_DOWNLOAD_TIMEOUT
)
self.proxy_vm_name = DEFAULT_PROXY_VM_NAME
config = configparser.ConfigParser()
try:
if os.path.exists("/etc/sd-sdk.conf"):
config.read("/etc/sd-sdk.conf")
self.proxy_vm_name = config["proxy"]["name"]
except Exception:
pass # We already have a default name
def _send_json_request(
self,
method: str,
path_query: str,
body: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
timeout: Optional[int] = None,
) -> Tuple[Any, int, Dict[str, str]]:
if self.proxy: # We are using the Qubes securedrop-proxy
func = self._send_rpc_json_request
else: # We are not using the Qubes securedrop-proxy
func = self._send_http_json_request
return func(method, path_query, body, headers, timeout)
def _send_http_json_request(
self,
method: str,
path_query: str,
body: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
timeout: Optional[int] = None,
) -> Tuple[Any, int, Dict[str, str]]:
url = urljoin(self.server, path_query)
kwargs = {"headers": headers} # type: Dict[str, Any]
if timeout:
kwargs["timeout"] = timeout
if method == "POST":
kwargs["data"] = body
try:
result = requests.request(method, url, **kwargs)
except (ConnectTimeout, ReadTimeout):
raise RequestTimeoutError
# Because when we download a file there is no JSON in the body
if path_query.endswith("/download"):
return result, result.status_code, dict(result.headers)
return result.json(), result.status_code, dict(result.headers)
def _send_rpc_json_request(
self,
method: str,
path_query: str,
body: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
timeout: Optional[int] = None,
) -> Tuple[Any, int, Dict[str, str]]:
data = {"method": method, "path_query": path_query} # type: Dict[str, Any]
if method == "POST":
data["body"] = body
if headers is not None and headers:
data["headers"] = headers
data_str = json.dumps(data)
result = json.loads(json_query(self.proxy_vm_name, data_str, timeout))
return json.loads(result["body"]), result["status"], result["headers"]
def authenticate(self, totp: Optional[str] = None) -> bool:
"""
Authenticates the user and fetches the token from the server.
:returns: True if authentication is successful, raise AuthError otherwise.
"""
if totp is None:
totp = self.totp
user_data = {
"username": self.username,
"passphrase": self.passphrase,
"one_time_code": totp,
}
method = "POST"
path_query = "api/v1/token"
body = json.dumps(user_data)
try:
token_data, status_code, headers = self._send_json_request(
method, path_query, body=body, timeout=self.default_request_timeout
)
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "expiration" not in token_data:
raise AuthError("Authentication error")
self.token = token_data["token"]
self.token_expiration = datetime.strptime(
token_data["expiration"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
self.token_journalist_uuid = token_data["journalist_uuid"]
self.journalist_first_name = token_data["journalist_first_name"]
self.journalist_last_name = token_data["journalist_last_name"]
self.update_auth_header()
return True
def update_auth_header(self) -> None:
if self.token is not None:
self.req_headers = {
"Authorization": "Token " + self.token,
"Content-Type": "application/json",
"Accept": "application/json",
}
def get_sources(self) -> List[Source]:
"""
Returns a list of all the sources from the Server.
:returns: List of Source objects.
"""
path_query = "api/v1/sources"
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
sources = data["sources"]
result = [] # type: List[Source]
for source in sources:
s = Source(**source)
result.append(s)
return result
def get_source(self, source: Source) -> Source:
"""
This will return a single Source based on UUID.
:param source: Source object containing only source's UUID value.
:returns: Source object fetched from server for the given UUID value.
"""
path_query = "api/v1/sources/{}".format(source.uuid)
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
return Source(**data)
def get_source_from_string(self, uuid: str) -> Source:
"""
This will fetch a source from server and return it.
:param uuid: Source UUID as string.
:returns: Source object fetched from server for the given UUID value.
"""
s = Source(uuid=uuid)
return self.get_source(s)
def delete_source(self, source: Source) -> bool:
"""
This method will delete the source and collection. If the UUID
is not found in the server, it will raise WrongUUIDError.
:param source: Source object containing only source's UUID value.
:returns: True if successful, raises Errors in case of wrong values.
"""
path_query = "api/v1/sources/{}".format(source.uuid)
method = "DELETE"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
if "message" in data and data["message"] == "Source and submissions deleted":
return True
# We should never reach here
return False
def delete_source_from_string(self, uuid: str) -> bool:
"""
This method will delete the source and collection. If the UUID
is not found in the server, it will raise WrongUUIDError.
:param uuid: Source UUID as string.
:returns: True if the operation is successful.
"""
s = Source(uuid=uuid)
return self.delete_source(s)
def add_star(self, source: Source) -> bool:
"""
Adds a star to a given source.
:param source: The source object to whom we want add a star.
:returns: True if successful, raises Error otherwise.
"""
path_query = "api/v1/sources/{}/add_star".format(source.uuid)
method = "POST"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "message" in data and data["message"] == "Star added":
return True
return False
def remove_star(self, source: Source) -> bool:
"""Removes star from a given Source.
:param source: Source object to remove the star from.
:returns: True if successful, raises Error otherwise.
"""
path_query = "api/v1/sources/{}/remove_star".format(source.uuid)
method = "DELETE"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "message" in data and data["message"] == "Star removed":
return True
return False
def get_submissions(self, source: Source) -> List[Submission]:
"""
Returns a list of Submission objects from the server for a given source.
:param source: Source object for whom we want to find all the submissions.
:returns: List of Submission objects.
"""
path_query = "api/v1/sources/{}/submissions".format(source.uuid)
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing submission {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
result = [] # type: List[Submission]
values = data["submissions"]
for val in values:
s = Submission(**val)
result.append(s)
return result
def get_submission(self, submission: Submission) -> Submission:
"""
Returns the updated Submission object from the server.
:param submission: Submission object we want to update.
:returns: Updated submission object from the server.
"""
path_query = "api/v1/sources/{}/submissions/{}".format(
submission.source_uuid, submission.uuid
)
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing submission {}".format(submission.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
return Submission(**data)
def get_submission_from_string(self, uuid: str, source_uuid: str) -> Submission:
"""
Returns the updated Submission object from the server.
:param uuid: UUID of the Submission object.
:param source_uuid: UUID of the source.
:returns: Updated submission object from the server.
"""
s = Submission(uuid=uuid)
s.source_uuid = source_uuid
return self.get_submission(s)
def get_all_submissions(self) -> List[Submission]:
"""
Returns a list of Submission objects from the server.
:returns: List of Submission objects.
"""
path_query = "api/v1/submissions"
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
result = [] # type: List[Submission]
values = data["submissions"]
for val in values:
s = Submission(**val)
result.append(s)
return result
def delete_submission(self, submission: Submission) -> bool:
"""
Deletes a given Submission object from the server.
:param submission: The Submission object we want to delete in the server.
:returns: True if successful, raises Error otherwise.
"""
# Not using direct URL because this helps to use the same method
# from local submission (not fetched from server) objects.
# See the *from_string for an example.
path_query = "api/v1/sources/{}/submissions/{}".format(
submission.source_uuid, submission.uuid
)
method = "DELETE"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing submission {}".format(submission.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
if "message" in data and data["message"] == "Submission deleted":
return True
# We should never reach here
return False
def delete_submission_from_string(self, uuid: str, source_uuid: str) -> bool:
"""
Deletes a given Submission based on UUIDs from the server.
:param uuid: UUID of the Submission object.
:param source_uuid: UUID of the source.
:returns: Updated submission object from the server.
"""
s = Submission(uuid=uuid)
s.source_url = "/api/v1/sources/{}".format(source_uuid)
return self.delete_submission(s)
def download_submission(
self, submission: Submission, path: str = "", timeout: Optional[int] = None
) -> Tuple[str, str]:
"""
Returns a tuple of etag (format is algorithm:checksum) and file path for
a given Submission object. This method requires a directory path
at which to save the submission file.
:param submission: Submission object
:param path: Local directory path to save the submission
:returns: Tuple of etag and path of the saved submission.
"""
path_query = "api/v1/sources/{}/submissions/{}/download".format(
submission.source_uuid, submission.uuid
)
method = "GET"
if path:
if os.path.exists(path) and not os.path.isdir(path):
raise BaseError("Please provide a valid directory to save.")
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=timeout or self.default_download_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing submission {}".format(submission.uuid))
# Get the headers
headers = headers
if not self.proxy:
# This is where we will save our downloaded file
filepath = os.path.join(path, submission.filename)
with open(filepath, "wb") as fobj:
for chunk in data.iter_content(
chunk_size=1024
): # Getting 1024 in each chunk
if chunk:
fobj.write(chunk)
else:
filepath = os.path.join(
"/home/user/QubesIncoming/", self.proxy_vm_name, data["filename"]
)
return headers['Etag'].strip('\"'), filepath
def flag_source(self, source: Source) -> bool:
"""
Flags a source for reply.
:param source: Source object we want to flag.
:returns: True if successful, raises Error otherwise.
"""
path_query = "api/v1/sources/{}/flag".format(source.uuid)
method = "POST"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
return True
def get_current_user(self) -> Any:
"""
Returns a dictionary of the current user data.
Example:
{
'is_admin': True,
'last_login': '2018-08-01T19:10:38.199429Z',
'username': 'journalist'
}
"""
path_query = "api/v1/user"
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
return data
def reply_source(
self, source: Source, msg: str, reply_uuid: Optional[str] = None
) -> Reply:
"""
This method is used to reply to a given source. The message should be preencrypted with the
source's GPG public key.
:param source: Source object to whom we want to reply.
:param msg: Encrypted message with Source's GPG public key.
:param reply_uuid: The UUID that will be used to identify the reply on the server.
"""
path_query = "api/v1/sources/{}/replies".format(source.uuid)
method = "POST"
reply = {"reply": msg}
if reply_uuid:
reply["uuid"] = reply_uuid
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
body=json.dumps(reply),
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 400:
raise ReplyError(data["message"])
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
if "message" in data and data["message"] == "Your reply has been stored":
return Reply(uuid=data["uuid"], filename=data["filename"])
raise BaseError("Error handling HTTP response to sending a reply")
def get_replies_from_source(self, source: Source) -> List[Reply]:
"""
This will return a list of replies associated with a source.
:param source: Source object containing only source's UUID value.
:returns: List of Reply objects.
"""
path_query = "api/v1/sources/{}/replies".format(source.uuid)
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
result = []
for datum in data["replies"]:
reply = Reply(**datum)
result.append(reply)
return result
def get_reply_from_source(self, source: Source, reply_uuid: str) -> Reply:
"""
This will return a single specific reply.
:param source: Source object.
:param reply_uuid: UUID of the reply.
:returns: A reply object
"""
path_query = "api/v1/sources/{}/replies/{}".format(source.uuid, reply_uuid)
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing source {}".format(source.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
reply = Reply(**data)
return reply
def get_all_replies(self) -> List[Reply]:
"""
This will return a list of all replies from the server.
:returns: List of Reply objects.
"""
path_query = "api/v1/replies"
method = "GET"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
result = []
for datum in data["replies"]:
reply = Reply(**datum)
result.append(reply)
return result
def download_reply(self, reply: Reply, path: str = "") -> Tuple[str, str]:
"""
Returns a tuple of etag (format is algorithm:checksum) and file path for
a given Reply object. This method requires a directory path
at which to save the reply file.
:param reply: Reply object
:param path: Local directory path to save the reply
:returns: Tuple of etag and path of the saved Reply.
"""
path_query = "api/v1/sources/{}/replies/{}/download".format(
reply.source_uuid, reply.uuid
)
method = "GET"
if path:
if os.path.exists(path) and not os.path.isdir(path):
raise BaseError("Please provide a valid directory to save.")
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing reply {}".format(reply.uuid))
# Get the headers
headers = headers
if not self.proxy:
# This is where we will save our downloaded file
filepath = os.path.join(path, reply.filename)
with open(filepath, "wb") as fobj:
for chunk in data.iter_content(
chunk_size=1024
): # Getting 1024 in each chunk
if chunk:
fobj.write(chunk)
else:
filepath = os.path.join(
"/home/user/QubesIncoming/", self.proxy_vm_name, data["filename"]
)
return headers['Etag'].strip('\"'), filepath
def delete_reply(self, reply: Reply) -> bool:
"""
Deletes a given Reply object from the server.
:param reply: The Reply object we want to delete in the server.
:returns: True if successful, raises Error otherwise.
"""
# Not using direct URL because this helps to use the same method
# from local reply (not fetched from server) objects.
# See the *from_string for an example.
path_query = "api/v1/sources/{}/replies/{}".format(
reply.source_uuid, reply.uuid
)
method = "DELETE"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
if status_code == 404:
raise WrongUUIDError("Missing reply {}".format(reply.uuid))
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
if "message" in data and data["message"] == "Reply deleted":
return True
# We should never reach here
return False
def logout(self) -> bool:
"""
Logs the current user out.
"""
path_query = "api/v1/logout"
method = "POST"
try:
data, status_code, headers = self._send_json_request(
method,
path_query,
headers=self.req_headers,
timeout=self.default_request_timeout,
)
except json.decoder.JSONDecodeError:
raise BaseError("Error in parsing JSON")
if "error" in data:
raise AuthError(data["error"])
if "message" in data and data["message"] == "Your token has been revoked.":
return True
else:
return False