[IMP] auth_crypt base_crypt cleanup
[odoo/odoo.git] / addons / auth_crypt / auth_crypt.py
1 #
2 # Implements encrypting functions.
3 #
4 # Copyright (c) 2008, F S 3 Consulting Inc.
5 #
6 # Maintainer:
7 # Alec Joseph Rivera (agi<at>fs3.ph)
8 # refactored by Antony Lesuisse <al<at>openerp.com>
9 #
10
11 import hashlib
12 import logging
13 from random import sample
14 from string import ascii_letters, digits
15
16 import openerp
17 from openerp.osv import fields, osv
18
19 _logger = logging.getLogger(__name__)
20
21 magic_md5 = '$1$'
22
23 def gen_salt(length=8, symbols=None):
24     if symbols is None:
25         symbols = ascii_letters + digits
26     return ''.join(sample(symbols, length))
27
28 def md5crypt( raw_pw, salt, magic=magic_md5 ):
29     """ md5crypt FreeBSD crypt(3) based on but different from md5
30
31     The md5crypt is based on Mark Johnson's md5crypt.py, which in turn is
32     based on  FreeBSD src/lib/libcrypt/crypt.c (1.2)  by  Poul-Henning Kamp.
33     Mark's port can be found in  ActiveState ASPN Python Cookbook.  Kudos to
34     Poul and Mark. -agi
35
36     Original license:
37
38     * "THE BEER-WARE LICENSE" (Revision 42):
39     *
40     * <phk@login.dknet.dk>  wrote  this file.  As  long as  you retain  this
41     * notice  you can do  whatever you want with this stuff. If we meet some
42     * day,  and you think this stuff is worth it,  you can buy me  a beer in
43     * return.
44     *
45     * Poul-Henning Kamp
46     """
47     raw_pw = raw_pw.encode('utf-8')
48     salt = salt.encode('utf-8')
49     hash = hashlib.md5()
50     hash.update( raw_pw + magic + salt )
51     st = hashlib.md5()
52     st.update( raw_pw + salt + raw_pw)
53     stretch = st.digest()
54
55     for i in range( 0, len( raw_pw ) ):
56         hash.update( stretch[i % 16] )
57
58     i = len( raw_pw )
59
60     while i:
61         if i & 1:
62             hash.update('\x00')
63         else:
64             hash.update( raw_pw[0] )
65         i >>= 1
66
67     saltedmd5 = hash.digest()
68
69     for i in range( 1000 ):
70         hash = hashlib.md5()
71
72         if i & 1:
73             hash.update( raw_pw )
74         else:
75             hash.update( saltedmd5 )
76
77         if i % 3:
78             hash.update( salt )
79         if i % 7:
80             hash.update( raw_pw )
81         if i & 1:
82             hash.update( saltedmd5 )
83         else:
84             hash.update( raw_pw )
85
86         saltedmd5 = hash.digest()
87
88     itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
89
90     rearranged = ''
91     for a, b, c in ((0, 6, 12), (1, 7, 13), (2, 8, 14), (3, 9, 15), (4, 10, 5)):
92         v = ord( saltedmd5[a] ) << 16 | ord( saltedmd5[b] ) << 8 | ord( saltedmd5[c] )
93
94         for i in range(4):
95             rearranged += itoa64[v & 0x3f]
96             v >>= 6
97
98     v = ord( saltedmd5[11] )
99
100     for i in range( 2 ):
101         rearranged += itoa64[v & 0x3f]
102         v >>= 6
103
104     return magic + salt + '$' + rearranged
105
106 class res_users(osv.osv):
107     _inherit = "res.users"
108
109     def set_pw(self, cr, uid, id, name, value, args, context):
110         if value:
111             encrypted = md5crypt(value, gen_salt())
112             cr.execute('update res_users set password_crypt=%s where id=%s', (encrypted, int(id)))
113         del value
114
115     def get_pw( self, cr, uid, ids, name, args, context ):
116         cr.execute('select id, password from res_users where id in %s', (tuple(map(int, ids)),))
117         stored_pws = cr.fetchall()
118         res = {}
119
120         for id, stored_pw in stored_pws:
121             res[id] = stored_pw
122
123         return res
124
125     _columns = {
126         'password': fields.function(get_pw, fnct_inv=set_pw, type='char', string='Password', invisible=True, store=True),
127         'password_crypt': fields.char(string='Encrypted Password', invisible=True),
128     }
129
130     def check_credentials(self, cr, uid, password):
131         # convert to base_crypt if needed
132         cr.execute('SELECT password, password_crypt FROM res_users WHERE id=%s AND active', (uid,))
133         if cr.rowcount:
134             stored_password, stored_password_crypt = cr.fetchone()
135             if password and not stored_password_crypt:
136                 salt = gen_salt()
137                 stored_password_crypt = md5crypt(stored_password, salt)
138                 cr.execute("UPDATE res_users SET password='', password_crypt=%s WHERE id=%s", (stored_password_crypt, uid))
139         try:
140             return super(res_users, self).check_credentials(cr, uid, password)
141         except openerp.exceptions.AccessDenied:
142             # check md5crypt
143             if stored_password_crypt[:len(magic_md5)] == "$1$":
144                 salt = stored_password_crypt[len(magic_md5):11]
145                 if stored_password_crypt == md5crypt(password, salt):
146                     return
147             # Reraise password incorrect
148             raise
149
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: