Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / auth_providers.py @ ee45eb81

History | View | Annotate | Download (7.5 kB)

1 d2633501 Kostas Papadimitriou
# Copyright 2011 GRNET S.A. All rights reserved.
2 d2633501 Kostas Papadimitriou
#
3 d2633501 Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
4 d2633501 Kostas Papadimitriou
# without modification, are permitted provided that the following
5 d2633501 Kostas Papadimitriou
# conditions are met:
6 d2633501 Kostas Papadimitriou
#
7 d2633501 Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
8 d2633501 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
9 d2633501 Kostas Papadimitriou
#      disclaimer.
10 d2633501 Kostas Papadimitriou
#
11 d2633501 Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
12 d2633501 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
13 d2633501 Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
14 d2633501 Kostas Papadimitriou
#      provided with the distribution.
15 d2633501 Kostas Papadimitriou
#
16 d2633501 Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d2633501 Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d2633501 Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d2633501 Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 d2633501 Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d2633501 Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d2633501 Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d2633501 Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d2633501 Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d2633501 Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d2633501 Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d2633501 Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
28 d2633501 Kostas Papadimitriou
#
29 d2633501 Kostas Papadimitriou
# The views and conclusions contained in the software and
30 d2633501 Kostas Papadimitriou
# documentation are those of the authors and should not be
31 d2633501 Kostas Papadimitriou
# interpreted as representing official policies, either expressed
32 d2633501 Kostas Papadimitriou
# or implied, of GRNET S.A.
33 d2633501 Kostas Papadimitriou
34 d2633501 Kostas Papadimitriou
35 d2633501 Kostas Papadimitriou
from django.core.urlresolvers import reverse
36 d2633501 Kostas Papadimitriou
from django.utils.translation import ugettext as _
37 081070a5 Kostas Papadimitriou
from django.utils.datastructures import SortedDict
38 d2633501 Kostas Papadimitriou
39 081070a5 Kostas Papadimitriou
from django.conf import settings
40 c4b1a172 Kostas Papadimitriou
41 081070a5 Kostas Papadimitriou
from astakos.im import settings as astakos_settings
42 c4b1a172 Kostas Papadimitriou
from astakos.im import messages as astakos_messages
43 d2633501 Kostas Papadimitriou
44 d2633501 Kostas Papadimitriou
import logging
45 d2633501 Kostas Papadimitriou
46 d2633501 Kostas Papadimitriou
logger = logging.getLogger(__name__)
47 d2633501 Kostas Papadimitriou
48 d2633501 Kostas Papadimitriou
# providers registry
49 6012535a Kostas Papadimitriou
PROVIDERS = {}
50 d2633501 Kostas Papadimitriou
51 d2633501 Kostas Papadimitriou
class AuthProviderBase(type):
52 d2633501 Kostas Papadimitriou
53 d2633501 Kostas Papadimitriou
    def __new__(cls, name, bases, dct):
54 d2633501 Kostas Papadimitriou
        include = False
55 d2633501 Kostas Papadimitriou
        if [b for b in bases if isinstance(b, AuthProviderBase)]:
56 d2633501 Kostas Papadimitriou
            type_id = dct.get('module')
57 d2633501 Kostas Papadimitriou
            if type_id:
58 d2633501 Kostas Papadimitriou
                include = True
59 081070a5 Kostas Papadimitriou
            if type_id in astakos_settings.IM_MODULES:
60 d2633501 Kostas Papadimitriou
                dct['module_enabled'] = True
61 d2633501 Kostas Papadimitriou
62 d2633501 Kostas Papadimitriou
        newcls = super(AuthProviderBase, cls).__new__(cls, name, bases, dct)
63 d2633501 Kostas Papadimitriou
        if include:
64 6012535a Kostas Papadimitriou
            PROVIDERS[type_id] = newcls
65 d2633501 Kostas Papadimitriou
        return newcls
66 d2633501 Kostas Papadimitriou
67 d2633501 Kostas Papadimitriou
68 d2633501 Kostas Papadimitriou
class AuthProvider(object):
69 d2633501 Kostas Papadimitriou
70 d2633501 Kostas Papadimitriou
    __metaclass__ = AuthProviderBase
71 d2633501 Kostas Papadimitriou
72 d2633501 Kostas Papadimitriou
    module = None
73 d2633501 Kostas Papadimitriou
    module_active = False
74 d2633501 Kostas Papadimitriou
    module_enabled = False
75 d2633501 Kostas Papadimitriou
    one_per_user = False
76 cb4714e7 Kostas Papadimitriou
    login_prompt = _('Login using')
77 d2633501 Kostas Papadimitriou
78 c4b1a172 Kostas Papadimitriou
    def get_message(self, msg, **kwargs):
79 c4b1a172 Kostas Papadimitriou
        params = kwargs
80 c4b1a172 Kostas Papadimitriou
        params.update({'provider': self.get_title_display})
81 c4b1a172 Kostas Papadimitriou
82 c4b1a172 Kostas Papadimitriou
        override_msg = getattr(self, 'get_%s_message_display' % msg.lower(), None)
