Esempio n. 1
0
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
    """Return the hash of password using pbkdf2."""
    if digest is None:
        digest = hashlib.sha256
    dklen = dklen or None
    password = force_bytes(password)
    salt = force_bytes(salt)
    return hashlib.pbkdf2_hmac(digest().name, password, salt, iterations,
                               dklen)
Esempio n. 2
0
def _generate_cache_key(request, method, headerlist, key_prefix):
    """Return a cache key from the headers given in the header list."""
    ctx = hashlib.md5()
    for header in headerlist:
        value = request.META.get(header)
        if value is not None:
            ctx.update(force_bytes(value))
    url = hashlib.md5(force_bytes(iri_to_uri(request.build_absolute_uri())))
    cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
        key_prefix, method, url.hexdigest(), ctx.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Esempio n. 3
0
    def __init__(self, param, cursor, strings_only=False):
        # With raw SQL queries, datetimes can reach this function
        # without being converted by DateTimeField.get_db_prep_value.
        if settings.USE_TZ and (isinstance(param, datetime.datetime)
                                and not isinstance(param, Oracle_datetime)):
            param = Oracle_datetime.from_datetime(param)

        string_size = 0
        # Oracle doesn't recognize True and False correctly.
        if param is True:
            param = 1
        elif param is False:
            param = 0
        if hasattr(param, 'bind_parameter'):
            self.force_bytes = param.bind_parameter(cursor)
        elif isinstance(param, (Database.Binary, datetime.timedelta)):
            self.force_bytes = param
        else:
            # To transmit to the database, we need Unicode if supported
            # To get size right, we must consider bytes.
            self.force_bytes = force_text(param, cursor.charset, strings_only)
            if isinstance(self.force_bytes, str):
                # We could optimize by only converting up to 4000 bytes here
                string_size = len(
                    force_bytes(param, cursor.charset, strings_only))
        if hasattr(param, 'input_size'):
            # If parameter has `input_size` attribute, use that.
            self.input_size = param.input_size
        elif string_size > 4000:
            # Mark any string param greater than 4000 characters as a CLOB.
            self.input_size = Database.CLOB
        elif isinstance(param, datetime.datetime):
            self.input_size = Database.TIMESTAMP
        else:
            self.input_size = None
Esempio n. 4
0
 def generic(self,
             method,
             path,
             data='',
             content_type='application/octet-stream',
             secure=False,
             **extra):
     """Construct an arbitrary HTTP request."""
     parsed = urlparse(str(path))  # path can be lazy
     data = force_bytes(data, settings.DEFAULT_CHARSET)
     r = {
         'PATH_INFO': self._get_path(parsed),
         'REQUEST_METHOD': method,
         'SERVER_PORT': '443' if secure else '80',
         'wsgi.url_scheme': 'https' if secure else 'http',
     }
     if data:
         r.update({
             'CONTENT_LENGTH': len(data),
             'CONTENT_TYPE': content_type,
             'wsgi.input': FakePayload(data),
         })
     r.update(extra)
     # If QUERY_STRING is absent or empty, we want to extract it from the URL.
     if not r.get('QUERY_STRING'):
         # WSGI requires latin-1 encoded strings. See get_path_info().
         query_string = parsed[4].encode().decode('iso-8859-1')
         r['QUERY_STRING'] = query_string
     return self.request(**r)
Esempio n. 5
0
def parse_rst(text, default_reference_context, thing_being_parsed=None):
    """
    Convert the string from reST to an XHTML fragment.
    """
    overrides = {
        'doctitle_xform': True,
        'initial_header_level': 3,
        "default_reference_context": default_reference_context,
        "link_base": reverse('server-admindocs-docroot').rstrip('/'),
        'raw_enabled': False,
        'file_insertion_enabled': False,
    }
    thing_being_parsed = thing_being_parsed and force_bytes(
        '<%s>' % thing_being_parsed)
    # Wrap ``text`` in some reST that sets the default role to ``cmsreference``,
    # then restores it.
    source = """
.. default-role:: cmsreference

%s

.. default-role::
"""
    parts = docutils.core.publish_parts(
        source % text,
        source_path=thing_being_parsed,
        destination_path=None,
        writer_name='html',
        settings_overrides=overrides,
    )
    return mark_safe(parts['fragment'])
Esempio n. 6
0
 def attr_value(self, target, index=0):
     """
     The attribute value for the given target node (e.g. 'PROJCS'). The index
     keyword specifies an index of the child node to return.
     """
     if not isinstance(target, str) or not isinstance(index, int):
         raise TypeError
     return capi.get_attr_value(self.ptr, force_bytes(target), index)
Esempio n. 7
0
 def _digest(cls, *args):
     """
     Generate a 32-bit digest of a set of arguments that can be used to
     shorten identifying names.
     """
     h = hashlib.md5()
     for arg in args:
         h.update(force_bytes(arg))
     return h.hexdigest()[:8]
