Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Remove Signonotron2's dependence on flask
Browse files Browse the repository at this point in the history
The Signonotron2 class doesn't need to depend on flask and it doing so
has the depdency going in the wrong direction.
  • Loading branch information
robyoung committed Dec 4, 2013
1 parent ae89be5 commit e8008d7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
16 changes: 9 additions & 7 deletions backdrop/write/admin_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ def setup(app, db, bucket_repository, user_repository):
ADMIN_UI_HOST = app.config["BACKDROP_ADMIN_UI_HOST"]
MAX_UPLOAD_SIZE = 1000000

app.oauth_service = Signonotron2(
client_id=app.config['OAUTH_CLIENT_ID'],
client_secret=app.config['OAUTH_CLIENT_SECRET'],
base_url=app.config['OAUTH_BASE_URL'],
backdrop_admin_ui_host=ADMIN_UI_HOST
)
@app.before_first_request
def setup_oauth_redirect_uri():
app.oauth_service = Signonotron2(
client_id=app.config['OAUTH_CLIENT_ID'],
client_secret=app.config['OAUTH_CLIENT_SECRET'],
base_url=app.config['OAUTH_BASE_URL'],
redirect_url=url_for(ADMIN_UI_HOST, "oauth_authorized")
)

@app.after_request
def prevent_clickjacking(response):
Expand Down Expand Up @@ -63,7 +65,7 @@ def oauth_sign_in():
This returns a redirect to the OAuth provider, so we shouldn't
allow this response to be cached.
"""
return app.oauth_service.authorize()
return redirect(app.oauth_service.authorize())

@app.route(USER_SCOPE + "/sign_out")
@cache_control.set("private, must-revalidate")
Expand Down
17 changes: 7 additions & 10 deletions backdrop/write/signonotron2.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from logging import getLogger
from flask import redirect, json
from admin_ui_helper import url_for
import json
from rauth import OAuth2Service, service

log = getLogger(__name__)


class Signonotron2(object):
def __init__(self, client_id, client_secret, base_url,
backdrop_admin_ui_host):
redirect_url):
self.signon = OAuth2Service(
client_id=client_id,
client_secret=client_secret,
Expand All @@ -17,25 +16,23 @@ def __init__(self, client_id, client_secret, base_url,
access_token_url="%s/oauth/token" % base_url,
base_url=base_url
)
self.backdrop_admin_ui_host = backdrop_admin_ui_host

def __redirect_uri(self):
return url_for(self.backdrop_admin_ui_host, "oauth_authorized")
self.redirect_url = redirect_url

def __json_access_token(self, something):
# TODO: use python json package
return json.loads(something)

def authorize(self):
params = {
"response_type": "code",
"redirect_uri": self.__redirect_uri()
"redirect_uri": self.redirect_url
}
return redirect(self.signon.get_authorize_url(**params))
return self.signon.get_authorize_url(**params)

def exchange(self, code):
data = dict(
grant_type='authorization_code',
redirect_uri=self.__redirect_uri(),
redirect_uri=self.redirect_url,
code=code
)
response = self.signon.get_raw_access_token('POST', data=data)
Expand Down
1 change: 0 additions & 1 deletion tests/write/test_file_upload_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def test_upload_auto_generate_ids(self):
assert_that(response, has_status(200))
db = MongoClient('localhost', 27017).backdrop_test
results = list(db.bucket_with_timestamp_auto_id.find())
print(results[0])

assert_that(len(results), is_(18))
assert_that(results[0], has_entries({
Expand Down
8 changes: 4 additions & 4 deletions tests/write/test_signonotron2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def setUp(self):
def tearDown(self):
self.ctx.pop()

def test_authorize_returns_a_redirect_to_signon_service(self):
def test_authorize_returns_a_url_to_signon_service(self):
oauth_service = Signonotron2(None, None, None, "")
oauth_service.signon = Mock()
oauth_service.signon.get_authorize_url.return_value = ""
oauth_service.signon.get_authorize_url.return_value = "http://example.com"

response = oauth_service.authorize()
url = oauth_service.authorize()

assert_that(response, has_status(302))
assert_that(url, equal_to("http://example.com"))

def test_exchange_returns_none_when_code_is_none(self):
oauth_service = Signonotron2(None, None, None, "")
Expand Down

0 comments on commit e8008d7

Please sign in to comment.