-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
50 lines (42 loc) · 1.48 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from functools import wraps
from flaskext.oauth import OAuth
from flask import session
from flask import redirect
from flask import url_for
from flask import request
# OAuth config:
oauth = OAuth()
twitter = oauth.remote_app('twitter',
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/authenticate',
consumer_key='aMCIQI4nO4V1FDuGps8nRw',
consumer_secret='EI7t8s8nefbqlpx0xcKgbitiHwn3ydpTGCWIWUsgug'
)
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get('twitter_user') is None:
return redirect(url_for('login', next=request.url))
return f(*args, **kwargs)
return decorated_function
@twitter.tokengetter
def get_twitter_token():
return session.get('twitter_token')
def do_login():
return twitter.authorize(callback=url_for('oauth_authorized',
next=request.args.get('next') or request.referrer or None))
def do_oauth_callback(resp):
""" this is the call back after login
"""
next_url = request.args.get('next') or url_for('index')
if resp is None:
flash(u'You denied the request to sign in.')
return redirect(next_url)
session['twitter_token'] = (
resp['oauth_token'],
resp['oauth_token_secret']
)
session['twitter_user'] = resp['screen_name']
return redirect(next_url)