Esempio n. 8
0
 def test_capability(self, capability):
     """
     Return a bool indicating whether the this Layer supports the given
     capability (a string).  Valid capability strings include:
       'RandomRead', 'SequentialWrite', 'RandomWrite', 'FastSpatialFilter',
       'FastFeatureCount', 'FastGetExtent', 'CreateField', 'Transactions',
       'DeleteFeature', and 'FastSetNextByIndex'.
     """
     return bool(capi.test_capability(self.ptr, force_bytes(capability)))
Esempio n. 9
0
def urlsafe_base64_decode(s):
    """
    Decode a base64 encoded string. Add back any trailing equal signs that
    might have been stripped.
    """
    s = force_bytes(s)
    try:
        return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'='))
    except (LookupError, BinasciiError) as e:
        raise ValueError(e)
Esempio n. 10
0
 def _encode_data(self, data, content_type):
     if content_type is MULTIPART_CONTENT:
         return encode_multipart(BOUNDARY, data)
     else:
         # Encode the content so that the byte representation is correct.
         match = CONTENT_TYPE_RE.match(content_type)
         if match:
             charset = match.group(1)
         else:
             charset = settings.DEFAULT_CHARSET
         return force_bytes(data, encoding=charset)
Esempio n. 11
0
 def vsi_buffer(self):
     if not self.is_vsi_based:
         return None
     # Prepare an integer that will contain the buffer length.
     out_length = c_int()
     # Get the data using the vsi file name.
     dat = capi.get_mem_buffer_from_vsi_file(
         force_bytes(self.name),
         byref(out_length),
         VSI_DELETE_BUFFER_ON_READ,
     )
     # Read the full buffer pointer.
     return string_at(dat, out_length.value)
Esempio n. 12
0
def salted_hmac(key_salt, value, secret=None):
    """
    Return the HMAC-SHA1 of 'value', using a key generated from key_salt and a
    secret (which defaults to settings.SECRET_KEY).

    A different key_salt should be passed in for every application of HMAC.
    """
    if secret is None:
        secret = settings.SECRET_KEY

    key_salt = force_bytes(key_salt)
    secret = force_bytes(secret)

    # We need to generate a derived key from our base key.  We can do this by
    # passing the key_salt and our base key through a pseudo-random function and
    # SHA1 works nicely.
    key = hashlib.sha1(key_salt + secret).digest()

    # If len(key_salt + secret) > sha_constructor().block_size, the above
    # line is redundant and could be replaced by key = key_salt + secret, since
    # the hmac module does the same thing for keys longer than the block size.
    # However, we need to ensure that we *always* do this.
    return hmac.new(key, msg=force_bytes(value), digestmod=hashlib.sha1)
Esempio n. 13
0
def truncate_name(identifier, length=None, hash_len=4):
    """
    Shorten a SQL identifier to a repeatable mangled version with the given
    length.

    If a quote stripped name contains a namespace, e.g. USERNAME"."TABLE,
    truncate the table portion only.
    """
    namespace, name = split_identifier(identifier)

    if length is None or len(name) <= length:
        return identifier

    digest = hashlib.md5(force_bytes(name)).hexdigest()[:hash_len]
    return '%s%s%s' % ('%s"."' % namespace if namespace else '', name[:length - hash_len], digest)
Esempio n. 14
0
def groups_for_user(environ, username):
    """
    Authorize a user based on groups
    """
    db.reset_queries()
    try:
        try:
            user = UserModel._default_manager.get_by_natural_key(username)
        except UserModel.DoesNotExist:
            return []
        if not user.is_active:
            return []
        return [force_bytes(group.name) for group in user.groups.all()]
    finally:
        db.close_old_connections()
Esempio n. 15
0
 def __getitem__(self, index):
     "Allows use of the index [] operator to get a layer at the index."
     if isinstance(index, str):
         try:
             layer = capi.get_layer_by_name(self.ptr, force_bytes(index))
         except GDALException:
             raise IndexError('Invalid OGR layer name given: %s.' % index)
     elif isinstance(index, int):
         if 0 <= index < self.layer_count:
             layer = capi.get_layer(self._ptr, index)
         else:
             raise IndexError(
                 'Index out of range when accessing layers in a datasource: %s.'
                 % index)
     else:
         raise TypeError('Invalid index type: %s' % type(index))
     return Layer(layer, self)
Esempio n. 16
0
 def decode(self, session_data):
     encoded_data = base64.b64decode(force_bytes(session_data))
     try:
         # could produce ValueError if there is no ':'
         hash, serialized = encoded_data.split(b':', 1)
         expected_hash = self._hash(serialized)
         if not constant_time_compare(hash.decode(), expected_hash):
             raise SuspiciousSession("Session data corrupted")
         else:
             return self.serializer().loads(serialized)
     except Exception as e:
         # ValueError, SuspiciousOperation, unpickling exceptions. If any of
         # these happen, just return an empty dictionary (an empty session).
         if isinstance(e, SuspiciousOperation):
             logger = logging.getLogger('server.security.%s' % e.__class__.__name__)
             logger.warning(str(e))
         return {}
