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

Remove fake_https option, use hooks to modify base string #5

Open
wants to merge 2 commits 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
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ Demo
使用 https:
""""""""""""

在最新的版本中,你可以使用 https 向饭否 API 发送请求,指定 fake_https 为真即可
在最新的版本中,你可以使用 https 向饭否 API 发送请求,指定 https 为 True 还需要使用 hooks 来修改一下 base string

.. code-block:: python

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> client = fanfou.XAuth(consumer, 'username', 'password', fake_https=True)
>>> client = fanfou.XAuth(consumer, 'username', 'password', https=True, hooks={'base_string': lambda s: s.replace('https', 'http')})

fake_https 选项在上面的 4 种认证方式中均可用。使用 fake_https 这个名字的原因是,目前饭否 API 服务器 https 还有点小问题,需要手动修改 base_string。在将来饭否修复了这一问题后,我们将会使用 https 而不是 fake_https
hooks 选项在上面的 4 种认证方式中均可用。使用 hooks 这个名字的原因是,目前饭否 HTTPS API 在校验 base string 时使用的仍是 http 的 URL,因此需要手动修改 base_string


步骤 2: 访问 API
Expand Down Expand Up @@ -256,9 +256,9 @@ auth classes

两个 Auth 类的 __init__ 方法如下:

class **OAuth** (oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, fake_https=False)
class **OAuth** (oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, hooks={})

class **XAuth** (oauth_consumer, username, password, https=False, fake_https=False)
class **XAuth** (oauth_consumer, username, password, https=False, hooks={})

致谢
------
Expand Down
10 changes: 5 additions & 5 deletions README_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ Way 4:
Using https:
""""""""""""

In the lastest edition, you can using https like that:
In the lastest edition, you can using https like this below:

.. code-block:: python

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> client = fanfou.XAuth(consumer, 'username', 'password', fake_https=True)
>>> client = fanfou.XAuth(consumer, 'username', 'password', https=True, hooks={'base_string': lambda s: s.replace('https', 'http')})

The fake_https is available in all authorize ways.
The hooks parameter is available in all authorize ways.


Step 2: Access API
Expand Down Expand Up @@ -242,9 +242,9 @@ auth classes

The __init__ method for auth classes is as follows:

class **OAuth** (oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, fake_https=False)
class **OAuth** (oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, hooks={})

class **XAuth** (oauth_consumer, username, password, https=False, fake_https=False)
class **XAuth** (oauth_consumer, username, password, https=False, hooks={})

Thanks
------
Expand Down
23 changes: 12 additions & 11 deletions fanfou/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hmac
import json
import time
import types
import random
import hashlib
import binascii
Expand Down Expand Up @@ -33,36 +34,36 @@ def oauth_query(args, via='quote', safe='~'):
return '&'.join('%s=%s' % (k, oauth_escape(v, via, safe)) for k, v in sorted(args.items()))


def oauth_normalized_url(url, fake_https):
def oauth_normalized_url(url):
scheme, netloc, path = parse.urlparse(url)[:3]
if fake_https:
scheme = 'http'
return '{0}://{1}{2}'.format(scheme, netloc, path)


class Auth(object):
def __init__(self, oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, fake_https=False):
def __init__(self, oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, hooks={}):
self.oauth_consumer = oauth_consumer
self.oauth_token = oauth_token or {}
self.callback = callback or 'http://localhost:8080/callback'
self.form_urlencoded = 'application/x-www-form-urlencoded'
self.fake_https = fake_https and not https
self.scheme = 'https' if (fake_https or https) else 'http'
self.scheme = 'https' if https else 'http'
self.auth_host = '{0}://{1}'.format(self.scheme, auth_host or 'm.fanfou.com')
self.base_api_url = '{0}://api.fanfou.com%s.json'.format(self.scheme)
self.access_token_url = '{0}://fanfou.com/oauth/access_token'.format(self.scheme)
self.request_token_url = '{0}://fanfou.com/oauth/request_token'.format(self.scheme)
self.authorize_url = parse.urljoin(self.auth_host, '/oauth/authorize?oauth_token=%s&oauth_callback=%s')
self.hooks = hooks

def HMAC_SHA1(self, keys_string, base_string):
hashed = hmac.new(keys_string.encode(), base_string.encode(), hashlib.sha1)
return binascii.b2a_base64(hashed.digest())[:-1]

def oauth_signature(self, url, method, base_args):
normalized_url = oauth_normalized_url(url, self.fake_https)
normalized_url = oauth_normalized_url(url)
query_items = oauth_query(base_args)
base_elems = (method.upper(), normalized_url, query_items)
base_string = '&'.join(oauth_escape(s) for s in base_elems)
if type(self.hooks.get('base_string')) is types.FunctionType:
base_string = self.hooks['base_string'](base_string)
keys_elems = (self.oauth_consumer['secret'], self.oauth_token.get('secret', ''))
keys_string = '&'.join(oauth_escape(s) for s in keys_elems)
return self.HMAC_SHA1(keys_string, base_string)
Expand Down Expand Up @@ -122,8 +123,8 @@ def oauth_request(self, url, method='GET', args={}, headers={}):


class OAuth(Auth):
def __init__(self, oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, fake_https=False):
Auth.__init__(self, oauth_consumer, oauth_token, callback, auth_host, https, fake_https)
def __init__(self, oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, hooks={}):
Auth.__init__(self, oauth_consumer, oauth_token, callback, auth_host, https, hooks)

def request(self, url, method='GET', args={}, headers={}):
return self.oauth_request(url, method, args, headers)
Expand All @@ -145,8 +146,8 @@ def access_token(self, oauth_token=None, oauth_verifier=None):


class XAuth(Auth):
def __init__(self, oauth_consumer, username, password, https=False, fake_https=False):
Auth.__init__(self, oauth_consumer, https=https, fake_https=fake_https)
def __init__(self, oauth_consumer, username, password, https=False, hooks={}):
Auth.__init__(self, oauth_consumer, https=https, hooks=hooks)
self.oauth_token = self.xauth(username, password)

def request(self, url, method='GET', args={}, headers={}):
Expand Down