예제 #1
0
파일: models.py 프로젝트: mulnz/flask-blog
def create_db_default_user():
    # Create the user, be sure to create a new user and remove this one immediately
    name = app.config.get('DEFAULT_NAME', 'admin')
    password = app.config.get('DEFAULT_PASSWORD', 'admin')
    password_hash = bcrypt.generate_password_hash(password)
    default_user = User(name=name, shortname=name, password_hash=password_hash)

    # Create the db and then add the user in
    db.create_all()
    db.session.add(default_user)
    db.session.commit()
예제 #2
0
    def on_model_change(self, form, model, is_created):
        """
        Convert markdown on updates and create hash on creation

        :param form: The form from the creation page
        :param model: The model that is to be created or updated
        :param is_created: Boolean showing if the model is eing created or updated
        """
        if form.data['convert']:
            model.about_html = markdown(model.about_md, output_format='html5')

        if is_created:
            model.password_hash = bcrypt.generate_password_hash(model.password_hash)
예제 #3
0
def register():

    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = RegisterForm()

    if form.validate_on_submit():
        hashed_pass = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pass)
        db.session.add(user)
        db.session.commit()
        flash(f'Your account has been created', 'success')
        return redirect(url_for('login'))

    return render_template('register.html', title='Register', form=form)
예제 #4
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))
    user = User.verify_reset_token(token)
    if user is None:
        flash("Il token non è valido o potrebbe essere scaduto", "warning")
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_pw
        db.session.commit()
        flash(f'Password modificata, prova ad accedere', 'success')
        return redirect(url_for('users.login'))

    return render_template('reset_token.html',
                           title="Reset Password",
                           form=form)
예제 #5
0
def resetpassword(token):
    if current_user.is_authenticated:
        return redirect(url_for("main.home"))
    user = User.verify_reset_token(token)
    if user is None:
        flash("That is an invalid or expired link", "warning")
        return redirect(url_for("users.request_reset"))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data)
        user.password = hashed_password
        db.session.commit()
        flash(
            "Your password has been changed,you can now login with your new password",
            "success")
        return redirect(url_for("users.login"))
    return render_template("reset_password.html",
                           form=form,
                           legend="Reset Password")
예제 #6
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an valid or expired token', 'warning')
        return redirect(url_for(users.reset_request))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_password
        user.save()
        flash(f'Your password have been updated! Then, you can login Now',
              'success')
        return redirect(url_for('users.login'))
    return render_template('reset_token.html',
                           form=form,
                           title='Reset Password Page')
예제 #7
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_pw
        db.session.commit()
        flash('Your password has been updated please log in!,success')
        return redirect(url_for('users.login'))
    return render_template('reset_token.html',
                           form=form,
                           title='reset password',
                           legend='Reset Password')
예제 #8
0
def register():
    title = '新規登録'
    if request.method == 'POST':
        if request.form['name'] == '':
            flash('名前を入力してください')
        if request.form['password'] == '':
            flash('パスワードを入力してください')
        if not request.form['name'] == '' and not request.form['password'] == '':
            account = Account(
                name=request.form['name'],
                hashed_password=bcrypt.generate_password_hash(request.form['password'])
            )
            db.session.add(account)
            db.session.commit()
            session['logged_in'] = True
            session['name'] = request.form['name']
            flash('ユーザー名:' + session['name'] + 'で登録しました')
            return redirect(url_for('index'))
    return render_template('register.html', title=title)
예제 #9
0
def resetToken(token):
    if current_user.is_authenticated:
        return redirect(url_for("main.home"))
    user = User.verify_reset_token(token)
    if user is None:
        flash(
            "Invalid token received. The token is either invalid or expired.",
            "warning")
        return redirect(url_for("users.resetRequest"))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashedPassword = bcrypt.generate_password_hash(
            form.password.data).decode("utf-8")
        user.password = hashedPassword
        db.session.commit()
        flash("Your password has successfully been updated!", "success")
        return redirect(url_for("users.login"))
    return render_template("resetToken.html",
                           title="Reset Password",
                           form=form)