83 c4b1a172 Kostas Papadimitriou
        msg = 'AUTH_PROVIDER_%s' % msg
84 c4b1a172 Kostas Papadimitriou
        return override_msg or getattr(astakos_messages, msg, msg) % params
85 c4b1a172 Kostas Papadimitriou
86 d2633501 Kostas Papadimitriou
    def __init__(self, user=None):
87 d2633501 Kostas Papadimitriou
        self.user = user
88 d2633501 Kostas Papadimitriou
89 081070a5 Kostas Papadimitriou
    def __getattr__(self, key):
90 081070a5 Kostas Papadimitriou
        if not key.startswith('get_'):
91 6974e526 Kostas Papadimitriou
            return super(AuthProvider, self).__getattribute__(key)
92 081070a5 Kostas Papadimitriou
93 081070a5 Kostas Papadimitriou
        if key.endswith('_display') or key.endswith('template'):
94 081070a5 Kostas Papadimitriou
            attr = key.replace('_display', '').replace('get_','')
95 081070a5 Kostas Papadimitriou
            settings_attr = self.get_setting(attr.upper())
96 081070a5 Kostas Papadimitriou
            if not settings_attr:
97 081070a5 Kostas Papadimitriou
                return getattr(self, attr)
98 cb4714e7 Kostas Papadimitriou
            return _(settings_attr)
99 081070a5 Kostas Papadimitriou
        else:
100 081070a5 Kostas Papadimitriou
            return super(AuthProvider, self).__getattr__(key)
101 081070a5 Kostas Papadimitriou
102 d2633501 Kostas Papadimitriou
    def get_setting(self, name, default=None):
103 081070a5 Kostas Papadimitriou
        attr = 'ASTAKOS_AUTH_PROVIDER_%s_%s' % (self.module.upper(), name.upper())
104 1d59653f Kostas Papadimitriou
        attr_sec = 'ASTAKOS_%s_%s' % (self.module.upper(), name.upper())
105 1d59653f Kostas Papadimitriou
        if not hasattr(settings, attr):
106 1d59653f Kostas Papadimitriou
            return getattr(settings, attr_sec, default)
107 d2633501 Kostas Papadimitriou
        return getattr(settings, attr, default)
108 d2633501 Kostas Papadimitriou
109 d2633501 Kostas Papadimitriou
    def is_available_for_login(self):
110 d2633501 Kostas Papadimitriou
        """ A user can login using authentication provider"""
111 d2633501 Kostas Papadimitriou
        return self.is_active() and self.get_setting('CAN_LOGIN',
112 d2633501 Kostas Papadimitriou
                                                     self.is_active())
113 d2633501 Kostas Papadimitriou
114 d2633501 Kostas Papadimitriou
    def is_available_for_create(self):
115 d2633501 Kostas Papadimitriou
        """ A user can create an account using this provider"""
116 d2633501 Kostas Papadimitriou
        return self.is_active() and self.get_setting('CAN_CREATE',
117 d2633501 Kostas Papadimitriou
                                                   self.is_active())
118 d2633501 Kostas Papadimitriou
119 d2633501 Kostas Papadimitriou
    def is_available_for_add(self):
120 d2633501 Kostas Papadimitriou
        """ A user can assign provider authentication method"""
121 d2633501 Kostas Papadimitriou
        return self.is_active() and self.get_setting('CAN_ADD',
122 d2633501 Kostas Papadimitriou
                                                   self.is_active())
123 d2633501 Kostas Papadimitriou
124 d2633501 Kostas Papadimitriou
    def is_active(self):
125 081070a5 Kostas Papadimitriou
        return self.module in astakos_settings.IM_MODULES
126 d2633501 Kostas Papadimitriou
127 d2633501 Kostas Papadimitriou
128 d2633501 Kostas Papadimitriou
class LocalAuthProvider(AuthProvider):
129 d2633501 Kostas Papadimitriou
    module = 'local'
130 d2633501 Kostas Papadimitriou
    title = _('Local password')
131 d2633501 Kostas Papadimitriou
    description = _('Create a local password for your account')
132 081070a5 Kostas Papadimitriou
    create_prompt =  _('Create an account')
133 081070a5 Kostas Papadimitriou
    add_prompt =  _('Create a local password for your account')
134 cb4714e7 Kostas Papadimitriou
    login_prompt = _('if you already have a username and password')
135 cb4714e7 Kostas Papadimitriou
    signup_prompt = _('New to ~Okeanos ?')
136 d2633501 Kostas Papadimitriou
137 d2633501 Kostas Papadimitriou
138 d2633501 Kostas Papadimitriou
    @property
139 d2633501 Kostas Papadimitriou
    def add_url(self):
140 d2633501 Kostas Papadimitriou
        return reverse('password_change')
141 d2633501 Kostas Papadimitriou
142 d2633501 Kostas Papadimitriou
    one_per_user = True
143 081070a5 Kostas Papadimitriou
144 081070a5 Kostas Papadimitriou
    login_template = 'im/auth/local_login_form.html'
145 081070a5 Kostas Papadimitriou
    login_prompt_template = 'im/auth/local_login_prompt.html'
