2020-07-15 23:46:04 +08:00
|
|
|
import functools
|
|
|
|
import logging
|
|
|
|
from typing import Dict
|
2020-04-16 21:39:10 +08:00
|
|
|
|
2019-09-13 21:12:58 +08:00
|
|
|
import requests
|
|
|
|
|
2021-09-17 17:24:05 +08:00
|
|
|
ISLAND_USERNAME = "test"
|
|
|
|
ISLAND_PASSWORD = "test"
|
2019-10-01 21:11:53 +08:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2019-09-13 21:12:58 +08:00
|
|
|
|
|
|
|
|
2021-05-20 13:42:51 +08:00
|
|
|
class AuthenticationFailedError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-09-02 22:36:19 +08:00
|
|
|
class InvalidRegistrationCredentialsError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-10-30 20:38:17 +08:00
|
|
|
# noinspection PyArgumentList
|
2019-09-13 21:12:58 +08:00
|
|
|
class MonkeyIslandRequests(object):
|
|
|
|
def __init__(self, server_address):
|
|
|
|
self.addr = "https://{IP}/".format(IP=server_address)
|
|
|
|
self.token = self.try_get_jwt_from_server()
|
|
|
|
|
|
|
|
def try_get_jwt_from_server(self):
|
|
|
|
try:
|
|
|
|
return self.get_jwt_from_server()
|
2021-05-20 13:42:51 +08:00
|
|
|
except AuthenticationFailedError:
|
2021-09-02 22:36:19 +08:00
|
|
|
self.try_set_island_to_credentials()
|
2021-05-20 13:42:51 +08:00
|
|
|
return self.get_jwt_from_server()
|
2021-09-02 22:36:19 +08:00
|
|
|
except (requests.ConnectionError, InvalidRegistrationCredentialsError) as err:
|
2019-10-06 20:05:34 +08:00
|
|
|
LOGGER.error(
|
2021-04-06 21:19:27 +08:00
|
|
|
"Unable to connect to island, aborting! Error information: {}. Server: {}".format(
|
|
|
|
err, self.addr
|
|
|
|
)
|
|
|
|
)
|
2019-09-13 21:12:58 +08:00
|
|
|
assert False
|
|
|
|
|
2021-05-20 13:42:51 +08:00
|
|
|
def get_jwt_from_server(self):
|
|
|
|
resp = requests.post( # noqa: DUO123
|
2022-08-01 21:29:55 +08:00
|
|
|
self.addr + "api/authenticate",
|
2021-09-02 22:36:19 +08:00
|
|
|
json={"username": ISLAND_USERNAME, "password": ISLAND_PASSWORD},
|
2021-05-20 13:42:51 +08:00
|
|
|
verify=False,
|
|
|
|
)
|
|
|
|
if resp.status_code == 401:
|
|
|
|
raise AuthenticationFailedError
|
|
|
|
return resp.json()["access_token"]
|
|
|
|
|
2021-09-02 22:36:19 +08:00
|
|
|
def try_set_island_to_credentials(self):
|
|
|
|
resp = requests.post( # noqa: DUO123
|
2022-08-01 21:19:22 +08:00
|
|
|
self.addr + "api/register",
|
2021-10-14 20:52:13 +08:00
|
|
|
json={"username": ISLAND_USERNAME, "password": ISLAND_PASSWORD},
|
2021-09-02 22:36:19 +08:00
|
|
|
verify=False,
|
2021-05-20 13:42:51 +08:00
|
|
|
)
|
2021-09-02 22:36:19 +08:00
|
|
|
if resp.status_code == 400:
|
|
|
|
raise InvalidRegistrationCredentialsError("Missing part of the credentials")
|
2021-05-20 13:42:51 +08:00
|
|
|
|
2019-10-30 20:38:17 +08:00
|
|
|
class _Decorators:
|
|
|
|
@classmethod
|
|
|
|
def refresh_jwt_token(cls, request_function):
|
|
|
|
@functools.wraps(request_function)
|
2019-11-27 19:39:47 +08:00
|
|
|
def request_function_wrapper(self, *args, **kwargs):
|
2019-10-30 20:38:17 +08:00
|
|
|
self.token = self.try_get_jwt_from_server()
|
|
|
|
# noinspection PyArgumentList
|
|
|
|
return request_function(self, *args, **kwargs)
|
2019-11-27 19:39:47 +08:00
|
|
|
|
2019-10-30 20:38:17 +08:00
|
|
|
return request_function_wrapper
|
|
|
|
|
|
|
|
@_Decorators.refresh_jwt_token
|
2019-09-13 21:12:58 +08:00
|
|
|
def get(self, url, data=None):
|
2021-04-22 19:09:19 +08:00
|
|
|
return requests.get( # noqa: DUO123
|
|
|
|
self.addr + url,
|
2021-04-06 21:19:27 +08:00
|
|
|
headers=self.get_jwt_header(),
|
|
|
|
params=data,
|
|
|
|
verify=False,
|
|
|
|
)
|
2019-09-13 21:12:58 +08:00
|
|
|
|
2019-10-30 20:38:17 +08:00
|
|
|
@_Decorators.refresh_jwt_token
|
2019-09-13 21:12:58 +08:00
|
|
|
def post(self, url, data):
|
2021-04-22 19:09:19 +08:00
|
|
|
return requests.post( # noqa: DUO123
|
|
|
|
self.addr + url, data=data, headers=self.get_jwt_header(), verify=False
|
2021-04-06 21:19:27 +08:00
|
|
|
)
|
2019-09-13 21:12:58 +08:00
|
|
|
|
2022-08-01 18:55:25 +08:00
|
|
|
@_Decorators.refresh_jwt_token
|
|
|
|
def put(self, url, data):
|
|
|
|
return requests.put( # noqa: DUO123
|
|
|
|
self.addr + url, data=data, headers=self.get_jwt_header(), verify=False
|
|
|
|
)
|
|
|
|
|
2022-08-05 21:20:52 +08:00
|
|
|
@_Decorators.refresh_jwt_token
|
|
|
|
def put_json(self, url, json: Dict):
|
|
|
|
return requests.put( # noqa: DUO123
|
|
|
|
self.addr + url, json=json, headers=self.get_jwt_header(), verify=False
|
|
|
|
)
|
|
|
|
|
2019-10-30 20:38:17 +08:00
|
|
|
@_Decorators.refresh_jwt_token
|
2022-07-23 00:34:28 +08:00
|
|
|
def post_json(self, url, json: Dict):
|
2021-04-22 19:09:19 +08:00
|
|
|
return requests.post( # noqa: DUO123
|
2022-07-23 00:34:28 +08:00
|
|
|
self.addr + url, json=json, headers=self.get_jwt_header(), verify=False
|
2021-04-06 21:19:27 +08:00
|
|
|
)
|
2020-04-16 21:39:10 +08:00
|
|
|
|
|
|
|
@_Decorators.refresh_jwt_token
|
2020-04-24 00:46:58 +08:00
|
|
|
def patch(self, url, data: Dict):
|
2021-04-22 19:09:19 +08:00
|
|
|
return requests.patch( # noqa: DUO123
|
|
|
|
self.addr + url, data=data, headers=self.get_jwt_header(), verify=False
|
2021-04-06 21:19:27 +08:00
|
|
|
)
|
2019-09-13 21:12:58 +08:00
|
|
|
|
2020-02-23 20:02:18 +08:00
|
|
|
@_Decorators.refresh_jwt_token
|
|
|
|
def delete(self, url):
|
2021-04-22 19:09:19 +08:00
|
|
|
return requests.delete( # noqa: DUO123
|
2021-04-06 21:19:27 +08:00
|
|
|
self.addr + url, headers=self.get_jwt_header(), verify=False
|
|
|
|
)
|
2020-02-23 20:02:18 +08:00
|
|
|
|
2019-09-13 21:12:58 +08:00
|
|
|
def get_jwt_header(self):
|
2020-07-30 17:41:25 +08:00
|
|
|
return {"Authorization": "Bearer " + self.token}
|