root / astakos / im / target / twitter.py @ 890b0eaf
History | View | Annotate | Download (5.2 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
# This is based on the docs at: https://github.com/simplegeo/python-oauth2
|
35 |
|
36 |
import oauth2 as oauth |
37 |
import urlparse |
38 |
|
39 |
from django.conf import settings |
40 |
from django.http import HttpResponse |
41 |
from django.utils import simplejson as json |
42 |
from django.contrib.auth import authenticate |
43 |
|
44 |
from astakos.im.target.util import prepare_response |
45 |
from astakos.im.util import get_or_create_user |
46 |
|
47 |
# It's probably a good idea to put your consumer's OAuth token and
|
48 |
# OAuth secret into your project's settings.
|
49 |
consumer = oauth.Consumer(settings.TWITTER_KEY, settings.TWITTER_SECRET) |
50 |
client = oauth.Client(consumer) |
51 |
|
52 |
request_token_url = 'http://twitter.com/oauth/request_token'
|
53 |
access_token_url = 'http://twitter.com/oauth/access_token'
|
54 |
|
55 |
# This is the slightly different URL used to authenticate/authorize.
|
56 |
authenticate_url = 'http://twitter.com/oauth/authenticate'
|
57 |
|
58 |
def login(request): |
59 |
# Step 1. Get a request token from Twitter.
|
60 |
resp, content = client.request(request_token_url, "GET")
|
61 |
if resp['status'] != '200': |
62 |
raise Exception("Invalid response from Twitter.") |
63 |
request_token = dict(urlparse.parse_qsl(content))
|
64 |
if request.GET.get('next'): |
65 |
request_token['next'] = request.GET['next'] |
66 |
|
67 |
# Step 2. Store the request token in a session for later use.
|
68 |
response = HttpResponse() |
69 |
response.set_cookie('Twitter-Request-Token', value=json.dumps(request_token), max_age=300) |
70 |
|
71 |
# Step 3. Redirect the user to the authentication URL.
|
72 |
url = "%s?oauth_token=%s" % (authenticate_url, request_token['oauth_token']) |
73 |
response['Location'] = url
|
74 |
response.status_code = 302
|
75 |
|
76 |
return response
|
77 |
|
78 |
def authenticated(request): |
79 |
# Step 1. Use the request token in the session to build a new client.
|
80 |
data = request.COOKIES.get('Twitter-Request-Token', None) |
81 |
if not data: |
82 |
raise Exception("Request token cookie not found.") |
83 |
request_token = json.loads(data) |
84 |
if not hasattr(request_token, '__getitem__'): |
85 |
raise BadRequest('Invalid data formating') |
86 |
try:
|
87 |
token = oauth.Token(request_token['oauth_token'],
|
88 |
request_token['oauth_token_secret'])
|
89 |
except:
|
90 |
raise BadRequest('Invalid request token cookie formatting') |
91 |
client = oauth.Client(consumer, token) |
92 |
|
93 |
# Step 2. Request the authorized access token from Twitter.
|
94 |
resp, content = client.request(access_token_url, "GET")
|
95 |
if resp['status'] != '200': |
96 |
raise Exception("Invalid response from Twitter.") |
97 |
|
98 |
"""
|
99 |
This is what you'll get back from Twitter. Note that it includes the
|
100 |
user's user_id and screen_name.
|
101 |
{
|
102 |
'oauth_token_secret': 'IcJXPiJh8be3BjDWW50uCY31chyhsMHEhqJVsphC3M',
|
103 |
'user_id': '120889797',
|
104 |
'oauth_token': '120889797-H5zNnM3qE0iFoTTpNEHIz3noL9FKzXiOxwtnyVOD',
|
105 |
'screen_name': 'heyismysiteup'
|
106 |
}
|
107 |
"""
|
108 |
access_token = dict(urlparse.parse_qsl(content))
|
109 |
|
110 |
# Step 3. Lookup the user or create them if they don't exist.
|
111 |
|
112 |
# When creating the user I just use their screen_name@twitter.com
|
113 |
# for their email and the oauth_token_secret for their password.
|
114 |
# These two things will likely never be used. Alternatively, you
|
115 |
# can prompt them for their email here. Either way, the password
|
116 |
# should never be used.
|
117 |
username = '%s@twitter.com' % access_token['screen_name'] |
118 |
realname = access_token['screen_name']
|
119 |
|
120 |
user = get_or_create_user(username, realname=realname, affiliation='Twitter', level=0, email=username) |
121 |
# in order to login the user we must call authenticate first
|
122 |
user = authenticate(username=user.username, auth_token=user.auth_token) |
123 |
return prepare_response(request,
|
124 |
user, |
125 |
request_token.get('next'))
|