root / snf-astakos-app / astakos / im / auth_providers.py @ 531fca05
History | View | Annotate | Download (10.1 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 |
|
35 |
from django.core.urlresolvers import reverse |
36 |
from django.utils.translation import ugettext as _ |
37 |
from django.utils.datastructures import SortedDict |
38 |
|
39 |
from django.conf import settings |
40 |
|
41 |
from astakos.im import settings as astakos_settings |
42 |
from astakos.im import messages as astakos_messages |
43 |
|
44 |
import logging |
45 |
import urllib |
46 |
|
47 |
logger = logging.getLogger(__name__) |
48 |
|
49 |
# providers registry
|
50 |
PROVIDERS = {} |
51 |
REQUIRED_PROVIDERS = {} |
52 |
|
53 |
class AuthProviderBase(type): |
54 |
|
55 |
def __new__(cls, name, bases, dct): |
56 |
include = False
|
57 |
if [b for b in bases if isinstance(b, AuthProviderBase)]: |
58 |
type_id = dct.get('module')
|
59 |
if type_id:
|
60 |
include = True
|
61 |
if type_id in astakos_settings.IM_MODULES: |
62 |
dct['module_enabled'] = True |
63 |
|
64 |
newcls = super(AuthProviderBase, cls).__new__(cls, name, bases, dct)
|
65 |
if include:
|
66 |
PROVIDERS[type_id] = newcls |
67 |
if newcls().is_required():
|
68 |
REQUIRED_PROVIDERS[type_id] = newcls |
69 |
return newcls
|
70 |
|
71 |
|
72 |
class AuthProvider(object): |
73 |
|
74 |
__metaclass__ = AuthProviderBase |
75 |
|
76 |
module = None
|
77 |
module_active = False
|
78 |
module_enabled = False
|
79 |
one_per_user = False
|
80 |
login_prompt = _('Login using ')
|
81 |
primary_login_prompt = _('Login using ')
|
82 |
login_message = None
|
83 |
logout_message = 'You may still be logged in %(provider)s though. Consider logging out from there too.'
|
84 |
remote_authenticate = True
|
85 |
remote_logout_url = None
|
86 |
logout_from_provider_text = None
|
87 |
icon_url = None
|
88 |
icon_medium_url = None
|
89 |
|
90 |
def get_message(self, msg, **kwargs): |
91 |
params = kwargs |
92 |
params.update({'provider': self.get_title_display}) |
93 |
|
94 |
override_msg = getattr(self, 'get_%s_message_display' % msg.lower(), None) |
95 |
msg = 'AUTH_PROVIDER_%s' % msg
|
96 |
return override_msg or getattr(astakos_messages, msg, msg) % params |
97 |
|
98 |
@property
|
99 |
def add_url(self): |
100 |
return reverse(self.login_view) |
101 |
|
102 |
def __init__(self, user=None): |
103 |
self.user = user
|
104 |
for tpl in ['login_prompt', 'login', 'signup_prompt']: |
105 |
tpl_name = '%s_%s' % (tpl, 'template') |
106 |
override = self.get_setting(tpl_name)
|
107 |
if override:
|
108 |
setattr(self, tpl_name, override) |
109 |
|
110 |
for key in ['one_per_user']: |
111 |
override = self.get_setting(key)
|
112 |
if override != None: |
113 |
setattr(self, key, override) |
114 |
|
115 |
self.login_message = self.login_message or self.get_title_display |
116 |
if self.logout_message and "%" in self.logout_message: |
117 |
logout_text_display = self.logout_from_provider_text or 'at %s' % self.get_title_display |
118 |
self.logout_message = self.logout_message % {'provider': |
119 |
logout_text_display} |
120 |
else:
|
121 |
self.logout_message = self.logout_message or '' |
122 |
|
123 |
if not self.icon_url: |
124 |
self.icon_url = '%s%s' % (settings.MEDIA_URL, 'im/auth/icons/%s.png' % |
125 |
self.get_title_display.lower())
|
126 |
|
127 |
if not self.icon_medium_url: |
128 |
self.icon_medium_url = '%s%s' % (settings.MEDIA_URL, 'im/auth/icons-medium/%s.png' % |
129 |
self.module.lower())
|
130 |
|
131 |
def __getattr__(self, key): |
132 |
if not key.startswith('get_'): |
133 |
return super(AuthProvider, self).__getattribute__(key) |
134 |
|
135 |
if key.endswith('_display') or key.endswith('template'): |
136 |
attr = key.replace('_display', '').replace('get_','') |
137 |
settings_attr = self.get_setting(attr.upper())
|
138 |
if not settings_attr: |
139 |
return getattr(self, attr) |
140 |
return _(settings_attr)
|
141 |
else:
|
142 |
return super(AuthProvider, self).__getattr__(key) |
143 |
|
144 |
def get_logout_message(self): |
145 |
content = ''
|
146 |
if self.remote_logout_url: |
147 |
content = '<a href="%s" title="Logout from %%s"></a>' % self.remote_logou_url |
148 |
return content % (self.get_logout_message_display % self.get_title_display) |
149 |
|
150 |
def get_setting(self, name, default=None): |
151 |
attr = 'ASTAKOS_AUTH_PROVIDER_%s_%s' % (self.module.upper(), name.upper()) |
152 |
attr_sec = 'ASTAKOS_%s_%s' % (self.module.upper(), name.upper()) |
153 |
if not hasattr(settings, attr): |
154 |
return getattr(settings, attr_sec, default) |
155 |
|
156 |
return getattr(settings, attr, default) |
157 |
|
158 |
def is_available_for_login(self): |
159 |
""" A user can login using authentication provider"""
|
160 |
return self.is_active() and self.get_setting('CAN_LOGIN', |
161 |
self.is_active())
|
162 |
|
163 |
def is_available_for_create(self): |
164 |
""" A user can create an account using this provider"""
|
165 |
return self.is_active() and self.get_setting('CAN_CREATE', |
166 |
self.is_active())
|
167 |
|
168 |
def is_available_for_add(self): |
169 |
""" A user can assign provider authentication method"""
|
170 |
return self.is_active() and self.get_setting('CAN_ADD', |
171 |
self.is_active())
|
172 |
|
173 |
def is_required(self): |
174 |
"""Provider required (user cannot remove the last one)"""
|
175 |
return self.is_active() and self.get_setting('REQUIRED', False) |
176 |
|
177 |
def is_active(self): |
178 |
return self.module in astakos_settings.IM_MODULES |
179 |
|
180 |
|
181 |
class LocalAuthProvider(AuthProvider): |
182 |
module = 'local'
|
183 |
title = _('Local password')
|
184 |
description = _('Create a local password for your account')
|
185 |
add_prompt = _('Create a local password for your account')
|
186 |
login_prompt = _('if you already have a username and password')
|
187 |
signup_prompt = _('New to ~okeanos ?')
|
188 |
signup_link_prompt = _('create an account now')
|
189 |
login_view = 'password_change'
|
190 |
remote_authenticate = False
|
191 |
logout_message = ''
|
192 |
|
193 |
one_per_user = True
|
194 |
|
195 |
login_template = 'im/auth/local_login_form.html'
|
196 |
login_prompt_template = 'im/auth/local_login_prompt.html'
|
197 |
signup_prompt_template = 'im/auth/local_signup_prompt.html'
|
198 |
details_tpl = _('You can login to your account using your'
|
199 |
' %(auth_backend)s password.')
|
200 |
|
201 |
@property
|
202 |
def extra_actions(self): |
203 |
return [(_('Change password'), reverse('password_change')), ] |
204 |
|
205 |
|
206 |
class ShibbolethAuthProvider(AuthProvider): |
207 |
module = 'shibboleth'
|
208 |
title = _('Academic account (Shibboleth)')
|
209 |
add_prompt = _('Allows you to login to your account using your academic '
|
210 |
'account')
|
211 |
details_tpl = _('Shibboleth account \'%(identifier)s\' is connected to your '
|
212 |
' account.')
|
213 |
user_title = _('Academic credentials (%(identifier)s)')
|
214 |
primary_login_prompt = _('If you are a student/researcher/faculty you can'
|
215 |
' login using your university-credentials in'
|
216 |
' the following page')
|
217 |
login_view = 'astakos.im.target.shibboleth.login'
|
218 |
|
219 |
login_template = 'im/auth/shibboleth_login.html'
|
220 |
login_prompt_template = 'im/auth/third_party_provider_generic_login_prompt.html'
|
221 |
logout_from_provider_text = ' at your Academic account (shibboleth)'
|
222 |
|
223 |
|
224 |
class TwitterAuthProvider(AuthProvider): |
225 |
module = 'twitter'
|
226 |
title = _('Twitter')
|
227 |
add_prompt = _('Allows you to login to your account using Twitter')
|
228 |
details_tpl = _('Twitter screen name: %(info_screen_name)s')
|
229 |
user_title = _('Twitter (%(info_screen_name)s)')
|
230 |
login_view = 'astakos.im.target.twitter.login'
|
231 |
|
232 |
login_template = 'im/auth/third_party_provider_generic_login.html'
|
233 |
login_prompt_template = 'im/auth/third_party_provider_generic_login_prompt.html'
|
234 |
|
235 |
|
236 |
class GoogleAuthProvider(AuthProvider): |
237 |
module = 'google'
|
238 |
title = _('Google')
|
239 |
add_prompt = _('Allows you to login to your account using Google')
|
240 |
details_tpl = _('Google account: %(info_email)s')
|
241 |
user_title = _('Google (%(info_email)s)')
|
242 |
login_view = 'astakos.im.target.google.login'
|
243 |
|
244 |
login_template = 'im/auth/third_party_provider_generic_login.html'
|
245 |
login_prompt_template = 'im/auth/third_party_provider_generic_login_prompt.html'
|
246 |
|
247 |
|
248 |
class LinkedInAuthProvider(AuthProvider): |
249 |
module = 'linkedin'
|
250 |
title = _('LinkedIn')
|
251 |
add_prompt = _('Allows you to login to your account using LinkedIn')
|
252 |
user_title = _('LinkedIn (%(info_emailAddress)s)')
|
253 |
details_tpl = _('LinkedIn account: %(info_emailAddress)s')
|
254 |
login_view = 'astakos.im.target.linkedin.login'
|
255 |
|
256 |
login_template = 'im/auth/third_party_provider_generic_login.html'
|
257 |
login_prompt_template = 'im/auth/third_party_provider_generic_login_prompt.html'
|
258 |
|
259 |
|
260 |
def get_provider(id, user_obj=None, default=None): |
261 |
"""
|
262 |
Return a provider instance from the auth providers registry.
|
263 |
"""
|
264 |
return PROVIDERS.get(id, default)(user_obj) |
265 |
|