cubicweb/server/utils.py
changeset 12014 116005b5fce9
parent 12013 7b975655d0ae
child 12193 e4ee04d442be
equal deleted inserted replaced
12013:7b975655d0ae 12014:116005b5fce9
    24 import sys
    24 import sys
    25 import logging
    25 import logging
    26 from threading import Thread
    26 from threading import Thread
    27 from getpass import getpass
    27 from getpass import getpass
    28 
    28 
    29 from six import PY2, text_type
    29 from six import PY2
    30 from six.moves import input
    30 from six.moves import input
    31 
    31 
    32 from passlib.utils import handlers as uh, to_hash_str
    32 from passlib.utils import handlers as uh, to_hash_str
    33 from passlib.context import CryptContext
    33 from passlib.context import CryptContext
    34 
    34 
    57     # passlib 1.5 wants calc_checksum, 1.6 wants _calc_checksum
    57     # passlib 1.5 wants calc_checksum, 1.6 wants _calc_checksum
    58     def calc_checksum(self, secret):
    58     def calc_checksum(self, secret):
    59         return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8')
    59         return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8')
    60     _calc_checksum = calc_checksum
    60     _calc_checksum = calc_checksum
    61 
    61 
       
    62 
    62 _CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'],
    63 _CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'],
    63                            deprecated=['cubicwebmd5crypt', 'des_crypt'])
    64                            deprecated=['cubicwebmd5crypt', 'des_crypt'])
    64 verify_and_update = _CRYPTO_CTX.verify_and_update
    65 verify_and_update = _CRYPTO_CTX.verify_and_update
       
    66 
    65 
    67 
    66 def crypt_password(passwd, salt=None):
    68 def crypt_password(passwd, salt=None):
    67     """return the encrypted password using the given salt or a generated one
    69     """return the encrypted password using the given salt or a generated one
    68     """
    70     """
    69     if salt is None:
    71     if salt is None:
    72     if salt == '':
    74     if salt == '':
    73         return salt
    75         return salt
    74     try:
    76     try:
    75         if _CRYPTO_CTX.verify(passwd, salt):
    77         if _CRYPTO_CTX.verify(passwd, salt):
    76             return salt
    78             return salt
    77     except ValueError: # e.g. couldn't identify hash
    79     except ValueError:  # e.g. couldn't identify hash
    78         pass
    80         pass
    79     # wrong password
    81     # wrong password
    80     return b''
    82     return b''
       
    83 
    81 
    84 
    82 @deprecated('[3.22] no more necessary, directly get eschema.eid')
    85 @deprecated('[3.22] no more necessary, directly get eschema.eid')
    83 def eschema_eid(cnx, eschema):
    86 def eschema_eid(cnx, eschema):
    84     """get eid of the CWEType entity for the given yams type.
    87     """get eid of the CWEType entity for the given yams type.
    85 
    88 
    90     return eschema.eid
    93     return eschema.eid
    91 
    94 
    92 
    95 
    93 DEFAULT_MSG = 'we need a manager connection on the repository \
    96 DEFAULT_MSG = 'we need a manager connection on the repository \
    94 (the server doesn\'t have to run, even should better not)'
    97 (the server doesn\'t have to run, even should better not)'
       
    98 
    95 
    99 
    96 def manager_userpasswd(user=None, msg=DEFAULT_MSG, confirm=False,
   100 def manager_userpasswd(user=None, msg=DEFAULT_MSG, confirm=False,
    97                        passwdmsg='password'):
   101                        passwdmsg='password'):
    98     if not user:
   102     if not user:
    99         if msg:
   103         if msg:
   155 
   159 
   156     return scheduler.enter(interval, 1, task, argument=args)
   160     return scheduler.enter(interval, 1, task, argument=args)
   157 
   161 
   158 
   162 
   159 _MARKER = object()
   163 _MARKER = object()
       
   164 
       
   165 
   160 def func_name(func):
   166 def func_name(func):
   161     name = getattr(func, '__name__', _MARKER)
   167     name = getattr(func, '__name__', _MARKER)
   162     if name is _MARKER:
   168     if name is _MARKER:
   163         name = getattr(func, 'func_name', _MARKER)
   169         name = getattr(func, 'func_name', _MARKER)
   164     if name is _MARKER:
   170     if name is _MARKER: