root / snf-astakos-app / astakos / im / target / twitter.py @ db9083f9
History | View | Annotate | Download (7.9 kB)
1 |
# Copyright 2011-2012 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 |
from django.http import HttpResponseBadRequest |
35 |
from django.utils.translation import ugettext as _ |
36 |
from django.contrib import messages |
37 |
from django.template import RequestContext |
38 |
from django.views.decorators.http import require_http_methods |
39 |
from django.http import HttpResponseRedirect |
40 |
from django.core.urlresolvers import reverse |
41 |
from django.core.exceptions import ImproperlyConfigured |
42 |
from django.shortcuts import get_object_or_404 |
43 |
|
44 |
from urlparse import urlunsplit, urlsplit |
45 |
|
46 |
from astakos.im.util import prepare_response, get_context |
47 |
from astakos.im.views import requires_anonymous, render_response, \ |
48 |
requires_auth_provider
|
49 |
from astakos.im.settings import ENABLE_LOCAL_ACCOUNT_MIGRATION, BASEURL |
50 |
from astakos.im.models import AstakosUser, PendingThirdPartyUser |
51 |
from astakos.im.forms import LoginForm |
52 |
from astakos.im.activation_backends import get_backend, SimpleBackend |
53 |
from astakos.im import settings |
54 |
|
55 |
import astakos.im.messages as astakos_messages |
56 |
|
57 |
import logging |
58 |
|
59 |
logger = logging.getLogger(__name__) |
60 |
|
61 |
import oauth2 as oauth |
62 |
import cgi |
63 |
|
64 |
consumer = oauth.Consumer(settings.TWITTER_TOKEN, settings.TWITTER_SECRET) |
65 |
client = oauth.Client(consumer) |
66 |
|
67 |
request_token_url = 'http://twitter.com/oauth/request_token'
|
68 |
access_token_url = 'http://twitter.com/oauth/access_token'
|
69 |
authenticate_url = 'http://twitter.com/oauth/authenticate'
|
70 |
|
71 |
|
72 |
@requires_auth_provider('twitter', login=True) |
73 |
@require_http_methods(["GET", "POST"]) |
74 |
def login(request): |
75 |
resp, content = client.request(request_token_url, "GET")
|
76 |
if resp['status'] != '200': |
77 |
messages.error(request, 'Invalid Twitter response')
|
78 |
return HttpResponseRedirect(reverse('edit_profile')) |
79 |
|
80 |
request.session['request_token'] = dict(cgi.parse_qsl(content)) |
81 |
url = "%s?oauth_token=%s" % (authenticate_url,
|
82 |
request.session['request_token']['oauth_token']) |
83 |
|
84 |
return HttpResponseRedirect(url)
|
85 |
|
86 |
|
87 |
@requires_auth_provider('twitter', login=True) |
88 |
@require_http_methods(["GET", "POST"]) |
89 |
def authenticated( |
90 |
request, |
91 |
template='im/third_party_check_local.html',
|
92 |
extra_context={} |
93 |
): |
94 |
|
95 |
if not 'request_token' in request.session: |
96 |
messages.error(request, 'Twitter handshake failed')
|
97 |
return HttpResponseRedirect(reverse('edit_profile')) |
98 |
|
99 |
token = oauth.Token(request.session['request_token']['oauth_token'], |
100 |
request.session['request_token']['oauth_token_secret']) |
101 |
client = oauth.Client(consumer, token) |
102 |
|
103 |
# Step 2. Request the authorized access token from Twitter.
|
104 |
resp, content = client.request(access_token_url, "GET")
|
105 |
if resp['status'] != '200': |
106 |
try:
|
107 |
del request.session['request_token'] |
108 |
except:
|
109 |
pass
|
110 |
messages.error(request, 'Invalid Twitter response')
|
111 |
return HttpResponseRedirect(reverse('edit_profile')) |
112 |
|
113 |
access_token = dict(cgi.parse_qsl(content))
|
114 |
userid = access_token['user_id']
|
115 |
|
116 |
# an existing user accessed the view
|
117 |
if request.user.is_authenticated():
|
118 |
if request.user.has_auth_provider('twitter', identifier=userid): |
119 |
return HttpResponseRedirect(reverse('edit_profile')) |
120 |
|
121 |
# automatically add eppn provider to user
|
122 |
user = request.user |
123 |
if not request.user.can_add_auth_provider('twitter', |
124 |
identifier=userid): |
125 |
messages.error(request, 'Account already exists.')
|
126 |
return HttpResponseRedirect(reverse('edit_profile')) |
127 |
|
128 |
user.add_auth_provider('twitter', identifier=userid)
|
129 |
return HttpResponseRedirect(reverse('edit_profile')) |
130 |
|
131 |
try:
|
132 |
# astakos user exists ?
|
133 |
user = AstakosUser.objects.get_auth_provider_user( |
134 |
'twitter',
|
135 |
identifier=userid |
136 |
) |
137 |
if user.is_active:
|
138 |
# authenticate user
|
139 |
return prepare_response(request,
|
140 |
user, |
141 |
request.GET.get('next'),
|
142 |
'renew' in request.GET) |
143 |
elif not user.activation_sent: |
144 |
message = _('Your request is pending activation')
|
145 |
#TODO: use astakos_messages
|
146 |
if not settings.MODERATION_ENABLED: |
147 |
url = user.get_resend_activation_url() |
148 |
msg_extra = _('<a href="%s">Resend activation email?</a>') % url
|
149 |
message = message + u' ' + msg_extra
|
150 |
|
151 |
messages.error(request, message) |
152 |
return HttpResponseRedirect(reverse('login')) |
153 |
|
154 |
else:
|
155 |
#TODO: use astakos_messages
|
156 |
message = _(u'Account disabled. Please contact support')
|
157 |
messages.error(request, message) |
158 |
return HttpResponseRedirect(reverse('login')) |
159 |
|
160 |
except AstakosUser.DoesNotExist, e:
|
161 |
#TODO: use astakos_messages
|
162 |
# eppn not stored in astakos models, create pending profile
|
163 |
user, created = PendingThirdPartyUser.objects.get_or_create( |
164 |
third_party_identifier=userid, |
165 |
provider='twitter',
|
166 |
) |
167 |
# update pending user
|
168 |
user.affiliation = 'Twitter'
|
169 |
user.generate_token() |
170 |
user.save() |
171 |
|
172 |
extra_context['provider'] = 'twitter' |
173 |
extra_context['token'] = user.token
|
174 |
extra_context['signup_url'] = reverse('twitter_signup', args=(user.token,)) |
175 |
|
176 |
return render_response(
|
177 |
template, |
178 |
context_instance=get_context(request, extra_context) |
179 |
) |
180 |
|
181 |
|
182 |
@requires_auth_provider('twitter', login=True, create=True) |
183 |
@require_http_methods(["GET"]) |
184 |
@requires_anonymous
|
185 |
def signup( |
186 |
request, |
187 |
token, |
188 |
backend=None,
|
189 |
on_creation_template='im/third_party_registration.html',
|
190 |
extra_context={}): |
191 |
|
192 |
extra_context = extra_context or {}
|
193 |
if not token: |
194 |
#TODO: use astakos_messages
|
195 |
return HttpResponseBadRequest(_('Missing key parameter.')) |
196 |
|
197 |
pending = get_object_or_404(PendingThirdPartyUser, token=token) |
198 |
d = pending.__dict__ |
199 |
d.pop('_state', None) |
200 |
d.pop('id', None) |
201 |
d.pop('token', None) |
202 |
d.pop('created', None) |
203 |
user = AstakosUser(**d) |
204 |
|
205 |
try:
|
206 |
backend = backend or get_backend(request)
|
207 |
except ImproperlyConfigured, e:
|
208 |
messages.error(request, e) |
209 |
else:
|
210 |
extra_context['form'] = backend.get_signup_form(
|
211 |
provider='twitter',
|
212 |
instance=user |
213 |
) |
214 |
|
215 |
extra_context['provider'] = 'twitter' |
216 |
extra_context['third_party_token'] = token
|
217 |
return render_response(
|
218 |
on_creation_template, |
219 |
context_instance=get_context(request, extra_context) |
220 |
) |
221 |
|