Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MathJax, registration support, flask-bcrypt & updated view permissions #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ content/
.eggs
*.egg-info
dist/
.vscode/settings.json
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
'Markdown>=2.2.0',
'Pygments>=1.5',
'WTForms>=1.0.2',
'Werkzeug>=0.8.3'
'Werkzeug>=0.8.3',
'python-markdown-math',
'flask-bcrypt'

],
setup_requires=['pytest-runner'],
tests_require=['pytest', 'mock'],
Expand Down
3 changes: 2 additions & 1 deletion wiki/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def __init__(self, text):
'codehilite',
'fenced_code',
'meta',
'tables'
'tables',
'mdx_math' # Add mathjax support
])
self.input = text
self.markdown = None
Expand Down
13 changes: 13 additions & 0 deletions wiki/web/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from wtforms import PasswordField
from wtforms.validators import InputRequired
from wtforms.validators import ValidationError
from wtforms.validators import EqualTo

from wiki.core import clean_url
from wiki.web import current_wiki
Expand All @@ -31,6 +32,7 @@ class SearchForm(FlaskForm):
ignore_case = BooleanField(
description='Ignore Case',
# FIXME: default is not correctly populated
# A: code is fine, firefox have a bug and will not render checkbox properly
default=True)


Expand All @@ -55,3 +57,14 @@ def validate_password(form, field):
return
if not user.check_password(field.data):
raise ValidationError('Username and password do not match.')

class RegistrationForm(FlaskForm):
name = TextField('Username', [InputRequired()])
password = PasswordField('Password', [InputRequired()])
password_confirm = PasswordField('Confirm Password',
validators=[InputRequired(), EqualTo('password')])

def validate_name(form, field):
user = current_users.get_user(form.name.data)
if user:
raise ValidationError('This username already exist.')
20 changes: 18 additions & 2 deletions wiki/web/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flask import render_template
from flask import request
from flask import url_for
from flask import current_app
from flask_login import current_user
from flask_login import login_required
from flask_login import login_user
Expand All @@ -18,9 +19,11 @@
from wiki.web.forms import LoginForm
from wiki.web.forms import SearchForm
from wiki.web.forms import URLForm
from wiki.web.forms import RegistrationForm
from wiki.web import current_wiki
from wiki.web import current_users
from wiki.web.user import protect
from wiki.web.user import UserManager


bp = Blueprint('wiki', __name__)
Expand Down Expand Up @@ -50,6 +53,7 @@ def display(url):


@bp.route('/create/', methods=['GET', 'POST'])
@login_required
@protect
def create():
form = URLForm()
Expand All @@ -60,6 +64,7 @@ def create():


@bp.route('/edit/<path:url>/', methods=['GET', 'POST'])
@login_required
@protect
def edit(url):
page = current_wiki.get(url)
Expand All @@ -84,6 +89,7 @@ def preview():


@bp.route('/move/<path:url>/', methods=['GET', 'POST'])
@login_required
@protect
def move(url):
page = current_wiki.get_or_404(url)
Expand All @@ -96,6 +102,7 @@ def move(url):


@bp.route('/delete/<path:url>/')
@login_required
@protect
def delete(url):
page = current_wiki.get_or_404(url)
Expand Down Expand Up @@ -155,9 +162,17 @@ def user_index():
pass


@bp.route('/user/create/')
@bp.route('/user/create/', methods=['GET', 'POST'])
def user_create():
pass
form = RegistrationForm()
if form.validate_on_submit():
user = form.name.data
password = form.password.data
UserManager(current_app.config['CONTENT_DIR']).add_user(name=user, password=password)
# user.set('authenticated', True)
flash('Registration successful.', 'success')
return redirect(request.args.get("next") or url_for('wiki.index'))
return render_template('register.html', form=form)


@bp.route('/user/<int:user_id>/')
Expand All @@ -166,6 +181,7 @@ def user_admin(user_id):


@bp.route('/user/delete/<int:user_id>/')
@login_required
def user_delete(user_id):
pass

Expand Down
22 changes: 19 additions & 3 deletions wiki/web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap.css') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='responsive.css') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='pygments.css') }}">
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
},
svg: {
fontCache: 'global'
}
};
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
</head>

<body>
Expand All @@ -26,10 +39,13 @@
</ul>

<ul class="nav pull-right">
{% if current_user.is_anonymous %}
<li><a href="{{ url_for('wiki.user_login') }}">Login</a></li>
{% else %}
{% if current_user.is_authenticated %}
<li><a href="{{ url_for('wiki.user_logout') }}">Logout</a></li>
{% else %}
<ul class="nav">
<li><a href="{{ url_for('wiki.user_login') }}">Login</a></li>
<li><a href="{{ url_for('wiki.user_create') }}">Register</a></li>
</ul>
{% endif %}
</ul>
</div>
Expand Down
16 changes: 16 additions & 0 deletions wiki/web/templates/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base.html" %}

{% block title %}Register{% endblock title %}

{% block content %}
<form class="form form-inline" method="POST">
{{ form.hidden_tag() }}
{{ form.name.label }} <br>
{{ form.name(placeholder='Username') }} <br>
{{ form.password.label }} <br>
{{ form.password(placeholder='Password') }} <br>
{{ form.password_confirm.label }} <br>
{{ form.password_confirm(placeholder='Confirm Password') }} <br>
<input type="submit" class="btn btn-primary" value="Register">
</form>
{% endblock content %}
2 changes: 1 addition & 1 deletion wiki/web/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<form class="form-inline well" method="POST">
{{ form.hidden_tag() }}
{{ form.term(placeholder='Search for.. (regex accepted)', autocomplete="off") }}
{{ form.ignore_case() }} Ignore Case
{{ form.ignore_case() }} {{ form.ignore_case.label }}
<input type="submit" class="btn btn-success pull-right" value="Search!">
</form>
</div>
Expand Down
18 changes: 5 additions & 13 deletions wiki/web/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
"""
import os
import json
import binascii
import hashlib
from functools import wraps

from flask import current_app
from flask_login import current_user
from flask_bcrypt import Bcrypt



bcrypt = Bcrypt()
class UserManager(object):
"""A very simple user Manager, that saves it's data as json."""
def __init__(self, path):
Expand Down Expand Up @@ -121,22 +120,15 @@ def check_password(self, password):


def get_default_authentication_method():
return current_app.config.get('DEFAULT_AUTHENTICATION_METHOD', 'cleartext')
return current_app.config.get('DEFAULT_AUTHENTICATION_METHOD', 'hash')


def make_salted_hash(password, salt=None):
if not salt:
salt = os.urandom(64)
d = hashlib.sha512()
d.update(salt[:32])
d.update(password)
d.update(salt[32:])
return binascii.hexlify(salt) + d.hexdigest()
return bcrypt.generate_password_hash(password).decode('utf-8')


def check_hashed_password(password, salted_hash):
salt = binascii.unhexlify(salted_hash[:128])
return make_salted_hash(password, salt) == salted_hash
return bcrypt.check_password_hash(salted_hash, password)


def protect(f):
Expand Down