Permalink
Browse files

Merge branch 'release/0.4.8'

  • Loading branch information...
2 parents 86063cf + b5842c1 commit 0eb3ea989e9a33978d267df124727bb3d93e57b0 @klen committed Sep 10, 2012
Showing with 56 additions and 47 deletions.
  1. +1 −0 Changelog
  2. +2 −2 base/auth/__init__.py
  3. +47 −38 base/auth/oauth.py
  4. +5 −6 base/config/production.py
  5. +1 −1 requirements.txt
View
1 Changelog
@@ -2,6 +2,7 @@
* Flask-Principal>=0.3.3
* Flask-Mail>=0.7.1
+ * Flask-Rauth
2012-09-03 klen
View
4 base/auth/__init__.py
@@ -7,7 +7,7 @@ def register_app(app):
from .views import users
app.register_blueprint(users)
- from .oauth import config_oauth
- config_oauth(app)
+ from .oauth import config_rauth
+ config_rauth(app)
register_app.priority = 1.0
View
85 base/auth/oauth.py
@@ -1,96 +1,105 @@
+import abc
+
from flask import url_for, request, flash, redirect
from flask_login import current_user
from flaskext.babel import lazy_gettext as _
-from flaskext.oauth import OAuth
+from flask_rauth import RauthOAuth1
from ..ext import db
from .models import User
from .views import users
-PROVIDERS = 'twitter',
-CLIENTS = dict()
-
-
-oauth = OAuth()
+# Providers and default settings
+PROVIDERS = dict(
+ twitter=dict(
+ base_url='http://api.twitter.com/1/',
+ request_token_url='http://api.twitter.com/oauth/request_token',
+ access_token_url='http://api.twitter.com/oauth/access_token',
+ authorize_url='http://api.twitter.com/oauth/authorize',
+ )
+)
-def config_oauth(app):
- " Configure oauth support. "
+def config_rauth(app):
+ " Config rauth instances. "
- for name in PROVIDERS:
+ for name, options in PROVIDERS.iteritems():
config = app.config.get('OAUTH_%s' % name.upper())
if not config:
continue
- if not name in oauth.remote_apps:
- remote_app = oauth.remote_app(name, **config)
+ options.update(config)
+ app.logger.info('Init OAuth %s' % name)
+
+ cls = PROVIDERS[name].pop('cls')
+ cls(app, name=name, **options)
+
- else:
- remote_app = oauth.remote_apps[name]
+class RauthBase(object):
- client_class = CLIENTS.get(name)
- client_class(app, remote_app)
+ rauth = RauthOAuth1
+ __meta__ = abc.ABCMeta
-class OAuthBase():
- name = 'base'
+ def __init__(self, app, **options):
- def __init__(self, app, remote_app):
- remote_app.tokengetter_func = self.get_token
+ client = self.rauth(**options)
+ login_name = 'login_%s' % client.name
+ authorize_name = 'authorize_%s' % client.name
+
+ @app.route('/%s' % login_name, endpoint=login_name)
def login():
- return remote_app.authorize(
+ return client.authorize(
callback=(
- url_for('authorize_%s' % self.name,
- next=request.args.get('next') or request.referrer or None)))
-
- login_name = 'login_%s' % self.name
- app.add_url_rule('/%s' % login_name, login_name, login)
+ url_for(authorize_name, _external=True,
+ next=request.args.get('next') or request.referrer)
+ ))
- authorize_name = 'authorize_%s' % self.name
app.add_url_rule('/%s' % authorize_name,
authorize_name,
- remote_app.authorized_handler(self.authorize))
+ client.authorized_handler(self.authorize))
+
+ client.tokengetter_f = self.get_token
@staticmethod
def get_token():
if current_user.is_authenticated() and current_user.oauth_token:
return current_user.oauth_token, current_user.oauth_secret
- def authorize(self, resp):
+ @abc.abstractmethod
+ def authorize(self, resp, oauth_token):
pass
-class OAuthTwitter(OAuthBase):
- name = 'twitter'
+class RauthTwitter(RauthBase):
- def authorize(self, resp):
+ def authorize(self, resp, oauth_token):
next_url = request.args.get('next') or url_for('urls.index')
- if resp is None:
+ if resp is None or resp == 'access_denied':
flash(_(u'You denied the request to sign in.'))
return redirect(next_url)
user = current_user
if not user.is_authenticated():
- user = User.query.filter(User.username == resp['screen_name']).first()
+ user = User.query.filter(
+ User.username == resp.content['screen_name']).first()
if user is None:
- user = User(username=resp['screen_name'])
+ user = User(username=resp.content['screen_name'])
user.generate_password()
db.session.add(user)
- user.oauth_token = resp['oauth_token']
- user.oauth_secret = resp['oauth_token_secret']
+ user.oauth_token, user.oauth_token_secret = oauth_token
db.session.commit()
users.login(user)
flash(_('Welcome %(user)s', user=user.username))
return redirect(next_url)
-
-CLIENTS['twitter'] = OAuthTwitter
+PROVIDERS['twitter']['cls'] = RauthTwitter
# pymode:lint_ignore=F0401
View
11 base/config/production.py
@@ -5,16 +5,15 @@
SECRET_KEY = 'SecretKeyForSessionSigning'
-
ADMINS = frozenset([MAIL_USERNAME])
+
+# flask.ext.collect
+# -----------------
COLLECT_STATIC_ROOT = op.join(op.dirname(ROOTDIR), 'static')
+# auth.oauth
+# ----------
OAUTH_TWITTER = dict(
- base_url='http://api.twitter.com/1/',
- request_token_url='http://api.twitter.com/oauth/request_token',
- access_token_url='http://api.twitter.com/oauth/access_token',
- authorize_url='http://api.twitter.com/oauth/authorize',
-
# flask-base-template app
consumer_key='ydcXz2pWyePfc3MX3nxJw',
consumer_secret='Pt1t2PjzKu8vsX5ixbFKu5gNEAekYrbpJrlsQMIwquc'
View
2 requirements.txt
@@ -9,8 +9,8 @@ Flask-Collect>=0.1.3
Flask-DebugToolbar>=0.7.1
Flask-Login>=0.1.3
Flask-Mail>=0.7.1
-Flask-OAuth>=0.11
Flask-Principal>=0.3.3
+Flask-Rauth>=0.2.2
Flask-Script>=0.4.0
Flask-SQLAlchemy>=0.16
Flask-Testing>=0.4

0 comments on commit 0eb3ea9

Please sign in to comment.