Skip to content

Commit

Permalink
Merge pull request #13 from singingwolfboy/requests-oauthlib-authoriz…
Browse files Browse the repository at this point in the history
…ed-property

Make the `authorized` property load the token from the backend
  • Loading branch information
singingwolfboy committed Apr 28, 2015
2 parents 177b7b9 + c046d81 commit c227368
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

unreleased
----------
* Make the ``authorized`` property on both ``OAuth1Session`` and ``OAuth2Session``
dynamically load the token from the backend

0.5.0 (2015-04-20)
------------------
* Redesigned token storage backend system: it now uses objects
Expand Down
2 changes: 1 addition & 1 deletion flask_dance/consumer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .oauth1 import OAuth1ConsumerBlueprint
from .oauth2 import OAuth2ConsumerBlueprint
from .base import oauth_authorized, oauth_error

from .requests import OAuth1Session, OAuth2Session
38 changes: 28 additions & 10 deletions flask_dance/consumer/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,28 @@ def __init__(self, blueprint=None, base_url=None, *args, **kwargs):
def token(self):
return self.blueprint.token

def prepare_request(self, request):
if self.base_url:
request.url = self.base_url.relative(request.url)
return super(OAuth1Session, self).prepare_request(request)

def request(self, method, url, data=None, headers=None, **kwargs):
def load_token(self):
t = self.token
if t and "oauth_token" in t and "oauth_token_secret" in t:
# This really, really violates the Law of Demeter, but
# I don't see a better way to set these parameters. :(
self.auth.client.resource_owner_key = to_unicode(t["oauth_token"])
self.auth.client.resource_owner_secret = to_unicode(t["oauth_token_secret"])
return True
return False

@property
def authorized(self):
self.load_token()
return super(OAuth1Session, self).authorized

def prepare_request(self, request):
if self.base_url:
request.url = self.base_url.relative(request.url)
return super(OAuth1Session, self).prepare_request(request)

def request(self, method, url, data=None, headers=None, **kwargs):
self.load_token()
return super(OAuth1Session, self).request(
method=method, url=url, data=data, headers=headers, **kwargs
)
Expand All @@ -62,14 +71,23 @@ def __init__(self, blueprint=None, base_url=None, *args, **kwargs):
def token(self):
return self.blueprint.token

def request(self, method, url, data=None, headers=None, **kwargs):
if self.base_url:
url = self.base_url.relative(url)

def load_token(self):
self._client.token = self.token
if self.token:
self._client._populate_attributes(self.token)
return True
return False

@property
def authorized(self):
self.load_token()
return super(OAuth2Session, self).authorized

def request(self, method, url, data=None, headers=None, **kwargs):
if self.base_url:
url = self.base_url.relative(url)

self.load_token()
return super(OAuth2Session, self).request(
method=method, url=url, data=data, headers=headers, **kwargs
)
50 changes: 50 additions & 0 deletions tests/consumer/test_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import pytest
import mock
from distutils.version import StrictVersion
import requests_oauthlib
from flask_dance.consumer.requests import OAuth1Session, OAuth2Session

requires_requests_oauthlib_043 = pytest.mark.skipif(
StrictVersion(requests_oauthlib.__version__) < StrictVersion('0.4.3'),
reason="requires requests_oauthlib at version 0.4.3 or higher",
)


FAKE_OAUTH1_TOKEN = {
"oauth_token": "abcdefg",
"oauth_token_secret": "hijklmnop",
}
FAKE_OAUTH2_TOKEN = {
"access_token": "deadbeef",
"scope": ["custom"],
"token_type": "bearer",
}


@requires_requests_oauthlib_043
def test_oauth1session_authorized():
bp = mock.Mock(token=FAKE_OAUTH1_TOKEN)
sess = OAuth1Session(client_key="ckey", client_secret="csec", blueprint=bp)
assert sess.authorized == True


@requires_requests_oauthlib_043
def test_oauth1session_not_authorized():
bp = mock.Mock(token=None)
sess = OAuth1Session(client_key="ckey", client_secret="csec", blueprint=bp)
assert sess.authorized == False


def test_oauth2session_authorized():
bp = mock.Mock(token=FAKE_OAUTH2_TOKEN)
sess = OAuth2Session(client_id="cid", blueprint=bp)
assert sess.authorized == True


def test_oauth2session_not_authorized():
bp = mock.Mock(token=None)
sess = OAuth2Session(client_id="cid", blueprint=bp)
assert sess.authorized == False

0 comments on commit c227368

Please sign in to comment.