{"id":16545432,"url":"https://github.com/hasansezertasan/starlette-flask","last_synced_at":"2026-01-31T20:03:45.738Z","repository":{"id":206147530,"uuid":"706500877","full_name":"hasansezertasan/starlette-flask","owner":"hasansezertasan","description":"Session Middleware for Starlette/FastAPI Applications based on Flask Session Decoding and Encoding","archived":false,"fork":false,"pushed_at":"2025-08-12T08:19:58.000Z","size":52,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-28T15:40:33.344Z","etag":null,"topics":["cookie","fastapi","flask","middleware","pypi-package","pypi-reserved","session","starlette","starlette-middleware","used-hatch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hasansezertasan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-10-18T04:36:02.000Z","updated_at":"2025-06-28T18:13:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"72c011e5-6f54-4838-9ca7-bf3bdb5ec62a","html_url":"https://github.com/hasansezertasan/starlette-flask","commit_stats":null,"previous_names":["hasansezertasan/starlette-flask"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hasansezertasan/starlette-flask","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasansezertasan%2Fstarlette-flask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasansezertasan%2Fstarlette-flask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasansezertasan%2Fstarlette-flask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasansezertasan%2Fstarlette-flask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hasansezertasan","download_url":"https://codeload.github.com/hasansezertasan/starlette-flask/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hasansezertasan%2Fstarlette-flask/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28952582,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T18:30:42.805Z","status":"ssl_error","status_checked_at":"2026-01-31T18:30:19.593Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cookie","fastapi","flask","middleware","pypi-package","pypi-reserved","session","starlette","starlette-middleware","used-hatch"],"created_at":"2024-10-11T19:06:54.465Z","updated_at":"2026-01-31T20:03:45.729Z","avatar_url":"https://github.com/hasansezertasan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# starlette-flask\n\n\u003e **This project is archived.** It achieved its goal and is no longer actively maintained. The existing PyPI package (v0.0.1) remains installable. See [why this was archived](#why-archived) below.\n\n[![CI](https://github.com/hasansezertasan/starlette-flask/actions/workflows/ci.yml/badge.svg)](https://github.com/hasansezertasan/starlette-flask/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)\n[![Coverage](https://img.shields.io/codecov/c/github/hasansezertasan/starlette-flask)](https://codecov.io/gh/hasansezertasan/starlette-flask)\n[![PyPI - Version](https://img.shields.io/pypi/v/starlette-flask.svg)](https://pypi.org/project/starlette-flask)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/starlette-flask.svg)](https://pypi.org/project/starlette-flask)\n[![License](https://img.shields.io/github/license/hasansezertasan/starlette-flask.svg)](https://github.com/hasansezertasan/starlette-flask/blob/main/LICENSE)\n[![Latest Commit](https://img.shields.io/github/last-commit/hasansezertasan/starlette-flask)](https://github.com/hasansezertasan/starlette-flask)\n\n[![Downloads](https://pepy.tech/badge/starlette-flask)](https://pepy.tech/project/starlette-flask)\n[![Downloads/Month](https://pepy.tech/badge/starlette-flask/month)](https://pepy.tech/project/starlette-flask)\n[![Downloads/Week](https://pepy.tech/badge/starlette-flask/week)](https://pepy.tech/project/starlette-flask)\n\n---\n\nSession Middleware for Starlette/FastAPI Applications based on Flask Session Decoding and Encoding.\n\n## Table of Contents\n\n- [starlette-flask](#starlette-flask)\n  - [Table of Contents](#table-of-contents)\n  - [Installation](#installation)\n  - [Usage](#usage)\n  - [Motivation](#motivation)\n  - [Author](#author)\n  - [Disclaimer](#disclaimer)\n  - [License](#license)\n\n## Installation\n\n```shell\npip install starlette-flask\n```\n\n## Usage\n\nSo what's the problem? Let's say you have a [Flask] application and it was live for a long time. You want to migrate to [FastAPI] but you don't want your users to lose their session data. And to be honest, migrating is not an easy process. You might want to take it slow and get the benefit of FastAPI features like mounting an application to another application.\n\nLet's try to mount a [Flask] application to a [FastAPI] application.\n\n```python\nfrom fastapi import FastAPI, Request, Response\nfrom flask import Flask, jsonify, session, request\nfrom starlette.middleware.sessions import SessionMiddleware\nfrom a2wsgi import WSGIMiddleware\n\nsecret_key = \"super-secret\"\n\n\nflask_app = Flask(__name__)\nflask_app.config[\"SECRET_KEY\"] = secret_key\n\n\n@flask_app.get(\"/\")\ndef flask_index():\n    return jsonify({\"message\": \"Hello World from Flask Application\"})\n\n\n@flask_app.get(\"/set-session\")\ndef flask_set_session():\n    session[\"application\"] = \"flask\"\n    session.modified = True\n    return jsonify({\"message\": \"Session set\"})\n\n\n@flask_app.get(\"/get-session\")\ndef flask_get_session():\n    return jsonify({\"message\": session.get(\"application\", None)})\n\n\n@flask_app.get(\"/delete-session\")\ndef flask_delete_session():\n    session.pop(\"application\")\n    session.modified = True\n    return jsonify({\"message\": \"Session deleted\"})\n\n\n@flask_app.before_request\ndef before_request():\n    print(session.items())\n\n@flask_app.after_request\ndef after_request(response):\n    print(session.items())\n    return response\n\n\nfastapi_application = FastAPI()\nfastapi_application.add_middleware(\n    SessionMiddleware,\n    secret_key=secret_key,\n)\n\n\n@fastapi_application.middleware(\"http\")\nasync def starlette_add_process_time_header(request: Request, call_next):\n    response = await call_next(request)\n    response.headers[\"X-Process-Time\"] = \"100\"\n    print(response.headers)\n    return response\n\n\n@fastapi_application.get(\"/\")\nasync def starlette_index(req: Request):\n    return {\"message\": \"Hello World from FastAPI Application\"}\n\n\n@fastapi_application.get(\"/set-session\")\nasync def starlette_set_session(req: Request):\n    req.session.update({\"application\": \"fastapi\"})\n    return {\"message\": \"Session set\"}\n\n\n@fastapi_application.get(\"/get-session\")\nasync def starlette_get_session(req: Request):\n    return {\"message\": req.session.get(\"application\", None)}\n\n\n@fastapi_application.get(\"/delete-session\")\nasync def starlette_delete_session(req: Request):\n    req.session.pop(\"application\")\n    return {\"message\": \"Session deleted\"}\n\n\napp = FastAPI()\napp.mount(\"/flask-application\", WSGIMiddleware(flask_app))\napp.mount(\"/fastapi-application\", fastapi_application)\n\n```\n\nThe problem here is this: If you set a session in [Flask] application, you can't get it from [FastAPI] application, and vice versa. At the same time, beyond accessing the session data, these two applications overwrite each other's session data. That's because they use different methods to sign the session data.\n\nSince they use different methods to sign the session data, they can't decode each other's session data. What can we do? We can use `starlette-flask` to solve this problem.\n\nAll you need to do is this:\n\n```diff\n- from starlette.middleware.sessions import SessionMiddleware\n+ from starlette_flask.middleware.sessions import SessionMiddleware\n```\n\n## Motivation\n\nI was migrating from [Flask] to [FastAPI] and I found out that I could use my existing [Flask] applications with [FastAPI] (thanks to [a2wsgi]) application.\n\n\u003e I must tell you this: Many of my [Flask] applications depend on third-party [Flask] extensions like [Flask Admin], [Flask Login], and [Flask-JWT-Extended]\n\nSo I searched how to couple [Flask] and [FastAPI] and [found a way](https://fastapi.tiangolo.com/advanced/wsgi/#using-wsgimiddleware). But there was a problem... I wasn't able to access the session data between [Flask] and [FastAPI] applications. I mean I was able to CRUD session data on the [Flask] side and [FastAPI] side but I couldn't CRUD the session data inside [FastAPI] that was CRUDed by [Flask], so I started this [discussion](https://github.com/tiangolo/fastapi/discussions/9318) in the [FastAPI] repository. Back then I wasn't able to solve this, so I decided not to use [Flask Login] and [Flask Admin] anymore...\n\nBut you can see that the discussion didn't get any answers from March to September. It was bothering me, so I decided to solve it myself. I took a look at the source code of [Flask] and [Starlette] (backend core of [FastAPI]). I found that they used different methods to sign the session data and [Starlette] kept re-signing the session data even if it wasn't created, updated, deleted, or even read, that was the problem... I needed a custom `SessionMiddleware` that uses the same method as [Flask] to sign the session data and I did implement it.\n\nHere are some related discussions/issues/pull requests:\n\n- [Sharing Session Data between Mounted Applications · tiangolo/fastapi · Discussion #9318](https://github.com/tiangolo/fastapi/discussions/9318)\n- [Use Base64Url encoding and decoding in Session Cookie by allezxandre · Pull Request #1922 · encode/starlette](https://github.com/encode/starlette/pull/1922)\n- [SessionMiddleware sends a new set-cookie for every request, with unintended results · Issue #2019 · encode/starlette](https://github.com/encode/starlette/issues/2019)\n- [Create More Flexable SessionMiddleware · encode/starlette · Discussion #2256](https://github.com/encode/starlette/discussions/2256)\n- [SessionMiddleware may create malformed cookie · Issue #1259 · encode/starlette](https://github.com/encode/starlette/issues/1259)\n- [Added `allow_path_regex` to the `SessionMiddleware` by hasansezertasan · Pull Request #2316 · encode/starlette](https://github.com/encode/starlette/pull/2316)\n\nHere is another related project: [volfpeter/fastapi-flask-auth: Lightweight FastAPI dependencies and authenticator that uses Flask session cookies for access control](https://github.com/volfpeter/fastapi-flask-auth). This project actually inspired me to build `starlette-flask`.\n\nCheck out [Middleware - Starlette](https://www.starlette.io/middleware/) page to learn more about middlewares in [Starlette].\n\n## Author\n\n- [Hasan Sezer Tasan](https://www.github.com/hasansezertasan), It's me :wave:\n\n## Disclaimer\n\n**I am not a security expert.** I just wanted to share my solution to this problem. If you have any suggestions, please open an issue or a pull request.\n\n\u003e There are also some issues and discussions about problems with the default `SessionMiddleware` that comes with [Starlette]. I believe my implementation is not perfect but either the default `SessionMiddleware` that comes with [Starlette].\n\n## Why Archived\n\nThis project is archived as of January 2026. The reasons:\n\n1. **Goal achieved.** The middleware works and the session interoperability problem is solved.\n2. **The knowledge matters more than the package.** The core insight is that Flask signs session cookies using `itsdangerous.URLSafeTimedSerializer` with `key_derivation=\"hmac\"`, `digest_method=hashlib.sha1`, and `salt=b\"cookie-session\"`. Starlette uses a different signing method. Additionally, Starlette's default `SessionMiddleware` re-signs the cookie on every request, even when session data hasn't changed, overwriting Flask's cookies.\n3. **AI can generate this.** The middleware is ~100 lines. Given the problem description above, any coding agent can produce an equivalent implementation tailored to your needs.\n\nIf you need this functionality, you can:\n\n- Install the existing package: `pip install starlette-flask` (v0.0.1, still on PyPI)\n- Copy the middleware from [`src/starlette_flask/middleware/sessions.py`](src/starlette_flask/middleware/sessions.py) into your project\n- Ask an AI coding assistant to generate a Flask-compatible session middleware for Starlette using the signing details above\n\nSee [ADR-001](docs/decisions/001-archive-project.md) for the full decision record.\n\n## License\n\n`starlette-flask` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n\n[FastAPI]: https://github.com/tiangolo/fastapi\n[Starlette]: https://github.com/encode/starlette\n[Flask]: https://github.com/pallets/flask\n[Flask Admin]: https://github.com/flask-admin/flask-admin\n[Flask Login]: https://github.com/maxcountryman/flask-login\n[a2wsgi]: https://github.com/abersheeran/a2wsgi\n[Flask-JWT-Extended]: https://github.com/vimalloc/flask-jwt-extended\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasansezertasan%2Fstarlette-flask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhasansezertasan%2Fstarlette-flask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhasansezertasan%2Fstarlette-flask/lists"}