Package couchdbkit :: Package ext :: Package pylons :: Package auth :: Module model
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.ext.pylons.auth.model

 1  # -*- coding: utf-8 - 
 2  # 
 3  # This file is part of couchdbkit released under the MIT license.  
 4  # See the NOTICE for more information. 
 5   
 6  from hashlib import sha256 
 7  import os 
 8   
 9   
10  from .... import Document, SchemaListProperty, StringProperty, \ 
11  StringListProperty 
12 13 -class Permission(Document):
14 name = StringProperty(required=True)
15
16 -class Group(Document):
17 """ 18 Group class, contains multiple permissions. 19 """ 20 name = StringProperty(required=True) 21 permissions = SchemaListProperty(Permission)
22
23 -class User(Document):
24 """The base User model. This should be extended by the user.""" 25 login = StringProperty(required=True) 26 password = StringProperty(required=True) 27 groups = StringListProperty() 28 29 @staticmethod
30 - def _hash_password(cleartext):
31 if isinstance(cleartext, unicode): 32 password_8bit = cleartext.encode('UTF-8') 33 else: 34 password_8bit = cleartext 35 36 salt = sha256() 37 salt.update(os.urandom(60)) 38 hash = sha256() 39 hash.update(password_8bit + salt.hexdigest()) 40 hashed_password = salt.hexdigest() + hash.hexdigest() 41 42 if not isinstance(hashed_password, unicode): 43 hashed_password = hashed_password.decode('UTF-8') 44 return hashed_password
45
46 - def set_password(self, password):
48 49 @staticmethod
50 - def authenticate(login, password):
51 user = User.view("user/by_login", key=login).one() 52 if not user: 53 return None 54 55 hashed_pass = sha256() 56 hashed_pass.update(password + user.password[:64]) 57 if user.password[64:] != hashed_pass.hexdigest(): 58 return None 59 return user
60