Browse Source

little refactoring

dependabot/add-v2-config-file
R4SAS 6 years ago
parent
commit
d37e573d9e
  1. 26
      pbincli/actions.py
  2. 23
      pbincli/api.py
  3. 20
      pbincli/format.py

26
pbincli/actions.py

@ -135,18 +135,7 @@ def get(args, api_client):
if version == 1 and 'meta' in result and 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']: if version == 1 and 'meta' in result and 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']:
print("Burn afrer reading flag found. Deleting paste...") print("Burn afrer reading flag found. Deleting paste...")
result = api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':'burnafterreading'})) api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':'burnafterreading'}))
if args.debug: print("Delete response:\t{}\n".format(result))
if 'status' in result and not result['status']:
print("Paste successfully deleted!")
elif 'status' in result and result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message']))
exit(1)
else:
print("Something went wrong...\nError: Empty response.")
exit(1)
elif 'status' in result and result['status']: elif 'status' in result and result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message'])) print("Something went wrong...\nError:\t\t{}".format(result['message']))
@ -164,15 +153,4 @@ def delete(args, api_client):
if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token)) if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token))
result = api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':token})) api_client.delete(json_encode({'pasteid':pasteid,'deletetoken':token}))
if args.debug: print("Response:\t{}\n".format(result))
if 'status' in result and not result['status']:
print("Paste successfully deleted!")
elif 'status' in result and result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message']))
exit(1)
else:
print("Something went wrong...\nError: Empty response.")
exit(1)

23
pbincli/api.py

@ -32,20 +32,27 @@ class PrivateBin:
def delete(self, request): def delete(self, request):
result = requests.post(
url = self.server,
headers = self.headers,
proxies = self.proxy,
data = request)
# using try as workaround for versions < 1.3 due to we cant detect # using try as workaround for versions < 1.3 due to we cant detect
# if server used version 1.2, where auto-deletion is added # if server used version 1.2, where auto-deletion is added
try: try:
return result.json() result = requests.post(
url = self.server,
headers = self.headers,
proxies = self.proxy,
data = request).json()
except ValueError as e: except ValueError as e:
# unable parse response as json because it can be empty (1.2), so simulate correct answer # unable parse response as json because it can be empty (1.2), so simulate correct answer
from json import loads as json_loads from json import loads as json_loads
return json_loads('{"status":0}') result = json_loads('{"status":0}')
if not result['status']:
print("Paste successfully deleted!")
elif result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message']))
exit(1)
else:
print("Something went wrong...\nError: Empty response.")
exit(1)
def getVersion(self): def getVersion(self):

20
pbincli/format.py

@ -120,6 +120,14 @@ class Paste:
return cipher return cipher
def __preparePassKey(self):
if self._password:
digest = sha256(self._password.encode("UTF-8")).hexdigest()
return b64encode(self._key) + digest.encode("UTF-8")
else:
return b64encode(self._key)
def __decompress(self, s): def __decompress(self, s):
if self._version == 2: if self._version == 2:
if self._compression == 'zlib': if self._compression == 'zlib':
@ -178,11 +186,7 @@ class Paste:
from hashlib import sha256 from hashlib import sha256
from sjcl import SJCL from sjcl import SJCL
if self._password: password = self.__preparePassKey()
digest = sha256(self._password.encode("UTF-8")).hexdigest()
password = b64encode(self._key) + digest.encode("UTF-8")
else:
password = b64encode(self._key)
cipher_text = json_decode(self._data['data']) cipher_text = json_decode(self._data['data'])
@ -245,11 +249,7 @@ class Paste:
self._data = {'expire':expiration,'formatter':formatter,'burnafterreading':int(burnafterreading),'opendiscussion':int(discussion)} self._data = {'expire':expiration,'formatter':formatter,'burnafterreading':int(burnafterreading),'opendiscussion':int(discussion)}
if self._password: password = self.__preparePassKey()
digest = sha256(self._password.encode("UTF-8")).hexdigest()
password = b64encode(self._key) + digest.encode("UTF-8")
else:
password = b64encode(self._key)
if self._debug: print("Password:\t{}".format(password)) if self._debug: print("Password:\t{}".format(password))

Loading…
Cancel
Save