Skip to content

Instantly share code, notes, and snippets.

View bchaber's full-sized avatar
😶
Don't shake the table

Bartek Chaber bchaber

😶
Don't shake the table
  • Warsaw University of Technology
  • Poland
View GitHub Profile
@bchaber
bchaber / app.py
Created November 4, 2021 09:31
A not-so-simple web service that tries to illustrate HATEOAS (using handcrafted json+hal) and stateless authorization (using JWT)
# To assure reproducability, the given Redis database (db=0)
# !!! is CLEARED !!! each time the application starts
#
# $ export JWT_SECRET=something
# $ python3 -m pip install flask pyjwt redis
# $ export REDIS_PASSWORD=verycomplex
# $ export REDIS_PORT=6379
# $ export REDIS_HOST=myhost.com
from os import getenv
from flask import Flask, g
@bchaber
bchaber / tokens.py
Created November 4, 2021 08:15
Script for token generation
# $ export JWT_SECRET=something
# $ python3 -m pip install pyjwt
import jwt, os
import datetime
SECRET = os.getenv("JWT_SECRET")
NOW = datetime.datetime.now(tz=datetime.timezone.utc)
def user_token(uid, role, seconds=60):
dt = datetime.timedelta(seconds=seconds)
@bchaber
bchaber / app.py
Created October 21, 2021 10:14
A simple web application with a server-side application state (session ID)
from flask import Flask
from flask import request
from flask import make_response
from json import loads, dumps
import random, string
app = Flask(__name__)
carts = { # server-side application state
'deadbeef' : {'Marchewka' : 4}
@bchaber
bchaber / app.py
Created October 19, 2021 09:56
A simple web application with application state on the client side (in a cookie)
from flask import Flask
from flask import request
from flask import make_response
from json import loads, dumps
app = Flask(__name__)
@app.route("/add2cart", methods=["POST"])
def add2cart():
cart = {} # default value, can be loaded from cookies
cookies = request.headers.get("Cookie", "")