Browse Source
* json will do basic authentication -- not sophisticated, but works * regular request will redirect to login page * csrf token * /movies/<option> instead of <lang>master
sipp11
10 years ago
7 changed files with 186 additions and 22 deletions
@ -0,0 +1,54 @@
|
||||
from functools import wraps |
||||
from flask import ( |
||||
request, Response, session, flash, redirect, url_for, abort |
||||
) |
||||
from settings import app_password, app_user |
||||
import random |
||||
import string |
||||
|
||||
|
||||
def csrf_token_generator(size=40, chars=string.ascii_uppercase + string.digits): |
||||
return ''.join(random.choice(chars) for _ in range(size)) |
||||
|
||||
|
||||
def check_basic_auth(user, passwd): |
||||
if user != app_user or passwd != app_password: |
||||
return False |
||||
else: |
||||
return True |
||||
|
||||
|
||||
def authenticate(): |
||||
"""Sends a 401 response that enables basic auth""" |
||||
return Response( |
||||
'Could not verify your access level for that URL.\n' |
||||
'You have to login with proper credentials', 401, |
||||
{'WWW-Authenticate': 'Basic realm="Login Required"'} |
||||
) |
||||
|
||||
|
||||
def requires_auth(f): |
||||
''' |
||||
REQUEST.json |
||||
only gets basic authentication |
||||
REQUEST.get |
||||
redirect to login page |
||||
''' |
||||
@wraps(f) |
||||
def decorated(*args, **kwargs): |
||||
if request.json: |
||||
auth = request.headers.get('Authorization') |
||||
if auth.startswith('Basic'): |
||||
basic_auth = request.authorization |
||||
if not check_basic_auth(basic_auth.username, basic_auth.password): |
||||
abort(401) |
||||
else: |
||||
abort(401) |
||||
return f(*args, **kwargs) |
||||
|
||||
auth = session.get('logged_in') |
||||
if not auth: |
||||
flash('You are not authorized') |
||||
return redirect(url_for('hello_world')) |
||||
return f(*args, **kwargs) |
||||
return decorated |
@ -1,9 +0,0 @@
|
||||
<html> |
||||
<head> |
||||
<title>Tornado Boilerplate</title> |
||||
</head> |
||||
|
||||
<body> |
||||
<h1>It worked!</h1> |
||||
</body> |
||||
</html> |
@ -0,0 +1,17 @@
|
||||
<!doctype html> |
||||
<title>Flaskr</title> |
||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}"> |
||||
<div class="page"> |
||||
<h1>Flaskr</h1> |
||||
<div class="metanav"> |
||||
{% if not session.logged_in %} |
||||
<a href="{{ url_for('login') }}">log in</a> |
||||
{% else %} |
||||
<a href="{{ url_for('logout') }}">log out</a> |
||||
{% endif %} |
||||
</div> |
||||
{% for message in get_flashed_messages() %} |
||||
<div class="flash">{{ message }}</div> |
||||
{% endfor %} |
||||
{% block body %}{% endblock %} |
||||
</div> |
@ -0,0 +1,15 @@
|
||||
{% extends "layout.html" %} |
||||
{% block body %} |
||||
<h2>Login</h2> |
||||
{% if error %}<p class="error"><strong>Error:</strong> {{ error }}{% endif %} |
||||
<form action="{{ url_for('login') }}" method="post"> |
||||
<dl> |
||||
<dt>Username: |
||||
<dd><input type="text" name="username"> |
||||
<dt>Password: |
||||
<dd><input type="password" name="password"> |
||||
<input name="_csrf_token" type="hidden" value="{{ csrf_token() }}"> |
||||
<dd><input type="submit" value="Login"> |
||||
</dl> |
||||
</form> |
||||
{% endblock %} |
Loading…
Reference in new issue