flask-session.manager.sk 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flask_session_manager_sk-0.1.1/.github/dependabot.yml +22 -0
- flask_session_manager_sk-0.1.1/.github/workflows/ci.yml +36 -0
- flask_session_manager_sk-0.1.1/.github/workflows/release.yml +111 -0
- flask_session_manager_sk-0.1.1/.gitignore +10 -0
- flask_session_manager_sk-0.1.1/.python-version +1 -0
- flask_session_manager_sk-0.1.1/LICENSE +21 -0
- flask_session_manager_sk-0.1.1/PKG-INFO +171 -0
- flask_session_manager_sk-0.1.1/README.md +150 -0
- flask_session_manager_sk-0.1.1/pyproject.toml +46 -0
- flask_session_manager_sk-0.1.1/src/flask_session_manager_sk/__init__.py +18 -0
- flask_session_manager_sk-0.1.1/src/flask_session_manager_sk/cookies.py +109 -0
- flask_session_manager_sk-0.1.1/src/flask_session_manager_sk/extension.py +110 -0
- flask_session_manager_sk-0.1.1/src/flask_session_manager_sk/request.py +35 -0
- flask_session_manager_sk-0.1.1/src/flask_session_manager_sk/tokens.py +43 -0
- flask_session_manager_sk-0.1.1/tests/__init__.py +0 -0
- flask_session_manager_sk-0.1.1/tests/integration/test_session_lifecycle.py +220 -0
- flask_session_manager_sk-0.1.1/tests/test_cookies.py +210 -0
- flask_session_manager_sk-0.1.1/tests/test_extension.py +227 -0
- flask_session_manager_sk-0.1.1/tests/test_request.py +131 -0
- flask_session_manager_sk-0.1.1/tests/test_tokens.py +103 -0
- flask_session_manager_sk-0.1.1/uv.lock +306 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: "pip"
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: "weekly"
|
|
7
|
+
open-pull-requests-limit: 10
|
|
8
|
+
groups:
|
|
9
|
+
python-minor-and-patch:
|
|
10
|
+
update-types:
|
|
11
|
+
- "minor"
|
|
12
|
+
- "patch"
|
|
13
|
+
|
|
14
|
+
- package-ecosystem: "github-actions"
|
|
15
|
+
directory: "/"
|
|
16
|
+
schedule:
|
|
17
|
+
interval: "weekly"
|
|
18
|
+
groups:
|
|
19
|
+
actions-minor-and-patch:
|
|
20
|
+
update-types:
|
|
21
|
+
- "minor"
|
|
22
|
+
- "patch"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
branches: [main]
|
|
7
|
+
workflow_call:
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
concurrency:
|
|
13
|
+
group: ci-${{ github.ref }}
|
|
14
|
+
cancel-in-progress: true
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
test:
|
|
18
|
+
name: Python ${{ matrix.python-version }}
|
|
19
|
+
runs-on: ubuntu-latest
|
|
20
|
+
strategy:
|
|
21
|
+
fail-fast: false
|
|
22
|
+
matrix:
|
|
23
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v7
|
|
26
|
+
- uses: astral-sh/setup-uv@v7
|
|
27
|
+
with:
|
|
28
|
+
enable-cache: true
|
|
29
|
+
- uses: actions/setup-python@v6
|
|
30
|
+
with:
|
|
31
|
+
python-version: ${{ matrix.python-version }}
|
|
32
|
+
- run: uv sync --frozen --dev
|
|
33
|
+
- run: uv run ruff check .
|
|
34
|
+
- run: uv run ruff format --check .
|
|
35
|
+
- run: uv run pytest -q
|
|
36
|
+
- run: uv build
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_run:
|
|
5
|
+
workflows: [CI]
|
|
6
|
+
branches: [main]
|
|
7
|
+
types: [completed]
|
|
8
|
+
workflow_dispatch:
|
|
9
|
+
inputs:
|
|
10
|
+
version_part:
|
|
11
|
+
description: Version increment
|
|
12
|
+
required: true
|
|
13
|
+
type: choice
|
|
14
|
+
default: patch
|
|
15
|
+
options:
|
|
16
|
+
- major
|
|
17
|
+
- minor
|
|
18
|
+
- patch
|
|
19
|
+
push:
|
|
20
|
+
tags:
|
|
21
|
+
- "v[0-9]+.[0-9]+.[0-9]+"
|
|
22
|
+
|
|
23
|
+
permissions:
|
|
24
|
+
contents: write
|
|
25
|
+
id-token: write
|
|
26
|
+
|
|
27
|
+
concurrency:
|
|
28
|
+
group: release-${{ github.event.workflow_run.head_branch || github.ref }}
|
|
29
|
+
cancel-in-progress: false
|
|
30
|
+
|
|
31
|
+
jobs:
|
|
32
|
+
publish:
|
|
33
|
+
name: Publish
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
if: >-
|
|
36
|
+
(github.event_name == 'workflow_run' &&
|
|
37
|
+
github.event.workflow_run.conclusion == 'success' &&
|
|
38
|
+
github.event.workflow_run.head_branch == 'main') ||
|
|
39
|
+
github.event_name == 'workflow_dispatch' ||
|
|
40
|
+
startsWith(github.ref, 'refs/tags/v')
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/checkout@v7
|
|
43
|
+
with:
|
|
44
|
+
fetch-depth: 0
|
|
45
|
+
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
|
|
46
|
+
token: ${{ secrets.GITHUB_TOKEN }}
|
|
47
|
+
- uses: astral-sh/setup-uv@v7
|
|
48
|
+
with:
|
|
49
|
+
enable-cache: true
|
|
50
|
+
|
|
51
|
+
- name: Bump version
|
|
52
|
+
id: bump
|
|
53
|
+
run: |
|
|
54
|
+
python - <<'PY'
|
|
55
|
+
import re, os
|
|
56
|
+
from pathlib import Path
|
|
57
|
+
|
|
58
|
+
pyproject = Path("pyproject.toml")
|
|
59
|
+
text = pyproject.read_text()
|
|
60
|
+
match = re.search(r'(?m)^version = "(\d+)\.(\d+)\.(\d+)"$', text)
|
|
61
|
+
major, minor, patch = map(int, match.groups())
|
|
62
|
+
|
|
63
|
+
# workflow_dispatch: user-chosen part
|
|
64
|
+
# workflow_run (auto): always patch
|
|
65
|
+
# tag push: no bump
|
|
66
|
+
part = os.environ.get("VERSION_PART", "patch")
|
|
67
|
+
|
|
68
|
+
if part == "major":
|
|
69
|
+
major += 1
|
|
70
|
+
minor = 0
|
|
71
|
+
patch = 0
|
|
72
|
+
elif part == "minor":
|
|
73
|
+
minor += 1
|
|
74
|
+
patch = 0
|
|
75
|
+
else:
|
|
76
|
+
patch += 1
|
|
77
|
+
|
|
78
|
+
new = f"{major}.{minor}.{patch}"
|
|
79
|
+
text = text[: match.start(1)] + new + text[match.end(3) :]
|
|
80
|
+
pyproject.write_text(text)
|
|
81
|
+
|
|
82
|
+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
|
|
83
|
+
f.write(f"new_version={new}\n")
|
|
84
|
+
PY
|
|
85
|
+
env:
|
|
86
|
+
VERSION_PART: >-
|
|
87
|
+
${{
|
|
88
|
+
github.event_name == 'workflow_dispatch'
|
|
89
|
+
&& github.event.inputs.version_part
|
|
90
|
+
|| 'patch'
|
|
91
|
+
}}
|
|
92
|
+
|
|
93
|
+
- name: Rebuild lockfile
|
|
94
|
+
if: github.event_name != 'push'
|
|
95
|
+
run: uv lock
|
|
96
|
+
|
|
97
|
+
- run: uv build
|
|
98
|
+
|
|
99
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
100
|
+
|
|
101
|
+
- name: Commit version bump and tag
|
|
102
|
+
if: github.event_name != 'push'
|
|
103
|
+
env:
|
|
104
|
+
NEW_VERSION: ${{ steps.bump.outputs.new_version }}
|
|
105
|
+
run: |
|
|
106
|
+
git config user.name "github-actions[bot]"
|
|
107
|
+
git config user.email "github-actions[bot]@users.noreply.github.com"
|
|
108
|
+
git add pyproject.toml uv.lock
|
|
109
|
+
git commit -m "chore: bump version to ${NEW_VERSION}"
|
|
110
|
+
git tag "v${NEW_VERSION}"
|
|
111
|
+
git push origin HEAD:main --follow-tags
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.11
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Skulldorom
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flask-session.manager.sk
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Flask companion package for react-session.manager.sk
|
|
5
|
+
Project-URL: Homepage, https://github.com/Skulldorom/flask-session.manager.sk
|
|
6
|
+
Project-URL: Repository, https://github.com/Skulldorom/flask-session.manager.sk
|
|
7
|
+
Project-URL: Issues, https://github.com/Skulldorom/flask-session.manager.sk/issues
|
|
8
|
+
Author: Skulldorom
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Framework :: Flask
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Requires-Dist: flask-jwt-extended>=4.7
|
|
18
|
+
Requires-Dist: flask>=3.1
|
|
19
|
+
Requires-Dist: ua-parser>=1.0
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# flask-session.manager.sk
|
|
23
|
+
|
|
24
|
+
Flask companion package for
|
|
25
|
+
[react-session.manager.sk](https://github.com/Skulldorom/react-session.manager.sk).
|
|
26
|
+
|
|
27
|
+
Cookie-driven JWT session management for Flask backends, designed to pair with the
|
|
28
|
+
React session manager's HttpOnly-cookie transport.
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install flask-session.manager.sk
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from flask import Flask, jsonify
|
|
40
|
+
from flask_jwt_extended import create_access_token
|
|
41
|
+
from flask_session_manager_sk import SessionManager, SessionManagerCallbacks
|
|
42
|
+
|
|
43
|
+
app = Flask(__name__)
|
|
44
|
+
app.config.update(
|
|
45
|
+
{
|
|
46
|
+
"SECRET_KEY": "your-secret-key",
|
|
47
|
+
"JWT_TOKEN_LOCATION": ["cookies"],
|
|
48
|
+
"JWT_COOKIE_SECURE": True, # require HTTPS in production
|
|
49
|
+
"JWT_COOKIE_CSRF_PROTECT": True,
|
|
50
|
+
"JWT_ACCESS_COOKIE_NAME": "access_token_cookie",
|
|
51
|
+
"FRONTEND_URL": "https://myapp.example.com",
|
|
52
|
+
"CORS_ORIGINS": ["https://myapp.example.com"],
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
manager = SessionManager()
|
|
57
|
+
|
|
58
|
+
callbacks = SessionManagerCallbacks(
|
|
59
|
+
user_lookup=lambda identity: get_user_by_id(identity),
|
|
60
|
+
refresh_user_token=lambda user, agent, device_uid: create_and_store_token(
|
|
61
|
+
user, agent, device_uid
|
|
62
|
+
),
|
|
63
|
+
verify_user_token=lambda user, agent, device_uid, token: user.check_token(
|
|
64
|
+
agent, device_uid, token
|
|
65
|
+
),
|
|
66
|
+
is_user_active=lambda user: user.is_active,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
manager.init_app(app, callbacks=callbacks)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@app.route("/auth/who")
|
|
73
|
+
@jwt_required(optional=True)
|
|
74
|
+
def whoami():
|
|
75
|
+
from flask_jwt_extended import current_user
|
|
76
|
+
|
|
77
|
+
if current_user:
|
|
78
|
+
return jsonify(logged_in=True, user_id=current_user.id)
|
|
79
|
+
return jsonify(logged_in=False)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Public API (v0.1)
|
|
83
|
+
|
|
84
|
+
Only four names are part of the stable surface:
|
|
85
|
+
|
|
86
|
+
| Name | Description |
|
|
87
|
+
|------|-------------|
|
|
88
|
+
| `SessionManager` | Flask extension that wires JWT callbacks |
|
|
89
|
+
| `SessionManagerCallbacks` | Frozen dataclass of application hooks |
|
|
90
|
+
| `token_response` | Create a JSON response with an HttpOnly JWT cookie |
|
|
91
|
+
| `clear_token_response` | Create a response that clears JWT cookies |
|
|
92
|
+
|
|
93
|
+
Everything else in the package is internal and may change without notice.
|
|
94
|
+
|
|
95
|
+
### SessionManagerCallbacks
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
@dataclass(frozen=True)
|
|
99
|
+
class SessionManagerCallbacks:
|
|
100
|
+
# Required
|
|
101
|
+
user_lookup: Callable[[str], Any | None]
|
|
102
|
+
refresh_user_token: Callable[[Any, str, str | None], str | None]
|
|
103
|
+
|
|
104
|
+
# Optional
|
|
105
|
+
verify_user_token: Callable[[Any, str | None, str | None], Any | None] | None = None
|
|
106
|
+
is_user_active: Callable[[Any], bool] | None = None
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Flask Configuration Reference
|
|
110
|
+
|
|
111
|
+
These values are read at runtime via `current_app.config`. No configuration is
|
|
112
|
+
stored in the package.
|
|
113
|
+
|
|
114
|
+
| Key | Required | Default | Description |
|
|
115
|
+
|-----|----------|---------|-------------|
|
|
116
|
+
| `SECRET_KEY` | Yes | — | Flask secret, must be ≥32 bytes for HMAC-SHA256 |
|
|
117
|
+
| `JWT_TOKEN_LOCATION` | Yes | — | Should include `"cookies"` |
|
|
118
|
+
| `JWT_ACCESS_COOKIE_NAME` | Yes | — | Cookie name; `react-session.manager.sk` expects `"access_token_cookie"` |
|
|
119
|
+
| `JWT_COOKIE_CSRF_PROTECT` | Yes | — | Must be `True` for browser clients |
|
|
120
|
+
| `JWT_COOKIE_SECURE` | Yes | — | `True` in production (HTTPS only) |
|
|
121
|
+
| `JWT_COOKIE_SAMESITE` | No | — | `"Lax"` or `"Strict"` |
|
|
122
|
+
| `FRONTEND_URL` | Yes | — | Canonical URL of the SPA |
|
|
123
|
+
| `CORS_ORIGINS` | Yes | — | List of allowed browser origins |
|
|
124
|
+
|
|
125
|
+
## React Companion Contract
|
|
126
|
+
|
|
127
|
+
The frontend companion
|
|
128
|
+
[react-session.manager.sk](https://github.com/Skulldorom/react-session.manager.sk)
|
|
129
|
+
(v4.0+) uses this transport contract:
|
|
130
|
+
|
|
131
|
+
- **Axios** configured with `withCredentials: true`, `withXSRFToken: true`
|
|
132
|
+
- Cookies expected at default Flask-JWT-Extended names:
|
|
133
|
+
- `access_token_cookie` (JWT)
|
|
134
|
+
- `csrf_access_token` (CSRF double-submit)
|
|
135
|
+
- CSRF header sent as `X-CSRF-TOKEN`
|
|
136
|
+
- `deviceUID` header sent on every request
|
|
137
|
+
- `appVersion` header sent on every request
|
|
138
|
+
- No `Authorization` bearer header for browser requests
|
|
139
|
+
- Legacy `localStorage`/`sessionStorage` bearer tokens are automatically cleared
|
|
140
|
+
|
|
141
|
+
## Internal Modules
|
|
142
|
+
|
|
143
|
+
Not part of the stable API. Import at your own risk.
|
|
144
|
+
|
|
145
|
+
| Module | Content |
|
|
146
|
+
|--------|---------|
|
|
147
|
+
| `flask_session_manager_sk.cookies` | CSRF origin checks, cookie auth detection, `clear_session_token_value` |
|
|
148
|
+
| `flask_session_manager_sk.request` | `get_agent`, `get_ip`, `get_token`, `get_dets_from_request` |
|
|
149
|
+
| `flask_session_manager_sk.tokens` | `create_token_hash`, `token_hint`, `verify_token_hash`, `update_token_record` |
|
|
150
|
+
| `flask_session_manager_sk.extension` | `SessionManager` implementation details |
|
|
151
|
+
|
|
152
|
+
## Migrating from Bearer Tokens
|
|
153
|
+
|
|
154
|
+
If your backend currently returns access tokens as JSON payloads (e.g.
|
|
155
|
+
`{"access_token": "..."}`) and stores them in `localStorage`:
|
|
156
|
+
|
|
157
|
+
1. **Package adoption**: Wire `SessionManager` with your user-lookup and
|
|
158
|
+
token-refresh callbacks.
|
|
159
|
+
2. **Login endpoint**: Call `token_response(payload, status, access_token)`
|
|
160
|
+
instead of `jsonify(payload)`. This sets the HttpOnly cookie automatically.
|
|
161
|
+
3. **CSRF check**: Add `reject_cookie_csrf()` as a `@app.before_request` hook
|
|
162
|
+
for all unsafe methods (POST, PUT, DELETE).
|
|
163
|
+
4. **Frontend**: Upgrade `react-session.manager.sk` to v4.0+. It removes
|
|
164
|
+
bearer-token browser storage and sends cookie credentials automatically.
|
|
165
|
+
5. **Backwards compatibility**: Non-browser clients (API scripts, scheduled
|
|
166
|
+
tasks) can still send `Authorization: Bearer <token>`. The CSRF check
|
|
167
|
+
skips bearer-authenticated requests.
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# flask-session.manager.sk
|
|
2
|
+
|
|
3
|
+
Flask companion package for
|
|
4
|
+
[react-session.manager.sk](https://github.com/Skulldorom/react-session.manager.sk).
|
|
5
|
+
|
|
6
|
+
Cookie-driven JWT session management for Flask backends, designed to pair with the
|
|
7
|
+
React session manager's HttpOnly-cookie transport.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install flask-session.manager.sk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from flask import Flask, jsonify
|
|
19
|
+
from flask_jwt_extended import create_access_token
|
|
20
|
+
from flask_session_manager_sk import SessionManager, SessionManagerCallbacks
|
|
21
|
+
|
|
22
|
+
app = Flask(__name__)
|
|
23
|
+
app.config.update(
|
|
24
|
+
{
|
|
25
|
+
"SECRET_KEY": "your-secret-key",
|
|
26
|
+
"JWT_TOKEN_LOCATION": ["cookies"],
|
|
27
|
+
"JWT_COOKIE_SECURE": True, # require HTTPS in production
|
|
28
|
+
"JWT_COOKIE_CSRF_PROTECT": True,
|
|
29
|
+
"JWT_ACCESS_COOKIE_NAME": "access_token_cookie",
|
|
30
|
+
"FRONTEND_URL": "https://myapp.example.com",
|
|
31
|
+
"CORS_ORIGINS": ["https://myapp.example.com"],
|
|
32
|
+
}
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
manager = SessionManager()
|
|
36
|
+
|
|
37
|
+
callbacks = SessionManagerCallbacks(
|
|
38
|
+
user_lookup=lambda identity: get_user_by_id(identity),
|
|
39
|
+
refresh_user_token=lambda user, agent, device_uid: create_and_store_token(
|
|
40
|
+
user, agent, device_uid
|
|
41
|
+
),
|
|
42
|
+
verify_user_token=lambda user, agent, device_uid, token: user.check_token(
|
|
43
|
+
agent, device_uid, token
|
|
44
|
+
),
|
|
45
|
+
is_user_active=lambda user: user.is_active,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
manager.init_app(app, callbacks=callbacks)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@app.route("/auth/who")
|
|
52
|
+
@jwt_required(optional=True)
|
|
53
|
+
def whoami():
|
|
54
|
+
from flask_jwt_extended import current_user
|
|
55
|
+
|
|
56
|
+
if current_user:
|
|
57
|
+
return jsonify(logged_in=True, user_id=current_user.id)
|
|
58
|
+
return jsonify(logged_in=False)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Public API (v0.1)
|
|
62
|
+
|
|
63
|
+
Only four names are part of the stable surface:
|
|
64
|
+
|
|
65
|
+
| Name | Description |
|
|
66
|
+
|------|-------------|
|
|
67
|
+
| `SessionManager` | Flask extension that wires JWT callbacks |
|
|
68
|
+
| `SessionManagerCallbacks` | Frozen dataclass of application hooks |
|
|
69
|
+
| `token_response` | Create a JSON response with an HttpOnly JWT cookie |
|
|
70
|
+
| `clear_token_response` | Create a response that clears JWT cookies |
|
|
71
|
+
|
|
72
|
+
Everything else in the package is internal and may change without notice.
|
|
73
|
+
|
|
74
|
+
### SessionManagerCallbacks
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
@dataclass(frozen=True)
|
|
78
|
+
class SessionManagerCallbacks:
|
|
79
|
+
# Required
|
|
80
|
+
user_lookup: Callable[[str], Any | None]
|
|
81
|
+
refresh_user_token: Callable[[Any, str, str | None], str | None]
|
|
82
|
+
|
|
83
|
+
# Optional
|
|
84
|
+
verify_user_token: Callable[[Any, str | None, str | None], Any | None] | None = None
|
|
85
|
+
is_user_active: Callable[[Any], bool] | None = None
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Flask Configuration Reference
|
|
89
|
+
|
|
90
|
+
These values are read at runtime via `current_app.config`. No configuration is
|
|
91
|
+
stored in the package.
|
|
92
|
+
|
|
93
|
+
| Key | Required | Default | Description |
|
|
94
|
+
|-----|----------|---------|-------------|
|
|
95
|
+
| `SECRET_KEY` | Yes | — | Flask secret, must be ≥32 bytes for HMAC-SHA256 |
|
|
96
|
+
| `JWT_TOKEN_LOCATION` | Yes | — | Should include `"cookies"` |
|
|
97
|
+
| `JWT_ACCESS_COOKIE_NAME` | Yes | — | Cookie name; `react-session.manager.sk` expects `"access_token_cookie"` |
|
|
98
|
+
| `JWT_COOKIE_CSRF_PROTECT` | Yes | — | Must be `True` for browser clients |
|
|
99
|
+
| `JWT_COOKIE_SECURE` | Yes | — | `True` in production (HTTPS only) |
|
|
100
|
+
| `JWT_COOKIE_SAMESITE` | No | — | `"Lax"` or `"Strict"` |
|
|
101
|
+
| `FRONTEND_URL` | Yes | — | Canonical URL of the SPA |
|
|
102
|
+
| `CORS_ORIGINS` | Yes | — | List of allowed browser origins |
|
|
103
|
+
|
|
104
|
+
## React Companion Contract
|
|
105
|
+
|
|
106
|
+
The frontend companion
|
|
107
|
+
[react-session.manager.sk](https://github.com/Skulldorom/react-session.manager.sk)
|
|
108
|
+
(v4.0+) uses this transport contract:
|
|
109
|
+
|
|
110
|
+
- **Axios** configured with `withCredentials: true`, `withXSRFToken: true`
|
|
111
|
+
- Cookies expected at default Flask-JWT-Extended names:
|
|
112
|
+
- `access_token_cookie` (JWT)
|
|
113
|
+
- `csrf_access_token` (CSRF double-submit)
|
|
114
|
+
- CSRF header sent as `X-CSRF-TOKEN`
|
|
115
|
+
- `deviceUID` header sent on every request
|
|
116
|
+
- `appVersion` header sent on every request
|
|
117
|
+
- No `Authorization` bearer header for browser requests
|
|
118
|
+
- Legacy `localStorage`/`sessionStorage` bearer tokens are automatically cleared
|
|
119
|
+
|
|
120
|
+
## Internal Modules
|
|
121
|
+
|
|
122
|
+
Not part of the stable API. Import at your own risk.
|
|
123
|
+
|
|
124
|
+
| Module | Content |
|
|
125
|
+
|--------|---------|
|
|
126
|
+
| `flask_session_manager_sk.cookies` | CSRF origin checks, cookie auth detection, `clear_session_token_value` |
|
|
127
|
+
| `flask_session_manager_sk.request` | `get_agent`, `get_ip`, `get_token`, `get_dets_from_request` |
|
|
128
|
+
| `flask_session_manager_sk.tokens` | `create_token_hash`, `token_hint`, `verify_token_hash`, `update_token_record` |
|
|
129
|
+
| `flask_session_manager_sk.extension` | `SessionManager` implementation details |
|
|
130
|
+
|
|
131
|
+
## Migrating from Bearer Tokens
|
|
132
|
+
|
|
133
|
+
If your backend currently returns access tokens as JSON payloads (e.g.
|
|
134
|
+
`{"access_token": "..."}`) and stores them in `localStorage`:
|
|
135
|
+
|
|
136
|
+
1. **Package adoption**: Wire `SessionManager` with your user-lookup and
|
|
137
|
+
token-refresh callbacks.
|
|
138
|
+
2. **Login endpoint**: Call `token_response(payload, status, access_token)`
|
|
139
|
+
instead of `jsonify(payload)`. This sets the HttpOnly cookie automatically.
|
|
140
|
+
3. **CSRF check**: Add `reject_cookie_csrf()` as a `@app.before_request` hook
|
|
141
|
+
for all unsafe methods (POST, PUT, DELETE).
|
|
142
|
+
4. **Frontend**: Upgrade `react-session.manager.sk` to v4.0+. It removes
|
|
143
|
+
bearer-token browser storage and sends cookie credentials automatically.
|
|
144
|
+
5. **Backwards compatibility**: Non-browser clients (API scripts, scheduled
|
|
145
|
+
tasks) can still send `Authorization: Bearer <token>`. The CSRF check
|
|
146
|
+
skips bearer-authenticated requests.
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
MIT — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "flask-session.manager.sk"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = "Flask companion package for react-session.manager.sk"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = { text = "MIT" }
|
|
8
|
+
authors = [{ name = "Skulldorom" }]
|
|
9
|
+
dependencies = [
|
|
10
|
+
"flask>=3.1",
|
|
11
|
+
"flask-jwt-extended>=4.7",
|
|
12
|
+
"ua-parser>=1.0",
|
|
13
|
+
]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Framework :: Flask",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/Skulldorom/flask-session.manager.sk"
|
|
24
|
+
Repository = "https://github.com/Skulldorom/flask-session.manager.sk"
|
|
25
|
+
Issues = "https://github.com/Skulldorom/flask-session.manager.sk/issues"
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["hatchling"]
|
|
29
|
+
build-backend = "hatchling.build"
|
|
30
|
+
|
|
31
|
+
[dependency-groups]
|
|
32
|
+
dev = [
|
|
33
|
+
"pytest>=9",
|
|
34
|
+
"ruff>=0.14",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[tool.ruff]
|
|
38
|
+
target-version = "py311"
|
|
39
|
+
line-length = 88
|
|
40
|
+
|
|
41
|
+
[tool.ruff.lint]
|
|
42
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
43
|
+
|
|
44
|
+
[tool.ruff.format]
|
|
45
|
+
quote-style = "double"
|
|
46
|
+
indent-style = "space"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Flask companion package for react-session.manager.sk.
|
|
2
|
+
|
|
3
|
+
Public API (stable):
|
|
4
|
+
SessionManager — Flask extension for JWT session management
|
|
5
|
+
SessionManagerCallbacks — frozen dataclass of callback hooks
|
|
6
|
+
token_response — create a Flask response with an HttpOnly JWT cookie
|
|
7
|
+
clear_token_response — create a Flask response that clears JWT cookies
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .cookies import clear_token_response, token_response
|
|
11
|
+
from .extension import SessionManager, SessionManagerCallbacks
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"SessionManager",
|
|
15
|
+
"SessionManagerCallbacks",
|
|
16
|
+
"token_response",
|
|
17
|
+
"clear_token_response",
|
|
18
|
+
]
|