Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set API key header to X-IRIS-AUTH #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions dfir_iris_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ClientSession(object):
Returns:

"""
def __init__(self, apikey=None, host=None, agent="iris-client", ssl_verify=True, proxy=None, timeout=120):
def __init__(self, apikey=None, host=None, agent="iris-client", ssl_verify=True, proxy=None, timeout=120, api_key_header=None, additional_headers=None):
"""
Initialize the ClientSession. APIKey validity is verified as well as API compatibility between the client
and the server.
Expand All @@ -70,6 +70,8 @@ def __init__(self, apikey=None, host=None, agent="iris-client", ssl_verify=True,
ssl_verify: Set or unset SSL verification
proxy: Proxy parameters - For future use only
timeout: Default timeout for requests
api_key_header: Header to use for the API key, can either be 'Authorization' or 'X-IRIS-AUTH'. Defaults to 'Authorization' when not set
additional_headers: Additional headers to pass in the request
"""
self._apikey = apikey
self._host = host
Expand All @@ -84,6 +86,12 @@ def __init__(self, apikey=None, host=None, agent="iris-client", ssl_verify=True,
if not self._ssl_verify:
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


if api_key_header and api_key_header not in ['Authorization', 'X-IRIS-AUTH']:
raise Exception("api_key_header can only be 'Authorization' or 'X-IRIS-AUTH'")
self._api_key_header = api_key_header or 'Authorization'
self._addition_headers = additional_headers

self._check_apikey_validity()

self._check_api_compatibility()
Expand Down Expand Up @@ -216,9 +224,12 @@ def _pi_request(self, uri: str, type: str = None, data: dict = None,

headers = {
'Content-Type': "application/json",
'Authorization': "Bearer " + self._apikey,
self._api_key_header: "Bearer " + self._apikey,
'User-Agent': self._agent
}
if self._addition_headers:
headers = {**headers, **self._addition_headers}

if type == "POST":
log.debug(f'POST : {self._pi_uri(uri)}')

Expand Down Expand Up @@ -271,9 +282,11 @@ def pi_post_files(self, uri: str, files: dict = None, data: dict = None, cid: in
ApiResponse object
"""
headers = {
'Authorization': "Bearer " + self._apikey,
self._api_key_header: "Bearer " + self._apikey,
'User-Agent': self._agent
}
if self._addition_headers:
headers = {**headers, **self._addition_headers}

if cid is None:
raise ValueError('cid is mandatory when uploading files')
Expand Down