Coverage for marvin.security : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" marvin.security ~~~~~~~~~~~~~~~
Security-related utilities and tools.
"""
# Don't require scrypt to be installed in debug mode, since it's hell to compile on windows except ImportError: # pragma: no cover from hashlib import sha256 class scrypt(object): # pylint: disable=invalid-name """ Faking scrypt using a single sha256 hash if scrypt is not available. """ @staticmethod def hash(password, salt, n, p, r): # pylint: disable=unused-argument,invalid-name """ Simulate scrypt.hash in dev mode. """ if not (current_app.config.get('DEBUG') or current_app.config.get('TESTING')): # pragma: no cover raise ValueError("scrypt needs to be configured in production!") hasher = sha256() hasher.update(password) hasher.update(salt) return hasher.digest()
""" Generate a random bytestring that can be used as a cryptographic salt.
The resulting bytestring will be approx 88 bytes long, 64 bytes of pure randomness and 33% overhead due to base64-encoding the result. """
""" Hash the given password.
Return a string in the format ``method$salt$hash``. """ # pylint: disable=invalid-name
""" Get optimal N, p and r values for this sytem. """ #FIXME: Hardcoded for now since lead dev is on windows and can't test against working scrypt...
""" Make sure the given argument is an instance of bytes. On python3, this is probably the case, but on python2 we have to encode it with an encoding, in this case we'll use utf-8. """
""" Check that the given password hashes to the given password_hash.
:param password: The password to test :param password_hash: A method$salt$hash string we can extract the method and params used in the original hashing from. """ # pylint: disable=invalid-name # For now we only support scrypt hashing, so we can ignore the algorithm, # and we know the number of params. This should be rewritten if we want to # support other algorithms or variable length params
""" Decode the given auth_token and return the data dict therein, or fail with a HTTP 400 error. """
""" Get the user object from the given auth_data. Raises HTTP 401 if token is no longer valid or user doesn't exist anymore. """ # Import the user model here to avoid circular imports abort(401)
""" Extract the token from a HTTP Authorization header.
The header is supposed to look be in the format `Authorization: Token <access token>`
:returns: Auth token from header, or `None` if none was found. """
""" Connect with @app.before_request to authenticate users using the `auth_token` request param.
Assigns a user to `g.user`, AnonymousUser if no auth_token was sent in the request. """ # pylint: disable=protected-access else: |