Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / target / twitter.py @ 564a2292

History | View | Annotate | Download (7.5 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
import json
35

    
36
from django.http import HttpResponseBadRequest
37
from django.utils.translation import ugettext as _
38
from django.contrib import messages
39
from django.template import RequestContext
40
from django.views.decorators.http import require_http_methods
41
from django.http import HttpResponseRedirect
42
from django.core.urlresolvers import reverse
43
from django.core.exceptions import ImproperlyConfigured
44
from django.shortcuts import get_object_or_404
45

    
46
from urlparse import urlunsplit, urlsplit
47

    
48
from astakos.im.util import prepare_response, get_context, login_url
49
from astakos.im.views import requires_anonymous, render_response, \
50
        requires_auth_provider, required_auth_methods_assigned
51
from astakos.im.settings import ENABLE_LOCAL_ACCOUNT_MIGRATION, BASEURL
52
from astakos.im.models import AstakosUser, PendingThirdPartyUser
53
from astakos.im.forms import LoginForm
54
from astakos.im.activation_backends import get_backend, SimpleBackend
55
from astakos.im import settings
56
from astakos.im import auth_providers
57
from astakos.im.target import add_pending_auth_provider, get_pending_key, \
58
    handle_third_party_signup
59

    
60
import astakos.im.messages as astakos_messages
61

    
62
import logging
63

    
64
logger = logging.getLogger(__name__)
65

    
66
import oauth2 as oauth
67
import cgi
68
import urllib
69

    
70
consumer = oauth.Consumer(settings.TWITTER_TOKEN, settings.TWITTER_SECRET)
71
client = oauth.Client(consumer)
72

    
73
request_token_url = 'http://twitter.com/oauth/request_token'
74
access_token_url = 'http://twitter.com/oauth/access_token'
75
authenticate_url = 'http://twitter.com/oauth/authenticate'
76

    
77
@requires_auth_provider('twitter', login=True)
78
@require_http_methods(["GET", "POST"])
79
def login(request):
80
    force_login = request.GET.get('force_login',
81
                                  settings.TWITTER_AUTH_FORCE_LOGIN)
82
    resp, content = client.request(request_token_url, "GET")
83
    if resp['status'] != '200':
84
        messages.error(request, 'Invalid Twitter response')
85
        return HttpResponseRedirect(reverse('edit_profile'))
86

    
87
    request.session['request_token'] = dict(cgi.parse_qsl(content))
88
    params = {
89
        'oauth_token': request.session['request_token']['oauth_token'],
90
    }
91
    if force_login:
92
        params['force_login'] = 1
93

    
94
    if request.GET.get('key', None):
95
        request.session['pending_key'] = request.GET.get('key')
96

    
97

    
98
    url = "%s?%s" % (authenticate_url, urllib.urlencode(params))
99

    
100
    return HttpResponseRedirect(url)
101

    
102

    
103
@requires_auth_provider('twitter', login=True)
104
@require_http_methods(["GET", "POST"])
105
def authenticated(
106
    request,
107
    template='im/third_party_check_local.html',
108
    extra_context={}):
109

    
110
    if request.GET.get('denied'):
111
        return HttpResponseRedirect(reverse('edit_profile'))
112

    
113
    if not 'request_token' in request.session:
114
        messages.error(request, 'Twitter handshake failed')
115
        return HttpResponseRedirect(reverse('edit_profile'))
116

    
117
    token = oauth.Token(request.session['request_token']['oauth_token'],
118
        request.session['request_token']['oauth_token_secret'])
119
    client = oauth.Client(consumer, token)
120

    
121
    # Step 2. Request the authorized access token from Twitter.
122
    resp, content = client.request(access_token_url, "GET")
123
    if resp['status'] != '200':
124
        try:
125
            del request.session['request_token']
126
        except:
127
            pass
128
        messages.error(request, 'Invalid Twitter response')
129
        return HttpResponseRedirect(reverse('edit_profile'))
130

    
131
    access_token = dict(cgi.parse_qsl(content))
132
    userid = access_token['user_id']
133
    username = access_token.get('screen_name', userid)
134
    provider_info = {'screen_name': username}
135
    affiliation = 'Twitter.com'
136

    
137
    third_party_key = get_pending_key(request)
138

    
139
    # an existing user accessed the view
140
    if request.user.is_authenticated():
141
        if request.user.has_auth_provider('twitter', identifier=userid):
142
            return HttpResponseRedirect(reverse('edit_profile'))
143

    
144
        # automatically add eppn provider to user
145
        user = request.user
146
        if not request.user.can_add_auth_provider('twitter',
147
                                                  identifier=userid):
148
            # TODO: handle existing uuid message separately
149
            messages.error(request, _(astakos_messages.AUTH_PROVIDER_ADD_FAILED) +
150
                          u' ' + _(astakos_messages.AUTH_PROVIDER_ADD_EXISTS))
151
            return HttpResponseRedirect(reverse('edit_profile'))
152

    
153
        user.add_auth_provider('twitter', identifier=userid,
154
                               affiliation=affiliation,
155
                               provider_info=provider_info)
156
        messages.success(request, astakos_messages.AUTH_PROVIDER_ADDED)
157
        return HttpResponseRedirect(reverse('edit_profile'))
158

    
159
    try:
160
        # astakos user exists ?
161
        user = AstakosUser.objects.get_auth_provider_user(
162
            'twitter',
163
            identifier=userid
164
        )
165
        if user.is_active:
166
            # authenticate user
167
            response = prepare_response(request,
168
                                    user,
169
                                    request.GET.get('next'),
170
                                    'renew' in request.GET)
171
            provider = auth_providers.get_provider('twitter')
172
            messages.success(request, _(astakos_messages.LOGIN_SUCCESS) %
173
                             _(provider.get_login_message_display))
174
            add_pending_auth_provider(request, third_party_key)
175
            response.set_cookie('astakos_last_login_method', 'twitter')
176
            return response
177
        else:
178
            message = user.get_inactive_message()
179
            messages.error(request, message)
180
            return HttpResponseRedirect(login_url(request))
181

    
182
    except AstakosUser.DoesNotExist, e:
183
        user_info = {'affiliation': affiliation}
184
        return handle_third_party_signup(request, userid, 'twitter',
185
                                         third_party_key,
186
                                         provider_info,
187
                                         user_info,
188
                                         template,
189
                                         extra_context)
190