146 081070a5 Kostas Papadimitriou
    signup_prompt_template = 'im/auth/local_signup_prompt.html'
147 d2633501 Kostas Papadimitriou
    details_tpl = _('You can login to your account using your'
148 d2633501 Kostas Papadimitriou
                    ' %(auth_backend)s password.')
149 d2633501 Kostas Papadimitriou
150 d2633501 Kostas Papadimitriou
    @property
151 d2633501 Kostas Papadimitriou
    def extra_actions(self):
152 d2633501 Kostas Papadimitriou
        return [(_('Change password'), reverse('password_change')), ]
153 d2633501 Kostas Papadimitriou
154 158a72ec root
class LDAPAuthProvider(AuthProvider):
155 158a72ec root
    module = 'ldap'
156 158a72ec root
    title = _('LDAP credentials')
157 158a72ec root
    description = _('Allows you to login using your LDAP credentials')
158 158a72ec root
159 158a72ec root
    one_per_user = True
160 158a72ec root
161 158a72ec root
    login_template = 'im/auth/local_login_form.html'
162 158a72ec root
    login_prompt_template = 'im/auth/local_login_prompt.html'
163 158a72ec root
    signup_prompt_template = 'im/auth/local_signup_prompt.html'
164 158a72ec root
    details_tpl = _('You can login to your account using your'
165 158a72ec root
                    ' %(auth_backend)s password.')
166 d2633501 Kostas Papadimitriou
167 d2633501 Kostas Papadimitriou
class ShibbolethAuthProvider(AuthProvider):
168 d2633501 Kostas Papadimitriou
    module = 'shibboleth'
169 d2633501 Kostas Papadimitriou
    title = _('Academic credentials (Shibboleth)')
170 d2633501 Kostas Papadimitriou
    description = _('Allows you to login to your account using your academic '
171 d2633501 Kostas Papadimitriou
                    'credentials')
172 081070a5 Kostas Papadimitriou
    add_prompt = _('Add academic credentials to your account.')
173 b9231ded Kostas Papadimitriou
    details_tpl = _('Shibboleth account \'%(identifier)s\' is connected to your '
174 3a72a5d4 Kostas Papadimitriou
                    ' account.')
175 3a72a5d4 Kostas Papadimitriou
    user_title = _('Academic credentials (%(identifier)s)')
176 cb4714e7 Kostas Papadimitriou
    primary_login_prompt = _('If you are a student/researcher/faculty you can'
177 cb4714e7 Kostas Papadimitriou
                             ' login using your university-credentials in'
178 cb4714e7 Kostas Papadimitriou
                             ' the following page')
179 d2633501 Kostas Papadimitriou
180 d2633501 Kostas Papadimitriou
    @property
181 d2633501 Kostas Papadimitriou
    def add_url(self):
182 d2633501 Kostas Papadimitriou
        return reverse('astakos.im.target.shibboleth.login')
183 d2633501 Kostas Papadimitriou
184 081070a5 Kostas Papadimitriou
    login_template = 'im/auth/shibboleth_login.html'
185 081070a5 Kostas Papadimitriou
    login_prompt_template = 'im/auth/shibboleth_login_prompt.html'
186 d2633501 Kostas Papadimitriou
187 d2633501 Kostas Papadimitriou
188 c101b32b Kostas Papadimitriou
class TwitterAuthProvider(AuthProvider):
189 c101b32b Kostas Papadimitriou
    module = 'twitter'
190 c101b32b Kostas Papadimitriou
    title = _('Twitter')
191 c101b32b Kostas Papadimitriou
    description = _('Allows you to login to your account using your twitter '
192 c101b32b Kostas Papadimitriou
                    'account')
193 c101b32b Kostas Papadimitriou
    add_prompt = _('Connect with your Twitter account.')
194 3a72a5d4 Kostas Papadimitriou
    details_tpl = _('Twitter screen name: %(info_screen_name)s')
195 3a72a5d4 Kostas Papadimitriou
    user_title = _('Twitter (%(info_screen_name)s)')
196 c101b32b Kostas Papadimitriou
197 c101b32b Kostas Papadimitriou
    @property
198 c101b32b Kostas Papadimitriou
    def add_url(self):
199 c101b32b Kostas Papadimitriou
        return reverse('astakos.im.target.twitter.login')
200 c101b32b Kostas Papadimitriou
201 c101b32b Kostas Papadimitriou
    login_template = 'im/auth/twitter_login.html'
202 c101b32b Kostas Papadimitriou
    login_prompt_template = 'im/auth/twitter_login_prompt.html'
203 c101b32b Kostas Papadimitriou
204 d2633501 Kostas Papadimitriou
def get_provider(id, user_obj=None, default=None):
205 d2633501 Kostas Papadimitriou
    """
206 d2633501 Kostas Papadimitriou
    Return a provider instance from the auth providers registry.
207 d2633501 Kostas Papadimitriou
    """
208 d2633501 Kostas Papadimitriou
    return PROVIDERS.get(id, default)(user_obj)