Created
July 13, 2012 06:15
-
-
Save spantaleev/3103069 to your computer and use it in GitHub Desktop.
Simple login system with Flask-Sijax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.pyc | |
env | |
static/js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from werkzeug.datastructures import CallbackDict | |
from flask.sessions import SessionInterface, SessionMixin | |
from itsdangerous import URLSafeTimedSerializer, BadSignature | |
class ItsdangerousSession(CallbackDict, SessionMixin): | |
def __init__(self, initial=None): | |
def on_update(self): | |
self.modified = True | |
CallbackDict.__init__(self, initial, on_update) | |
self.modified = False | |
class ItsdangerousSessionInterface(SessionInterface): | |
salt = 'cookie-session' | |
session_class = ItsdangerousSession | |
def get_serializer(self, app): | |
if not app.secret_key: | |
return None | |
return URLSafeTimedSerializer(app.secret_key, | |
salt=self.salt) | |
def open_session(self, app, request): | |
s = self.get_serializer(app) | |
if s is None: | |
return None | |
val = request.cookies.get(app.session_cookie_name) | |
if not val: | |
return self.session_class() | |
max_age = app.permanent_session_lifetime.total_seconds() | |
try: | |
data = s.loads(val, max_age=max_age) | |
return self.session_class(data) | |
except BadSignature: | |
return self.session_class() | |
def save_session(self, app, session, response): | |
domain = self.get_cookie_domain(app) | |
if not session: | |
if session.modified: | |
response.delete_cookie(app.session_cookie_name, | |
domain=domain) | |
return | |
expires = self.get_expiration_time(app, session) | |
val = self.get_serializer(app).dumps(dict(session)) | |
response.set_cookie(app.session_cookie_name, val, | |
expires=expires, httponly=True, | |
domain=domain) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" | |
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script type="text/javascript" src="/static/js/sijax/sijax.js"></script> | |
<script type="text/javascript"> | |
{{ g.sijax.get_js()|safe }} | |
</script> | |
</head> | |
<body> | |
{% if user_id is not none %} | |
You are currently logged in as {{ user_id }} | |
<a href="javascript://" onclick="Sijax.request('logout');"> | |
Logout | |
</a> | |
{% else %} | |
<a href="javascript://" onclick="Sijax.request('login');"> | |
Login | |
</a> | |
{% endif %} | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flask-sijax | |
itsdangerous |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os, sys | |
path = os.path.join('.', os.path.dirname(__file__), '../') | |
sys.path.append(path) | |
from flask import Flask, g, render_template, session, url_for | |
from itis import ItsdangerousSessionInterface | |
import flask_sijax | |
app = Flask(__name__) | |
app.secret_key = 'very secret key here' | |
app.config["SIJAX_STATIC_PATH"] = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/') | |
app.config["SIJAX_JSON_URI"] = '/static/js/sijax/json2.js' | |
flask_sijax.Sijax(app) | |
app.session_interface = ItsdangerousSessionInterface() | |
@flask_sijax.route(app, "/") | |
def home(): | |
def login(obj_response): | |
session['user_id'] = 'some_username' | |
obj_response.redirect(url_for('home')) | |
def logout(obj_response): | |
del session['user_id'] | |
obj_response.redirect(url_for('home')) | |
if g.sijax.is_sijax_request: | |
g.sijax.register_callback('login', login) | |
g.sijax.register_callback('logout', logout) | |
return g.sijax.process_request() | |
return render_template('login.html', user_id=session.get('user_id', None)) | |
if __name__ == '__main__': | |
app.run(debug=True, port=5555) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment