root / snf-astakos-app / astakos / im / target / __init__.py @ dd5f8f4d
History | View | Annotate | Download (4.6 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.contrib import messages |
37 |
from django.utils.translation import ugettext as _ |
38 |
from django.http import HttpResponseRedirect |
39 |
from django.core.urlresolvers import reverse |
40 |
|
41 |
from astakos.im.models import PendingThirdPartyUser |
42 |
from astakos.im.util import get_query |
43 |
from astakos.im import messages as astakos_messages |
44 |
from astakos.im import auth_providers |
45 |
from astakos.im.util import prepare_response, get_context |
46 |
from astakos.im.views import requires_anonymous, render_response |
47 |
|
48 |
|
49 |
def add_pending_auth_provider(request, third_party_token): |
50 |
|
51 |
if third_party_token:
|
52 |
# use requests to assign the account he just authenticated with with
|
53 |
# a third party provider account
|
54 |
try:
|
55 |
request.user.add_pending_auth_provider(third_party_token) |
56 |
messages.success(request, _(astakos_messages.AUTH_PROVIDER_ADDED)) |
57 |
except PendingThirdPartyUser.DoesNotExist:
|
58 |
messages.error(request, _(astakos_messages.AUTH_PROVIDER_ADD_FAILED)) |
59 |
|
60 |
|
61 |
def get_pending_key(request): |
62 |
|
63 |
third_party_token = get_query(request).get('key', False) |
64 |
if not third_party_token: |
65 |
third_party_token = request.session.get('pending_key', None) |
66 |
if third_party_token:
|
67 |
del request.session['pending_key'] |
68 |
return third_party_token
|
69 |
|
70 |
|
71 |
def handle_third_party_signup(request, userid, provider_module, third_party_key, |
72 |
provider_info={}, |
73 |
pending_user_params={}, |
74 |
template="im/third_party_check_local.html",
|
75 |
extra_context={}): |
76 |
|
77 |
# user wants to add another third party login method
|
78 |
if third_party_key:
|
79 |
messages.error(request, _(astakos_messages.AUTH_PROVIDER_INVALID_LOGIN)) |
80 |
return HttpResponseRedirect(reverse('login') + "?key=%s" % third_party_key) |
81 |
|
82 |
provider = auth_providers.get_provider(provider_module) |
83 |
if not provider.is_available_for_create(): |
84 |
messages.error(request, |
85 |
_(astakos_messages.AUTH_PROVIDER_INVALID_LOGIN)) |
86 |
return HttpResponseRedirect(reverse('login')) |
87 |
|
88 |
# eppn not stored in astakos models, create pending profile
|
89 |
user, created = PendingThirdPartyUser.objects.get_or_create( |
90 |
third_party_identifier=userid, |
91 |
provider=provider_module, |
92 |
) |
93 |
# update pending user
|
94 |
for param, value in pending_user_params.iteritems(): |
95 |
setattr(user, param, value)
|
96 |
|
97 |
user.info = json.dumps(provider_info) |
98 |
user.generate_token() |
99 |
user.save() |
100 |
|
101 |
extra_context['provider'] = provider_module
|
102 |
extra_context['provider_title'] = provider.get_title_display
|
103 |
extra_context['token'] = user.token
|
104 |
extra_context['signup_url'] = reverse('signup') + \ |
105 |
"?third_party_token=%s" % user.token
|
106 |
extra_context['add_url'] = reverse('index') + \ |
107 |
"?key=%s#other-login-methods" % user.token
|
108 |
extra_context['can_create'] = provider.is_available_for_create()
|
109 |
extra_context['can_add'] = provider.is_available_for_add()
|
110 |
|
111 |
|
112 |
return render_response(
|
113 |
template, |
114 |
context_instance=get_context(request, extra_context) |
115 |
) |
116 |
|