Python flask_security.utils.encrypt_password() Examples

The following are 14 code examples of flask_security.utils.encrypt_password(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_security.utils , or try the search function .
Example #1
Source File: commands.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def create():
    '''Create a new user'''
    data = {
        'first_name': click.prompt('First name'),
        'last_name': click.prompt('Last name'),
        'email': click.prompt('Email'),
        'password': click.prompt('Password', hide_input=True),
        'password_confirm': click.prompt('Confirm Password', hide_input=True),
    }
    # Until https://github.com/mattupstate/flask-security/issues/672 is fixed
    with current_app.test_request_context():
        form = RegisterForm(MultiDict(data), meta={'csrf': False})
    if form.validate():
        data['password'] = encrypt_password(data['password'])
        del data['password_confirm']
        data['confirmed_at'] = datetime.utcnow()
        user = datastore.create_user(**data)
        success('User(id={u.id} email={u.email}) created'.format(u=user))
        return user
    errors = '\n'.join('\n'.join(e) for e in form.errors.values())
    exit_with_error('Error creating user', errors) 
Example #2
Source File: object_faker.py    From betterlifepsi with MIT License 6 votes vote down vote up
def user(self, role_names=None, organization=None):
        if role_names is None:
            role_names = []
        from psi.app.models import User, Role
        from flask_security.utils import encrypt_password
        user = User()
        user.active = True
        user.display = self.faker.name()
        user.email = self.faker.email()
        user.login = self.faker.name()
        password = self.faker.password()
        user.password = encrypt_password(password)
        if organization is None:
            user.organization = self.organization()
        else:
            user.organization = organization
        for role_name in role_names:
            role = Role.query.filter_by(name=role_name).first()
            user.roles.append(role)
        return user, password 
Example #3
Source File: test_project_views.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def a_user(app, session, project_client, name, email,
           logged_in, disabled, active):
    """ gives us a user who is a member.
    """
    from pygameweb.user.models import User, Group
    from flask_security.utils import encrypt_password
    group = Group(name='members', title='Member')
    user = User(name=name,
                email=email,
                password=encrypt_password('password'),
                disabled=disabled,
                active=active,
                roles=[group])
    session.add(user)
    session.commit()

    # https://flask-login.readthedocs.org/en/latest/#fresh-logins
    with project_client.session_transaction() as sess:
        sess['user_id'] = user.id
        sess['_fresh'] = True
    return user 
Example #4
Source File: test_wiki_views.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def user(app, session, wiki_client):
    """ gives us a user who is a member.
    """
    from pygameweb.user.models import User
    from flask_security.utils import encrypt_password
    user = User(name='joe',
                email='asdf@example.com',
                password=encrypt_password('password'))
    session.add(user)
    session.commit()
    # https://flask-login.readthedocs.org/en/latest/#fresh-logins
    with wiki_client.session_transaction() as sess:
        sess['user_id'] = user.id
        sess['_fresh'] = True

    return user 
Example #5
Source File: test_news_views.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def a_user(app, session, news_client, name, email,
           logged_in, disabled, active):
    """ gives us a user who is a member.
    """
    from pygameweb.user.models import User, Group
    from flask_security.utils import encrypt_password
    group = Group(name='admin', title='Admin')
    user = User(name=name,
                email=email,
                password=encrypt_password('password'),
                disabled=disabled,
                active=active,
                roles=[group])
    session.add(user)
    session.commit()

    # https://flask-login.readthedocs.org/en/latest/#fresh-logins
    with news_client.session_transaction() as sess:
        sess['user_id'] = user.id
        sess['_fresh'] = True
    return user 
Example #6
Source File: test_comment.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def user(app, session, comment_client):
    """ gives us a user who is a member.
    """
    from pygameweb.user.models import User
    from flask_security.utils import encrypt_password
    user = User(name='joe',
                email='asdf@example.com',
                password=encrypt_password('password'))
    session.add(user)
    session.commit()
    # https://flask-login.readthedocs.org/en/latest/#fresh-logins
    with comment_client.session_transaction() as sess:
        sess['user_id'] = user.id
        sess['_fresh'] = True

    return user 
Example #7
Source File: manage.py    From spkrepo with MIT License 5 votes vote down vote up
def run(self, **kwargs):
        # handle confirmed
        if re.sub(r"\s", "", str(kwargs.pop("confirmed"))).lower() in [
            "",
            "y",
            "yes",
            "1",
            "active",
        ]:
            kwargs["confirmed_at"] = datetime.datetime.now()

        # sanitize active input
        ai = re.sub(r"\s", "", str(kwargs["active"]))
        kwargs["active"] = ai.lower() in ["", "y", "yes", "1", "active"]

        from flask_security.forms import ConfirmRegisterForm
        from werkzeug.datastructures import MultiDict

        form = ConfirmRegisterForm(MultiDict(kwargs), csrf_enabled=False)

        if form.validate():
            kwargs["password"] = encrypt_password(kwargs["password"])
            user_datastore.create_user(**kwargs)
            print("User created successfully.")
            kwargs["password"] = "****"
            pprint(kwargs)
        else:
            print("Error creating user")
            pprint(form.errors) 
Example #8
Source File: commands.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def password(email):
    user = datastore.get_user(email)
    password = click.prompt('Enter new password', hide_input=True)
    user.password = encrypt_password(password)
    user.save() 
Example #9
Source File: views.py    From submission with MIT License 5 votes vote down vote up
def on_model_change(self, form, model, is_created):

        # If the password field isn't blank...
        if len(model.password2):

            # ... then encrypt the new password prior to storing it in the database. If the password field is blank,
            # the existing password in the database will be retained.
            model.password = encrypt_password(model.password2) 
Example #10
Source File: user.py    From mma-dexter with Apache License 2.0 5 votes vote down vote up
def create_defaults(self):
        from . import Country
        from flask_security.utils import encrypt_password

        admin_user = User()
        admin_user.first_name = "Admin"
        admin_user.last_name = "Admin"
        admin_user.admin = True
        admin_user.email = "admin@code4sa.org"
        admin_user.country = Country.query.filter(Country.name == 'South Africa').one()
        admin_user.password = encrypt_password('admin')

        return [admin_user] 
Example #11
Source File: user.py    From betterlifepsi with MIT License 5 votes vote down vote up
def on_model_change(self, form, model, is_created):
        if not is_super_admin():
            super(UserAdmin, self).on_model_change(form, model, is_created)
        # If the password field isn't blank...
        if len(model.password2):
            # ... then encrypt the new password prior to storing it in the database. If the password field is blank,
            # the existing password in the database will be retained.
            model.password = encrypt_password(model.password2) 
Example #12
Source File: views.py    From pygameweb with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def on_model_change(self, form, model, is_created):
        if len(model.password2):
            model.password = utils.encrypt_password(model.password2) 
Example #13
Source File: server.py    From flask-restless-security with MIT License 5 votes vote down vote up
def create_test_models():
    user_datastore.create_user(email='test', password=encrypt_password('test'))
    user_datastore.create_user(email='test2', password=encrypt_password('test2'))
    stuff = SomeStuff(data1=2, data2='toto', user_id=1)
    db.session.add(stuff)
    stuff = SomeStuff(data1=5, data2='titi', user_id=1)
    db.session.add(stuff)
    db.session.commit() 
Example #14
Source File: test.py    From flask-restless-security with MIT License 5 votes vote down vote up
def setUp(self):
        app.config.from_object('config.TestingConfig')
        self.client = app.test_client()

        db.init_app(app)
        with app.app_context():
            db.create_all()
            user_datastore.create_user(email='test', password=encrypt_password('test'))
            db.session.commit()