root / snf-astakos-app / astakos / im / target / google.py @ 91bbc1a4
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 |
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
|
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 logging |
61 |
import time |
62 |
import astakos.im.messages as astakos_messages |
63 |
import urlparse |
64 |
import urllib |
65 |
|
66 |
logger = logging.getLogger(__name__) |
67 |
|
68 |
import oauth2 as oauth |
69 |
import cgi |
70 |
|
71 |
signature_method = oauth.SignatureMethod_HMAC_SHA1() |
72 |
|
73 |
OAUTH_CONSUMER_KEY = settings.GOOGLE_CLIENT_ID |
74 |
OAUTH_CONSUMER_SECRET = settings.GOOGLE_SECRET |
75 |
|
76 |
consumer = oauth.Consumer(key=OAUTH_CONSUMER_KEY, secret=OAUTH_CONSUMER_SECRET) |
77 |
client = oauth.Client(consumer) |
78 |
|
79 |
token_scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'
|
80 |
authenticate_url = 'https://accounts.google.com/o/oauth2/auth'
|
81 |
access_token_url = 'https://www.googleapis.com/oauth2/v1/tokeninfo'
|
82 |
request_token_url = 'https://accounts.google.com/o/oauth2/token'
|
83 |
|
84 |
|
85 |
def get_redirect_uri(): |
86 |
return "%s%s" % (settings.BASEURL, |
87 |
reverse('astakos.im.target.google.authenticated'))
|
88 |
|
89 |
@requires_auth_provider('google', login=True) |
90 |
@require_http_methods(["GET", "POST"]) |
91 |
def login(request): |
92 |
params = { |
93 |
'scope': token_scope,
|
94 |
'response_type': 'code', |
95 |
'redirect_uri': get_redirect_uri(),
|
96 |
'client_id': settings.GOOGLE_CLIENT_ID
|
97 |
} |
98 |
force_login = request.GET.get('force_login', False) |
99 |
if force_login:
|
100 |
params['approval_prompt'] = 'force' |
101 |
|
102 |
if request.GET.get('key', None): |
103 |
request.session['pending_key'] = request.GET.get('key') |
104 |
|
105 |
url = "%s?%s" % (authenticate_url, urllib.urlencode(params))
|
106 |
return HttpResponseRedirect(url)
|
107 |
|
108 |
|
109 |
@requires_auth_provider('google', login=True) |
110 |
@require_http_methods(["GET", "POST"]) |
111 |
def authenticated( |
112 |
request, |
113 |
template='im/third_party_check_local.html',
|
114 |
extra_context={} |
115 |
): |
116 |
|
117 |
if request.GET.get('error', None): |
118 |
return HttpResponseRedirect(reverse('edit_profile')) |
119 |
|
120 |
# TODO: Handle errors, e.g. error=access_denied
|
121 |
try:
|
122 |
code = request.GET.get('code', None) |
123 |
params = { |
124 |
'code': code,
|
125 |
'client_id': settings.GOOGLE_CLIENT_ID,
|
126 |
'client_secret': settings.GOOGLE_SECRET,
|
127 |
'redirect_uri': get_redirect_uri(),
|
128 |
'grant_type': 'authorization_code' |
129 |
} |
130 |
get_token_url = "%s" % (request_token_url,)
|
131 |
resp, content = client.request(get_token_url, "POST",
|
132 |
body=urllib.urlencode(params)) |
133 |
token = json.loads(content).get('access_token', None) |
134 |
|
135 |
resp, content = client.request("%s?access_token=%s" % (access_token_url,
|
136 |
token) , "GET")
|
137 |
access_token_data = json.loads(content) |
138 |
except Exception, e: |
139 |
messages.error(request, 'Invalid Google response. Please contact support')
|
140 |
return HttpResponseRedirect(reverse('edit_profile')) |
141 |
|
142 |
if not access_token_data.get('user_id', None): |
143 |
messages.error(request, 'Invalid Google response. Please contact support')
|
144 |
return HttpResponseRedirect(reverse('edit_profile')) |
145 |
|
146 |
userid = access_token_data['user_id']
|
147 |
username = access_token_data.get('email', None) |
148 |
provider_info = access_token_data |
149 |
affiliation = 'Google.com'
|
150 |
|
151 |
third_party_key = get_pending_key(request) |
152 |
|
153 |
# an existing user accessed the view
|
154 |
if request.user.is_authenticated():
|
155 |
if request.user.has_auth_provider('google', identifier=userid): |
156 |
return HttpResponseRedirect(reverse('edit_profile')) |
157 |
|
158 |
# automatically add eppn provider to user
|
159 |
user = request.user |
160 |
if not request.user.can_add_auth_provider('google', |
161 |
identifier=userid): |
162 |
# TODO: handle existing uuid message separately
|
163 |
messages.error(request, _(astakos_messages.AUTH_PROVIDER_ADD_FAILED) + |
164 |
u' ' + _(astakos_messages.AUTH_PROVIDER_ADD_EXISTS))
|
165 |
return HttpResponseRedirect(reverse('edit_profile')) |
166 |
|
167 |
user.add_auth_provider('google', identifier=userid,
|
168 |
affiliation=affiliation, |
169 |
provider_info=provider_info) |
170 |
messages.success(request, astakos_messages.AUTH_PROVIDER_ADDED) |
171 |
return HttpResponseRedirect(reverse('edit_profile')) |
172 |
|
173 |
try:
|
174 |
# astakos user exists ?
|
175 |
user = AstakosUser.objects.get_auth_provider_user( |
176 |
'google',
|
177 |
identifier=userid |
178 |
) |
179 |
if user.is_active:
|
180 |
# authenticate user
|
181 |
response = prepare_response(request, |
182 |
user, |
183 |
userid, |
184 |
request.GET.get('next'),
|
185 |
'renew' in request.GET) |
186 |
messages.success(request, _(astakos_messages.LOGIN_SUCCESS)) |
187 |
add_pending_auth_provider(request, third_party_key) |
188 |
response.set_cookie('astakos_last_login_method', 'google') |
189 |
return response
|
190 |
else:
|
191 |
message = user.get_inactive_message() |
192 |
messages.error(request, message) |
193 |
return HttpResponseRedirect(login_url(request))
|
194 |
|
195 |
except AstakosUser.DoesNotExist, e:
|
196 |
user_info = {'affiliation': affiliation}
|
197 |
return handle_third_party_signup(request, userid, 'google', |
198 |
third_party_key, |
199 |
provider_info, |
200 |
user_info, |
201 |
template, |
202 |
extra_context) |
203 |
|