예제 #10
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('home.homepage'))
    user = UserModel.verify_reset_token(token)
    if user is None:
        flash('That is an invalid token or expired token.', 'warning')
        return redirect(url_for('auth.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        pwdhash = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = pwdhash
        # save user to the database
        db.session.commit()

        flash("Your password has been updated", 'success')
        return redirect(url_for('auth.login_page'))
    return render_template('auth/reset_token.html',
                           title='Reset Password',
                           form=form)
예제 #11
0
def register_page():
    if current_user.is_authenticated:
        return redirect(url_for('home.homepage'))

    form = RegistrationForm()
    if form.validate_on_submit():
        pwdhash = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = UserModel(
            username=form.username.data,
            email=form.email.data,
            password=pwdhash,
        )

        # save user to the database
        user.save_to_db()

        flash(f"Account created for {form.username.data}", 'success')
        return redirect(url_for('auth.login_page'))
    return render_template('auth/register.html', title="Sign Up", form=form)
예제 #12
0
def register():
    # if user is authenticated redirect to home
    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        # decode('utf-8') - to make it string not byte
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        # add and commit user
        db.session.add(user)
        db.session.commit()
        # flash for sending alert
        flash('Your account has been created! You are now able to login',
              'success')
        return redirect(url_for('login'))
    return render_template("register.html", title="Register", form=form)
예제 #13
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    user = User.verify_reset_token(token)

    if user is None:
        flash('Invalid token or expired token', 'warning')
        return redirect(url_for('reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_pass = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_pass
        db.session.commit()
        flash('Password has been updated! You are now able to log in',
              'success')

        return redirect(url_for('login'))
    return render_template("reset_token.html",
                           title="Reset Password",
                           form=form)
예제 #14
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    # token verfication method from User model
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        # hash new password and commit changes
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        flash('Password has been updated! You are now able to log in',
              'success')
        return redirect(url_for('users.login'))
    return render_template('users/reset_token.html',
                           title='Reset Password',
                           form=form)
예제 #15
0
def profile():
    form = UpdateAccountForm()
    if form.validate_on_submit():
        form_picture = form.picture.data
        if form_picture:
            picture_file = save_profile_picture(form_picture)
            if picture_file:
                current_user.profile_image = picture_file
            # else:  USING FORM VALIDATE ON SUBMIT, so else with flash is not required
            #     flash("Unsupported file type. Please upload .JPG or .PNG")
            #     return redirect(url_for('users.profile'))
        if form.password.data and form.confirm_password.data:
            if form.password.data == form.confirm_password.data:
                hashed_pw = bcrypt.generate_password_hash(form.password.data)
                current_user.password = hashed_pw
            else:
                flash("Passwords don't match", 'error')
                return redirect(url_for('users.profile'))
        current_user.username = form.username.data  #current user is derived from usermixin class in models, from loginmanager
        current_user.email = form.email.data

        db.session.commit()
        flash('Your account has been updated!', 'success')
        return redirect(url_for('users.profile'))
    elif request.method == 'GET':  #adds the current users email and username on the form field
        form.username.data = current_user.username
        form.email.data = current_user.email
    profile_image = url_for('static',
                            filename='profile_pics/' +
                            current_user.profile_image)
    page = request.args.get(
        'page', 1, type=int)  #get page query '?page=xx' from the url bar
    user = User.query.filter_by(username=current_user.username).first()
    posts = Post.query.filter_by(author=user).order_by(
        Post.date_posted.desc()).paginate(per_page=15, page=page)
    return render_template('profile.html',
                           title='Account',
                           profile_image=profile_image,
                           form=form,
                           posts=posts)
예제 #16
0
def register():
    #Since has successfully registered then they redirected to the home page i.e.,they can't register again
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        #generate hash password
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        #Instnce of User() with the info
        user = User(username=form.username.data,
                    email=form.email.data, password=hashed_password)
        #Adding the user
        db.session.add(user)
        #Commiting to the database
        db.session.commit()
        #message
        flash(
            f'Account created for {form.username.data}! You are now able to log in', 'success')
        #redirects us to login to the page after registering successfully
        return redirect(url_for('login'))
    
    return render_template('register.html', form=form)
예제 #17
0
def register():
    if current_user.is_authenticated:  # .attribute comes from UserMixin class
        return redirect(url_for(
            'main.home'))  # so as logged user doesn't see those register&login
    form = RegistrationForm()
    if form.validate_on_submit():  # this func() triggers validate_funcs()
        hashed_pass = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        new_user = User(username=form.username.data,
                        email=form.email.data,
                        password=hashed_pass,
                        country=form.country.data)
        db.session.add(new_user)
        db.session.commit()

        send_confirm_email(new_user)

        flash(
            f'Account for {form.username.data} \
				has been created. Verify your account by the link sent to the email',
            'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Registration', form=form)
예제 #18
0
def register():
    if current_user.is_authenticated:
        redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():

        pass_encrypt = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        username = form.username.data

        user = User(
            username=username,
            email=form.email.data,
            password=pass_encrypt,
        )
        if form.picture.data:
            img_file = save_picture(form.picture.data)
            user.image_file = img_file
        db.session.add(user)
        db.session.commit()
        flash(f'Your account has been created!', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
예제 #19
0
def register():
	if current_user.is_authenticated:
		return redirect(url_for('home'))

	form=Registrationform()
	if form.validate_on_submit():
		hash_pass=bcrypt.generate_password_hash(form.password.data).decode('utf-8')
		check=User.query.filter_by(username=form.username.data).first()
		check1=User.query.filter_by(email=form.email.data).first()
		if(check==None and check1==None):
			user=User(username=form.username.data,email=form.email.data,password=hash_pass)
			db.session.add(user)
			db.session.commit()
			flash(f'Account created for ' + form.username.data ,'success')
			return redirect(url_for('login'))
		else:
			if(check1!=None):
				flash(f'Account already existed for ' + form.email.data ,'danger')
			if(check!=None):
				flash(f'Account already existed for ' + form.username.data ,'danger')

			return redirect(url_for('register'))

	return render_template('register.html',name='Register',form=form)
예제 #20
0
 def create_user(self, name, email, password):
     new_user = User(name=name,
                     email=email,
                     password=bcrypt.generate_password_hash(password))
     db.session.add(new_user)
     db.session.commit()
예제 #21
0
 def password(self, password):
     self.password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
예제 #22
0
파일: views.py 프로젝트: mulnz/flask-blog
    def on_model_change(self, form, model, is_created):
        if model.about_html:
            model.about_html = markdown(model.about_md, output_format='html5')

        if is_created:
            model.password_hash = bcrypt.generate_password_hash(model.password_hash)
예제 #23
0
파일: auth.py 프로젝트: th0mas/personal
 def create_password_hash(self, password):
     """Creates a secure password hash using bcrypt"""
     return bcrypt.generate_password_hash(password)
예제 #24
0
 def hash_password(self, password):
     self.password = bcrypt.generate_password_hash(password)
예제 #25
0
 def verify_password(self, password):
     password_hash = bcrypt.generate_password_hash(password)
     return bcrypt.check_password_hash(password_hash, self.password)
예제 #26
0
 def make_password(plaintext):
     return bcrypt.generate_password_hash(plaintext)
예제 #27
0
 def hash_password(password):
     return bcrypt.generate_password_hash(password)
예제 #28
0
 def password(self, plain_text):
     self.password_hash = bcrypt.generate_password_hash(plain_text).decode(
         'utf-8')