view piecrust/admin/bcryptfallback.py @ 853:f070a4fc033c

core: Continue PieCrust3 refactor, simplify pages. The asset pipeline is still the only function pipeline at this point. * No more `QualifiedPage`, and several other pieces of code deleted. * Data providers are simpler and more focused. For instance, the page iterator doesn't try to support other types of items. * Route parameters are proper known source metadata to remove the confusion between the two. * Make the baker and pipeline more correctly manage records and record histories. * Add support for record collapsing and deleting stale outputs in the asset pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 21 May 2017 00:06:59 -0700
parents a9f4a6e60b0b
children 0d699f04968c
line wrap: on
line source

import hashlib
import logging


print_warning = False
logger = logging.getLogger(__name__)


try:
    from bcrypt import hashpw, gensalt
except ImportError:
    print_warning = True

    def hashpw(password, *args, **kwargs):
        return hashlib.sha512(password).hexdigest().encode('utf8')

    def gensalt(*args, **kwargs):
        return b''


try:
    from flask.ext.bcrypt import Bcrypt
except ImportError:
    print_warning = True

    def generate_password_hash(password):
        return hashlib.sha512(password.encode('utf8')).hexdigest()

    def check_password_hash(reference, check):
        check_hash = hashlib.sha512(check.encode('utf8')).hexdigest()
        return check_hash == reference

    class SHA512Fallback(object):
        is_fallback_bcrypt = True

        def __init__(self, app=None):
            self.generate_password_hash = generate_password_hash
            self.check_password_hash = check_password_hash

        def init_app(self, app):
            app.bcrypt = self

    Bcrypt = SHA512Fallback


if print_warning:
    logging.warning("Bcrypt not available... falling back to SHA512.")
    logging.warning("Run `pip install Flask-Bcrypt` for more secure "
                    "password hashing.")