def edit_user(username): own_user = username == current_user.name if not current_user.role.is_administrator and not own_user: forbidden() user = User.query.filter_by(name=username).first() if not user: return not_found() form = UserForm(edit=True) if not form.is_submitted(): form.username.data = user.name form.email.data = user.email form.role.data = user.role.name form.active.data = user.active if not form.validate_on_submit(): return render_template('admin/form/user.html', title='Edit {}'.format(username), form=form, User=User, random_password=True, password_length={'min': TRACKER_PASSWORD_LENGTH_MIN, 'max': TRACKER_PASSWORD_LENGTH_MAX}) active_admins = User.query.filter_by(active=True, role=UserRole.administrator).count() if user.id == current_user.id and 1 == active_admins and not form.active.data: return forbidden() user.name = form.username.data user.email = form.email.data user.role = UserRole.fromstring(form.role.data) if form.random_password.data: form.password.data = random_string() if 0 != len(form.password.data): user.salt = random_string() user.password = hash_password(form.password.data, user.salt) user.active = form.active.data user_invalidate(user) db.session.commit() flash_password = '' if form.random_password.data: flash_password = '******'.format(form.password.data) flash('Edited user {}{}'.format(user.name, flash_password)) return redirect('/user')
def test_current_password_incorrect(db, client): new_password = random_string() resp = client.post(url_for('edit_own_user_profile'), follow_redirects=True, data=dict(password=new_password, password_repeat=new_password, password_current=new_password)) assert resp.status_code == 200 assert ERROR_PASSWORD_INCORRECT in resp.data.decode()
def test_password_repeat_mismatches(db, client): new_password = random_string() resp = client.post(url_for('edit_own_user_profile'), follow_redirects=True, data=dict(password=new_password, password_repeat=new_password[::-1], password_current=DEFAULT_USERNAME)) assert resp.status_code == 200 assert ERROR_PASSWORD_REPEAT_MISMATCHES in resp.data.decode()
def wrapper(db, *args, **kwargs): user = User() user.active = active user.name = username user.password = password if password else username user.role = role user.email = email if email else '{}@cyber.cyber'.format(username) user.salt = salt if salt else random_string() user.password = hash_password(user.password, user.salt) db.session.add(user) db.session.commit() func(db=db, *args, **kwargs)
def create_user(): form = UserForm() if not form.validate_on_submit(): return render_template('admin/form/user.html', title='Create user', form=form, User=User, password_length={'min': TRACKER_PASSWORD_LENGTH_MIN, 'max': TRACKER_PASSWORD_LENGTH_MAX}) password = random_string() if not form.password.data else form.password.data salt = random_string() user = db.create(User, name=form.username.data, email=form.email.data, salt=salt, password=hash_password(password, salt), role=UserRole.fromstring(form.role.data), active=form.active.data) db.session.commit() flash('Created user {} with password {}'.format(user.name, password)) return redirect('/user')
def edit_own_user_profile(): form = UserPasswordForm() if not form.validate_on_submit(): return render_template('form/profile.html', title='Edit profile', form=form, password_length={ 'min': TRACKER_PASSWORD_LENGTH_MIN, 'max': TRACKER_PASSWORD_LENGTH_MAX }) user = current_user user.salt = random_string() user.password = hash_password(form.password.data, user.salt) db.session.commit() flash('Profile saved') return redirect('/')
def test_edit_user(db, client): new_password = random_string() new_email = '{}foo'.format(EMAIL) new_role = UserRole.security_team resp = client.post(url_for('edit_user', username=USERNAME), follow_redirects=True, data=dict(username=USERNAME, email=new_email, password=new_password, role=new_role.name, active=True)) assert resp.status_code == 200 resp = client.post(url_for('logout'), follow_redirects=True) assert_not_logged_in(resp) resp = client.post(url_for('login'), follow_redirects=True, data={'username': USERNAME, 'password': new_password}) assert_logged_in(resp) assert USERNAME == current_user.name assert new_email == current_user.email assert new_role == current_user.role
from .base import BaseForm from config import TRACKER_PASSWORD_LENGTH_MIN, TRACKER_PASSWORD_LENGTH_MAX from app.model.user import User from app.user import hash_password, random_string from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired, Length from hmac import compare_digest ERROR_INVALID_USERNAME_PASSWORD = '******' ERROR_ACCOUNT_DISABLED = 'Account is disabled.' dummy_password = hash_password(random_string(), random_string()) class LoginForm(BaseForm): username = StringField( u'Username', validators=[DataRequired(), Length(max=User.NAME_LENGTH)]) password = PasswordField(u'Password', validators=[ DataRequired(), Length(min=TRACKER_PASSWORD_LENGTH_MIN, max=TRACKER_PASSWORD_LENGTH_MAX) ]) login = SubmitField(u'login') def validate(self): self.user = None rv = BaseForm.validate(self) if not rv: return False
from werkzeug.exceptions import Unauthorized, Forbidden from flask import url_for from flask_login import current_user from .conftest import logged_in, assert_logged_in, assert_not_logged_in, create_user, DEFAULT_USERNAME from app.user import random_string from app.form.login import ERROR_ACCOUNT_DISABLED from app.form.admin import ERROR_USERNAME_EXISTS, ERROR_EMAIL_EXISTS from app.model.enum import UserRole USERNAME = '******' PASSWORD = random_string() EMAIL = '{}@cyber.cyber'.format(USERNAME) @create_user(username=USERNAME, password=PASSWORD, role=UserRole.administrator) @logged_in def test_delete_user(db, client): resp = client.post(url_for('delete_user', username=USERNAME), follow_redirects=True, data=dict(confirm='confirm')) assert resp.status_code == 200 resp = client.post(url_for('logout'), follow_redirects=True) assert_not_logged_in(resp) resp = client.post(url_for('login'), follow_redirects=True, data=dict(username=USERNAME, password=PASSWORD)) assert_not_logged_in(resp, status_code=Unauthorized.code)