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

1.0.2 support. Python 3 endode / decode problems fixed #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 12 additions & 15 deletions httpie_api_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
"""
from httpie.plugins import AuthPlugin
import hmac, base64, hashlib, datetime
import urllib.parse

try:
import urlparse
except ImportError:
import urllib.parse

__version__ = '0.3.0'
__version__ = '0.3.1'
__author__ = 'Kyle Hargraves'
__licence__ = 'MIT'

class ApiAuth:
def __init__(self, access_id, secret_key):
self.access_id = access_id
self.secret_key = secret_key.encode('ascii')
def __init__(self, username=None, password=None):
self.username = username
self.password = password.encode('ascii')

def __call__(self, r):
method = r.method.upper()
Expand All @@ -36,16 +32,17 @@ def __call__(self, r):
httpdate = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
r.headers['Date'] = httpdate

url = urlparse.urlparse(r.url)
url = urllib.parse.urlparse(r.url)
path = url.path
if url.query:
path = path + '?' + url.query

string_to_sign = '%s,%s,%s,%s,%s' % (method, content_type, content_md5, path, httpdate)
digest = hmac.new(self.secret_key, string_to_sign, hashlib.sha1).digest()
string_to_sign = '%s,%s,%s,%s,%s' % (method, content_type.decode(), content_md5, path, httpdate)
Copy link

@costi costi Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to take care the empty content-type case.

if not content_type:
            content_type = ''

Error:
http: error: AttributeError: 'str' object has no attribute 'decode'

Change line 23 to content_type = b''


digest = hmac.new(self.password, string_to_sign.encode(), hashlib.sha1).digest()
signature = base64.encodestring(digest).rstrip()

r.headers['Authorization'] = 'APIAuth %s:%s' % (self.access_id, signature)
r.headers['Authorization'] = 'APIAuth %s:%s' % (self.username, signature.decode())
return r

class ApiAuthPlugin(AuthPlugin):
Expand All @@ -54,5 +51,5 @@ class ApiAuthPlugin(AuthPlugin):
auth_type = 'api-auth'
description = 'Sign requests using the ApiAuth authentication method'

def get_auth(self, access_id, secret_key):
return ApiAuth(access_id, secret_key)
def get_auth(self, username, password):
return ApiAuth(username, password)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
name='httpie-api-auth',
description='ApiAuth plugin for HTTPie.',
long_description=open('README.rst').read().strip(),
version='0.3.0',
version='0.3.1',
author='Kyle Hargraves',
author_email='[email protected]',
license='MIT',
Expand Down