Esempio n. 17
0
    def __init__(self,
                 ds_input,
                 ds_driver=False,
                 write=False,
                 encoding='utf-8'):
        # The write flag.
        if write:
            self._write = 1
        else:
            self._write = 0
        # See also https://trac.osgeo.org/gdal/wiki/rfc23_ogr_unicode
        self.encoding = encoding

        Driver.ensure_registered()

        if isinstance(ds_input, str):
            # The data source driver is a void pointer.
            ds_driver = Driver.ptr_type()
            try:
                # OGROpen will auto-detect the data source type.
                ds = capi.open_ds(force_bytes(ds_input), self._write,
                                  byref(ds_driver))
            except GDALException:
                # Making the error message more clear rather than something
                # like "Invalid pointer returned from OGROpen".
                raise GDALException('Could not open the datasource at "%s"' %
                                    ds_input)
        elif isinstance(ds_input, self.ptr_type) and isinstance(
                ds_driver, Driver.ptr_type):
            ds = ds_input
        else:
            raise GDALException('Invalid data source input type: %s' %
                                type(ds_input))

        if ds:
            self.ptr = ds
            self.driver = Driver(ds_driver)
        else:
            # Raise an exception if the returned pointer is NULL
            raise GDALException('Invalid data source file "%s"' % ds_input)
Esempio n. 18
0
    def __init__(self, dr_input):
        """
        Initialize an GDAL/OGR driver on either a string or integer input.
        """
        if isinstance(dr_input, str):
            # If a string name of the driver was passed in
            self.ensure_registered()

            # Checking the alias dictionary (case-insensitive) to see if an
            # alias exists for the given driver.
            if dr_input.lower() in self._alias:
                name = self._alias[dr_input.lower()]
            else:
                name = dr_input

            # Attempting to get the GDAL/OGR driver by the string name.
            for iface in (vcapi, rcapi):
                driver = c_void_p(iface.get_driver_by_name(force_bytes(name)))
                if driver:
                    break
        elif isinstance(dr_input, int):
            self.ensure_registered()
            for iface in (vcapi, rcapi):
                driver = iface.get_driver(dr_input)
                if driver:
                    break
        elif isinstance(dr_input, c_void_p):
            driver = dr_input
        else:
            raise GDALException(
                'Unrecognized input type for GDAL/OGR Driver: %s' %
                type(dr_input))

        # Making sure we get a valid pointer to the OGR Driver
        if not driver:
            raise GDALException(
                'Could not initialize GDAL/OGR Driver on input: %s' % dr_input)
        self.ptr = driver
Esempio n. 19
0
 def auth_code(self, target):
     "Return the authority code for the given string target node."
     return capi.get_auth_code(self.ptr, force_bytes(target))
Esempio n. 20
0
 def index(self, field_name):
     "Return the index of the given field name."
     i = capi.get_field_index(self.ptr, force_bytes(field_name))
     if i < 0:
         raise IndexError('Invalid OFT field name given: %s.' % field_name)
     return i
Esempio n. 21
0
 def read(self, wkt):
     if not isinstance(wkt, (bytes, str)):
         raise TypeError
     return wkt_reader_read(self.ptr, force_bytes(wkt))
Esempio n. 22
0
 def from_json(geom_input):
     return OGRGeometry(OGRGeometry._from_json(force_bytes(geom_input)))
Esempio n. 23
0
 def from_gml(cls, gml_string):
     return cls(capi.from_gml(force_bytes(gml_string)))
Esempio n. 24
0
 def import_user_input(self, user_input):
     "Import the Spatial Reference from the given user input string."
     capi.from_user_input(self.ptr, force_bytes(user_input))
Esempio n. 25
0
 def import_wkt(self, wkt):
     "Import the Spatial Reference from OGC WKT (string)"
     capi.from_wkt(self.ptr, byref(c_char_p(force_bytes(wkt))))
Esempio n. 26
0
 def write(self, content):
     if self.read_started:
         raise ValueError("Unable to write a payload after he's been read")
     content = force_bytes(content)
     self.__content.write(content)
     self.__len += len(content)
Esempio n. 27
0
 def xml(self, dialect=''):
     "Return the XML representation of this Spatial Reference."
     return capi.to_xml(self.ptr, byref(c_char_p()), force_bytes(dialect))
Esempio n. 28
0
 def convert_binaryfield_value(self, value, expression, connection):
     if isinstance(value, Database.LOB):
         value = force_bytes(value.read())
     return value
Esempio n. 29
0
def constant_time_compare(val1, val2):
    """Return True if the two strings are equal, False otherwise."""
    return hmac.compare_digest(force_bytes(val1), force_bytes(val2))
Esempio n. 30
0
def get_cookie_signer(salt='server.core.signing.get_cookie_signer'):
    Signer = import_string(settings.SIGNING_BACKEND)
    key = force_bytes(settings.SECRET_KEY)  # SECRET_KEY may be str or bytes.
    return Signer(b'server.http.cookies' + key, salt=salt)