Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
martynp committed Mar 14, 2023
1 parent 6091d60 commit dd50519
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion httpie_hmac/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .httpie_hmac import HmacPlugin, HmacAuth, HmacGenerate
from .httpie_hmac import HmacPlugin, HmacAuth, HmacGenerate # noqa: F401
32 changes: 27 additions & 5 deletions httpie_hmac/httpie_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,34 @@


class HmacGenerate:
def generate(access_key, secret_key, method, content_type, content_md5, http_date, path, r):
def generate(access_key,
secret_key,
method,
content_type,
content_md5,
http_date,
path,
r):
pass


class Simple(HmacGenerate):
def generate(access_key, secret_key, method, content_type, content_md5, http_date, path, r):
def generate(access_key,
secret_key,
method,
content_type,
content_md5,
http_date,
path,
r):

string_to_sign = '\n'.join(
[method, content_md5, content_type, http_date, path]).encode()
digest = hmac.new(secret_key, string_to_sign,
hashlib.sha256).digest()
signature = base64.b64encode(digest).rstrip().decode('utf-8')

if access_key == None or access_key == '':
if access_key is None or access_key == '':
r.headers['Authorization'] = f"HMAC {signature}"
elif secret_key == '':
raise ValueError('HMAC secret key cannot be empty.')
Expand Down Expand Up @@ -65,7 +79,8 @@ def __init__(self, access_key, secret_key, format):
loader.exec_module(mod)
if issubclass(mod.HmacAuthCustom, HmacGenerate) is False:
raise TypeError(
"Custom generator must inherit httpie_hmac.HmacGenerate")
"Custom generator must inherit "
"httpie_hmac.HmacGenerate")
self.formatter = mod.HmacAuthCustom
else:
self.formatter = generators[format]
Expand Down Expand Up @@ -108,7 +123,14 @@ def __call__(self, r):
path = url.path

# Call the formatter to add the required headers and return r
return self.formatter.generate(self.access_key, self.secret_key_bytes, method, content_type, content_md5, http_date, path, r)
return self.formatter.generate(self.access_key,
self.secret_key_bytes,
method,
content_type,
content_md5,
http_date,
path,
r)


class HmacPlugin(AuthPlugin):
Expand Down

0 comments on commit dd50519

Please sign in to comment.