web/webconfig.py
branchstable
changeset 8605 797fc2e2fb78
parent 8601 1a6000ff2080
child 8669 62213a34726e
equal deleted inserted replaced
8604:7bacc4f21edc 8605:797fc2e2fb78
    19 
    19 
    20 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    21 _ = unicode
    21 _ = unicode
    22 
    22 
    23 import os
    23 import os
       
    24 import hmac
       
    25 from uuid import uuid4
    24 from os.path import join, exists, split, isdir
    26 from os.path import join, exists, split, isdir
    25 from warnings import warn
    27 from warnings import warn
    26 
    28 
    27 from logilab.common.decorators import cached
    29 from logilab.common.decorators import cached, cachedproperty
    28 from logilab.common.deprecation import deprecated
    30 from logilab.common.deprecation import deprecated
    29 
    31 
    30 from cubicweb import ConfigurationError
    32 from cubicweb import ConfigurationError
    31 from cubicweb.toolsutils import read_config
    33 from cubicweb.toolsutils import read_config
    32 from cubicweb.cwconfig import CubicWebConfiguration, register_persistent_options, merge_options
    34 from cubicweb.cwconfig import CubicWebConfiguration, register_persistent_options, merge_options
   269         except KeyError:
   271         except KeyError:
   270             user, passwd = None, None
   272             user, passwd = None, None
   271         except UnicodeDecodeError:
   273         except UnicodeDecodeError:
   272             raise ConfigurationError("anonymous information should only contains ascii")
   274             raise ConfigurationError("anonymous information should only contains ascii")
   273         return user, passwd
   275         return user, passwd
       
   276 
       
   277     @cachedproperty
       
   278     def _instance_salt(self):
       
   279         """This random key/salt is used to sign content to be sent back by
       
   280         browsers, eg. in the error report form.
       
   281         """
       
   282         return str(uuid4())
       
   283 
       
   284     def sign_text(self, text):
       
   285         """sign some text for later checking"""
       
   286         # replace \r\n so we do not depend on whether a browser "reencode"
       
   287         # original message using \r\n or not
       
   288         return hmac.new(self._instance_salt,
       
   289                         text.strip().replace('\r\n', '\n')).hexdigest()
       
   290 
       
   291     def check_text_sign(self, text, signature):
       
   292         """check the text signature is equal to the given signature"""
       
   293         return self.sign_text(text) == signature
       
   294 
   274 
   295 
   275     def locate_resource(self, rid):
   296     def locate_resource(self, rid):
   276         """return the (directory, filename) where the given resource
   297         """return the (directory, filename) where the given resource
   277         may be found
   298         may be found
   278         """
   299         """