Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / auth_providers.py @ 7233d542

History | View | Annotate | Download (4.9 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

    
38
from astakos.im import settings
39

    
40
import logging
41

    
42
logger = logging.getLogger(__name__)
43

    
44
# providers registry
45
PROVIDERS = {}
46

    
47
class AuthProviderBase(type):
48

    
49
    def __new__(cls, name, bases, dct):
50
        include = False
51
        if [b for b in bases if isinstance(b, AuthProviderBase)]:
52
            type_id = dct.get('module')
53
            if type_id:
54
                include = True
55
            if type_id in settings.IM_MODULES:
56
                dct['module_enabled'] = True
57

    
58
        newcls = super(AuthProviderBase, cls).__new__(cls, name, bases, dct)
59
        if include:
60
            PROVIDERS[type_id] = newcls
61
        return newcls
62

    
63

    
64
class AuthProvider(object):
65

    
66
    __metaclass__ = AuthProviderBase
67

    
68
    module = None
69
    module_active = False
70
    module_enabled = False
71
    one_per_user = False
72

    
73
    def __init__(self, user=None):
74
        self.user = user
75

    
76
    def get_setting(self, name, default=None):
77
        attr = 'AUTH_PROVIDER_%s_%s' % (self.module.upper(), name.upper())
78
        return getattr(settings, attr, default)
79

    
80
    def is_available_for_login(self):
81
        """ A user can login using authentication provider"""
82
        return self.is_active() and self.get_setting('CAN_LOGIN',
83
                                                     self.is_active())
84

    
85
    def is_available_for_create(self):
86
        """ A user can create an account using this provider"""
87
        return self.is_active() and self.get_setting('CAN_CREATE',
88
                                                   self.is_active())
89

    
90
    def is_available_for_add(self):
91
        """ A user can assign provider authentication method"""
92
        return self.is_active() and self.get_setting('CAN_ADD',
93
                                                   self.is_active())
94

    
95
    def is_active(self):
96
        return self.module in settings.IM_MODULES
97

    
98

    
99
class LocalAuthProvider(AuthProvider):
100
    module = 'local'
101
    title = _('Local password')
102
    description = _('Create a local password for your account')
103

    
104

    
105
    @property
106
    def add_url(self):
107
        return reverse('password_change')
108

    
109
    add_description = _('Create a local password for your account')
110
    login_template = 'auth/local_login_form.html'
111
    add_template = 'auth/local_add_action.html'
112
    one_per_user = True
113
    details_tpl = _('You can login to your account using your'
114
                    ' %(auth_backend)s password.')
115

    
116
    @property
117
    def extra_actions(self):
118
        return [(_('Change password'), reverse('password_change')), ]
119

    
120

    
121
class ShibbolethAuthProvider(AuthProvider):
122
    module = 'shibboleth'
123
    title = _('Academic credentials (Shibboleth)')
124
    description = _('Allows you to login to your account using your academic '
125
                    'credentials')
126

    
127
    @property
128
    def add_url(self):
129
        return reverse('astakos.im.target.shibboleth.login')
130

    
131
    add_description = _('Allows you to login to your account using your academic '
132
                    'credentials')
133
    login_template = 'auth/shibboleth_login_form.html'
134
    add_template = 'auth/shibboleth_add_action.html'
135
    details_tpl = _('You can login to your account using your'
136
                    ' shibboleth credentials. Shibboleth id: %(identifier)s')
137

    
138

    
139
def get_provider(id, user_obj=None, default=None):
140
    """
141
    Return a provider instance from the auth providers registry.
142
    """
143
    return PROVIDERS.get(id, default)(user_obj)
144