Projet

Général

Profil

Télécharger (2,48 ko) Statistiques
| Branche: | Tag: | Révision:

mandayejs / mandayejs / mandaye / models.py @ a9a12993

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

    
17
import base64
18
from Crypto.Cipher import AES
19

    
20
from django.db import models
21
from django.conf import settings
22
from django.utils.translation import ugettext_lazy as _
23

    
24
from jsonfield import JSONField
25

    
26
from mandayejs.mandaye.utils import get_password_field
27

    
28

    
29
class UserCredentials(models.Model):
30
    user = models.ForeignKey('auth.User')
31
    locators = JSONField(_('locators'), default={}, blank=True) 
32
    linked = models.BooleanField(_('associated'), default=False, blank=True)
33

    
34
    class Meta:
35
        unique_together = ('user',)
36

    
37
    def __unicode__(self):
38
        return self.user.get_full_name() \
39
            or self.user.email \
40
            or self.user.username
41

    
42
    def save(self, *args, **kwargs):
43
        self.encrypt()
44
        super(UserCredentials, self).save(*args, **kwargs)
45

    
46
    def _get_cipher(self):
47
        """Return cipher object
48
        """
49
        return AES.new(getattr(settings, 'SECRET_KEY'), AES.MODE_CFB, "0000000000000000")
50

    
51
    def encrypt(self,):
52
        """Encrypt password
53
        """
54
        password_field_name = get_password_field()
55
        cipher = self._get_cipher()
56
        self.locators[password_field_name] = \
57
           base64.b64encode(cipher.encrypt(
58
               self.locators.get(password_field_name,'')
59
            )) 
60

    
61
        return self.locators
62

    
63
    def decrypt(self,):
64
        """Decrypt password
65
        """
66
        password_field_name = get_password_field()
67
        cipher = self._get_cipher()
68
        self.locators[password_field_name] = \
69
            cipher.decrypt(
70
                base64.b64decode(
71
                    self.locators.get(password_field_name,'')
72
            ))
73

    
74
        return self.locators
75

    
76
    def to_login_info(self, decrypt=False):
77
        if decrypt:
78
            self.decrypt()
79
        return {'#'+k : v for k,v in self.locators.items() }
80

    
(4-4/6)