Browse Source

basic login and csrf working

pull/1/head
wes 8 years ago
parent
commit
bba7d358e4
  1. 1
      .gitignore
  2. 17
      src/admin.py
  3. 2
      src/scripts/editor.js
  4. 10
      src/scripts/editor.tag
  5. 6
      src/scripts/post.tag
  6. 19
      src/templates/login.html
  7. 3
      src/templates/write.html
  8. 29
      src/website.py

1
.gitignore

@ -10,3 +10,4 @@ node_modules
deploy.sh deploy.sh
tags tags
build build
.sass_cache

17
src/admin.py

@ -0,0 +1,17 @@
#! /usr/bin/python3
class Admin:
def __init__(self):
return
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return "admin"

2
src/scripts/editor.js

@ -1,4 +1,4 @@
import riot from 'riot'; import riot from 'riot';
import './editor.tag'; import './editor.tag';
axios.defaults.withCredentials = true
riot.mount("editor"); riot.mount("editor");

10
src/scripts/editor.tag

@ -2,8 +2,8 @@
<div class="centered container"> <div class="centered container">
<div class="columns"> <div class="columns">
<div class="column col-6"> <div class="column col-6">
<input ref="title"></input> <span>title</span><input ref="title">
<input ref="author"></input> <span>author</span><input ref="author"></input>
<textarea onfocus={clearplaceholder} <textarea onfocus={clearplaceholder}
onblur={checkplaceholder} onblur={checkplaceholder}
oninput={echo} oninput={echo}
@ -75,12 +75,14 @@ submit() {
var post = self.querystring.stringify({ var post = self.querystring.stringify({
"title" : this.refs.title.value, "title" : this.refs.title.value,
"author" : this.refs.author.value, "author" : this.refs.author.value,
"content" : this.refs.textarea.value "content" : this.refs.textarea.value,
"csrf_token" : this.opts.csrf_token
}); });
var headers = { var headers = {
"headers" : { "headers" : {
"Content-Type" : "application/x-www-form-urlencoded" "Content-Type" : "application/x-www-form-urlencoded",
"X-CSRFToken" : this.opts.csrf_token
} }
}; };

6
src/scripts/post.tag

@ -108,9 +108,9 @@ setPost(pid, transition) {
return; return;
} }
self.opts.state.pid = pid; self.opts.state.pid = pid;
self.author = postcontent[0].doc.author[0]; self.author = postcontent[0].doc.author;
self.content = postcontent[0].doc.content[0]; self.content = postcontent[0].doc.content;
self.title = postcontent[0].doc.title[0]; self.title = postcontent[0].doc.title;
self.transition = transition; self.transition = transition;
self.swipe = !self.swipe; self.swipe = !self.swipe;
self.nomore = false; self.nomore = false;

19
src/templates/login.html

@ -0,0 +1,19 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<link rel="stylesheet" href="/styles/primop.me.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<html>
<body>
<section class="text-center nav navbar centered page-top navbar-section">
<h1 class="blog-title">logged in status: {{ success }}</h1>
</section>
<footer class="footer">
</footer>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript" src="/scripts/riotblog.min.js"></script>
</body>
</html>

3
src/templates/write.html

@ -11,8 +11,7 @@
<body> <body>
{% block content %} {% block content %}
<editor> <editor csrf_token="{{ csrf_token() }}"></editor>
</editor>
{% endblock %} {% endblock %}

29
src/website.py

@ -4,13 +4,15 @@ from functools import partial
from flask import abort, Flask, render_template, flash, request, send_from_directory, jsonify from flask import abort, Flask, render_template, flash, request, send_from_directory, jsonify
from werkzeug.local import Local, LocalProxy, LocalManager from werkzeug.local import Local, LocalProxy, LocalManager
from flask_appconfig import AppConfig from flask_appconfig import AppConfig
from flask_login import LoginManager, login_required from flask_login import LoginManager, login_required, login_user
from flask_wtf.csrf import CSRFProtect from flask_wtf.csrf import CSRFProtect
from urllib.parse import unquote from urllib.parse import unquote
from urllib.parse import quote, unquote from urllib.parse import quote, unquote
from json import dumps, loads from json import dumps, loads
from admin import Admin
from werkzeug.contrib.cache import MemcachedCache from werkzeug.contrib.cache import MemcachedCache
cache = MemcachedCache(['127.0.0.1:11211']) cache = MemcachedCache(['127.0.0.1:11211'])
@ -45,6 +47,22 @@ def NeverWhere(configfile=None):
#return send_from_directory("/srv/http/goal/favicon.ico", #return send_from_directory("/srv/http/goal/favicon.ico",
#'favicon.ico', mimetype='image/vnd.microsoft.icon') #'favicon.ico', mimetype='image/vnd.microsoft.icon')
@login_manager.user_loader
def load_user(user_id):
return Admin
@app.route("/blog/admin_login", methods=("GET", "POST"))
def admin_login():
password = request.args.get("password")
success = False
if password == app.config["ADMIN_PASSWORD"]:
print("logged in successfully")
success = True
login_user(Admin())
else:
print("did not log in successfully")
return render_template("login.html", success=success)
@app.route("/blog/projects", methods=("GET",)) @app.route("/blog/projects", methods=("GET",))
def projects(): def projects():
return jsonify(cacheit("projects", getProjects)) return jsonify(cacheit("projects", getProjects))
@ -91,7 +109,14 @@ def NeverWhere(configfile=None):
""" """
Insert a post, requires auth Insert a post, requires auth
""" """
return posts.savepost(**request.form)
author = request.form.get("author", "no author")
title = request.form.get("title", "no title")
content = request.form.get("content", "no content")
post = {"author" : author, "title" : title, "content" : content}
return posts.savepost(**post)
# default, not found error # default, not found error

Loading…
Cancel
Save