Skip to content

Commit

Permalink
Merge branch 'master' into sign_only
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-K committed Jun 5, 2015
2 parents 072f1d5 + c36b36d commit 068b670
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
6 changes: 5 additions & 1 deletion flask_oauthlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,12 @@ def request(self, url, data=None, headers=None, format='urlencoded',
# change the uri, headers, or body.
uri, headers, body = self.pre_request(uri, headers, body)

if body:
data = to_bytes(body, self.encoding)
else:
data = None
resp, content = self.http_request(
uri, headers, data=to_bytes(body, self.encoding), method=method
uri, headers, data=data, method=method
)
return OAuthResponse(resp, content, self.content_type)

Expand Down
44 changes: 14 additions & 30 deletions flask_oauthlib/contrib/client/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

from flask import current_app, redirect, request
from requests_oauthlib import OAuth1Session, OAuth2Session
from requests_oauthlib.oauth1_session import TokenMissing
from oauthlib.oauth2.rfc6749.errors import MissingCodeError
from werkzeug.utils import import_string

from .descriptor import OAuthProperty, WebSessionData
from .structure import OAuth1Response, OAuth2Response
from .exceptions import AccessTokenNotFound
from .signals import request_token_fetched


__all__ = ['OAuth1Application', 'OAuth2Application']
Expand Down Expand Up @@ -155,8 +157,6 @@ class OAuth1Application(BaseApplication):

session_class = OAuth1Session

_session_request_token = WebSessionData('req_token')

def make_client(self, token):
"""Creates a client with specific access token pair.
Expand All @@ -166,24 +166,24 @@ def make_client(self, token):
object.
"""
if isinstance(token, dict):
access_token = token['token']
access_token_secret = token['token_secret']
access_token = token['oauth_token']
access_token_secret = token['oauth_token_secret']
else:
access_token, access_token_secret = token
return self.make_oauth_session(
resource_owner_key=access_token,
resource_owner_secret=access_token_secret)

def authorize(self, callback_uri, code=302):
# TODO add support for oauth_callback=oob (out-of-band) here
# http://tools.ietf.org/html/rfc5849#section-2.1
oauth = self.make_oauth_session(callback_uri=callback_uri)

# fetches request token
response = oauth.fetch_request_token(self.request_token_url)
request_token = response['oauth_token']
request_token_secret = response['oauth_token_secret']

# stores request token and callback uri
self._session_request_token = (request_token, request_token_secret)
token = oauth.fetch_request_token(self.request_token_url)
request_token_fetched.send(self, response=OAuth1Response(token))
# TODO check oauth_callback_confirmed here
# http://tools.ietf.org/html/rfc5849#section-2.1

# redirects to third-part URL
authorization_url = oauth.authorization_url(self.authorization_url)
Expand All @@ -194,26 +194,13 @@ def authorized_response(self):

# obtains verifier
try:
response = oauth.parse_authorization_response(request.url)
except ValueError as e:
if 'denied' not in repr(e).split("'"):
raise
oauth.parse_authorization_response(request.url)
except TokenMissing:
return # authorization denied
verifier = response['oauth_verifier']

# restores request token from session
if not self._session_request_token:
return
request_token, request_token_secret = self._session_request_token
del self._session_request_token

# obtains access token
oauth = self.make_oauth_session(
resource_owner_key=request_token,
resource_owner_secret=request_token_secret,
verifier=verifier)
oauth_tokens = oauth.fetch_access_token(self.access_token_url)
return OAuth1Response(oauth_tokens)
token = oauth.fetch_access_token(self.access_token_url)
return OAuth1Response(token)

def make_oauth_session(self, **kwargs):
oauth = self.session_class(
Expand Down Expand Up @@ -286,10 +273,7 @@ def authorized_response(self):
return OAuth2Response(token)

def make_oauth_session(self, **kwargs):
# joins scope into unicode
kwargs.setdefault('scope', self.scope)
if kwargs['scope']:
kwargs['scope'] = u','.join(kwargs['scope'])

# configures automatic token refresh if possible
if self.refresh_token_url:
Expand Down
6 changes: 6 additions & 0 deletions flask_oauthlib/contrib/client/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from flask.signals import Namespace

__all__ = ['request_token_fetched']

_signals = Namespace()
request_token_fetched = _signals.signal('request-token-fetched')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def fread(filename):
install_requires=[
'Flask',
'oauthlib>=0.6.2',
'requests-oauthlib>=0.4.1',
'requests-oauthlib>=0.5.0',
],
tests_require=['nose', 'Flask-SQLAlchemy', 'mock'],
test_suite='nose.collector',
Expand Down

0 comments on commit 068b670

Please sign in to comment.