Statistics
| Branch: | Tag: | Revision:

root / djangobackends / shibauthBackend.py @ 0492a5b5

History | View | Annotate | Download (2 kB)

1
# -*- coding: utf-8 -*- vim:fileencoding=utf-8:
2
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
3

    
4
# Copyright (C) 2010-2014 GRNET S.A.
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
#
19

    
20

    
21
from django.contrib.auth.models import User, UserManager, Permission, Group
22
from django.conf import settings
23

    
24
class shibauthBackend:
25
    def authenticate(self, **kwargs):
26
        username = kwargs.get('username')
27
        firstname = kwargs.get('firstname')
28
        lastname = kwargs.get('lastname')
29
        mail = kwargs.get('mail')
30
        authsource = kwargs.get('authsource')
31
        if authsource != 'shibboleth':
32
            return None
33
        try:
34
            user = self._auth_user(username, firstname, lastname, mail)
35
        except:
36
            return None
37
        if not user:
38
            return None
39
        return user
40

    
41
    def _auth_user(self, username, firstname, lastname, mail):
42

    
43
        try:
44
            user = User.objects.get(username__exact=username)
45
        # The user did not exist. Create one with no privileges
46
        except: 
47
            user = User.objects.create_user(username, mail, None)
48
            user.first_name = firstname
49
            user.last_name = lastname
50
            user.is_staff = False
51
            user.is_superuser = False
52
            user.is_active = False
53
            user.save()
54

    
55
        return user
56

    
57
    def get_user(self, user_id):
58
        try:
59
            return User.objects.get(pk=user_id)
60
        except User.DoesNotExist:
61
            return None