Skip to main content

An encrypted cookie based session implementation for flask

Project description

pipeline status coverage report black badge pre-commit

Description

A cookie based session for flask relying on Fernet encrypted cookies.

Motivation

flask default session rely on signed cookies. This sometimes is not enough, and encrypted data should be used.

Example : Use cookies to store OAuth2 access tokens without the burden of server side storage.

Usage

Installation

pip install flask-encrypted-cookies-session

Flask application configuration

Private key used to encrypt cookies can be generated with python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key())"

# -*- coding: utf-8 -*-
from flask import Flask, session

from flask_encrypted_cookies_session import EncryptedCookieSession

DEBUG = "True"
ENCRYPTED_COOKIES_SECRET_KEY = (
    "JNJQuYdaUGr8XBSoZNYF9FC-A7RZ7iFqV_KqrCwYr0s="  # Fernet.generate_key()
)
# To rotate your keys:
# ENCRYPTED_COOKIES_SECRET_KEY = "JNJQuYdaUGr8XBSoZNYF9FC-A7RZ7iFqV_KqrCwYr0s=,Dfo2hCeG-S6CeY-_tgJ33gip9rxC2t8qNK0CM0gZlRk="  # [Fernet.generate_key(), Fernet.generate_key()]

app = Flask(__name__)
app.config.from_object(__name__)

# This will replace the default Flask application session interface with the encrypted
# cookie based session
EncryptedCookieSession(app)


@app.route("/set/")
def session_set():
    session["key"] = "value"
    return "ok"


@app.route("/get/")
def session_get():
    return session.get("key", "not set")

Development

poetry is used to manage this project.

poe the poet is used as the task runner of this project. If you don't know what a task runner is, think about an alternative version of a Makefile.

Install project dependencies

$ poetry install

Unit testing

Test with all python versions

$ poe test

Test with a specific python version

$ poe test-py39

F.A.Q

Where can I open an Issue or a Pull Request to contribute ?

The github repository should be used for Issues or contributions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_encrypted_cookies_session-0.2.4.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file flask_encrypted_cookies_session-0.2.4.tar.gz.

File metadata

File hashes

Hashes for flask_encrypted_cookies_session-0.2.4.tar.gz
Algorithm Hash digest
SHA256 72ddeacb7b63f885be7072a112a890eb48c2e1191d96e36040344df4798d3816
MD5 c8a5244318cc06b981bcfcfca797da1c
BLAKE2b-256 5366f528a61353a80012e24ebdfe17e49242c9441c729bad442c8cc19bcec0dc

See more details on using hashes here.

File details

Details for the file flask_encrypted_cookies_session-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_encrypted_cookies_session-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 296e78414daad709ff7f6cbc1abd9e6c0e18f1fbd88d4195327cced8fec213b3
MD5 38cb62329b84a1a65715b207e7071293
BLAKE2b-256 546feb175c58f05448c21809fdfb660befd8f481ecaf4f90497fe351c32706f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page