django-cerber 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.
- django_cerber-0.1.1/LICENSE +22 -0
- django_cerber-0.1.1/PKG-INFO +166 -0
- django_cerber-0.1.1/README.md +132 -0
- django_cerber-0.1.1/pyproject.toml +190 -0
- django_cerber-0.1.1/setup.cfg +4 -0
- django_cerber-0.1.1/src/django_cerber/__init__.py +1 -0
- django_cerber-0.1.1/src/django_cerber/actions.py +102 -0
- django_cerber-0.1.1/src/django_cerber/admin.py +44 -0
- django_cerber-0.1.1/src/django_cerber/apps.py +14 -0
- django_cerber-0.1.1/src/django_cerber/conf.py +99 -0
- django_cerber-0.1.1/src/django_cerber/guard.py +327 -0
- django_cerber-0.1.1/src/django_cerber/main.py +19 -0
- django_cerber-0.1.1/src/django_cerber/management/__init__.py +0 -0
- django_cerber-0.1.1/src/django_cerber/management/commands/__init__.py +0 -0
- django_cerber-0.1.1/src/django_cerber/management/commands/cerber_logout.py +51 -0
- django_cerber-0.1.1/src/django_cerber/middleware.py +49 -0
- django_cerber-0.1.1/src/django_cerber/migrations/0001_initial.py +169 -0
- django_cerber-0.1.1/src/django_cerber/migrations/__init__.py +0 -0
- django_cerber-0.1.1/src/django_cerber/models.py +110 -0
- django_cerber-0.1.1/src/django_cerber/py.typed +0 -0
- django_cerber-0.1.1/src/django_cerber/sessions.py +78 -0
- django_cerber-0.1.1/src/django_cerber/templates/admin/django_cerber/logout_all_confirmation.html +29 -0
- django_cerber-0.1.1/src/django_cerber.egg-info/PKG-INFO +166 -0
- django_cerber-0.1.1/src/django_cerber.egg-info/SOURCES.txt +28 -0
- django_cerber-0.1.1/src/django_cerber.egg-info/dependency_links.txt +1 -0
- django_cerber-0.1.1/src/django_cerber.egg-info/requires.txt +5 -0
- django_cerber-0.1.1/src/django_cerber.egg-info/top_level.txt +1 -0
- django_cerber-0.1.1/tests/test_guard.py +440 -0
- django_cerber-0.1.1/tests/test_logout.py +168 -0
- django_cerber-0.1.1/tests/test_main.py +17 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2025 Tomasz Drobiszewski
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-cerber
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Login anomaly detection for Django: country changes, Tor and session revocation.
|
|
5
|
+
Author-email: Tomasz Drobiszewski <tdrobiszewski@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/iddqd1/django-cerber/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/iddqd1/django-cerber/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: documentation, https://django-cerber.readthedocs.io
|
|
10
|
+
Project-URL: repository, https://github.com/iddqd1/django-cerber
|
|
11
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
12
|
+
Classifier: Framework :: Django
|
|
13
|
+
Classifier: Framework :: Django :: 5.2
|
|
14
|
+
Classifier: Framework :: Django :: 6.0
|
|
15
|
+
Classifier: Framework :: Django :: 6.1
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Natural Language :: English
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
26
|
+
Requires-Python: >=3.11
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: django>=5.2
|
|
30
|
+
Requires-Dist: django-model-utils>=5
|
|
31
|
+
Provides-Extra: geoip
|
|
32
|
+
Requires-Dist: geoip2>=5.4; extra == "geoip"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# Django Cerber
|
|
36
|
+
|
|
37
|
+
<p align="center">
|
|
38
|
+
<a href="https://github.com/iddqd1/django-cerber/actions/workflows/ci.yml?query=branch%3Amain">
|
|
39
|
+
<img src="https://img.shields.io/github/actions/workflow/status/iddqd1/django-cerber/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >
|
|
40
|
+
</a>
|
|
41
|
+
<a href="https://django-cerber.readthedocs.io">
|
|
42
|
+
<img src="https://img.shields.io/readthedocs/django-cerber.svg?logo=read-the-docs&logoColor=fff&style=flat-square" alt="Documentation Status">
|
|
43
|
+
</a>
|
|
44
|
+
<a href="https://codecov.io/gh/iddqd1/django-cerber">
|
|
45
|
+
<img src="https://img.shields.io/codecov/c/github/iddqd1/django-cerber.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">
|
|
46
|
+
</a>
|
|
47
|
+
</p>
|
|
48
|
+
<p align="center">
|
|
49
|
+
<a href="https://github.com/astral-sh/uv">
|
|
50
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv">
|
|
51
|
+
</a>
|
|
52
|
+
<a href="https://github.com/astral-sh/ruff">
|
|
53
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff">
|
|
54
|
+
</a>
|
|
55
|
+
<a href="https://github.com/j178/prek">
|
|
56
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json" alt="prek">
|
|
57
|
+
</a>
|
|
58
|
+
</p>
|
|
59
|
+
<p align="center">
|
|
60
|
+
<a href="https://pypi.org/project/django-cerber/">
|
|
61
|
+
<img src="https://img.shields.io/pypi/v/django-cerber.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">
|
|
62
|
+
</a>
|
|
63
|
+
<img src="https://img.shields.io/pypi/pyversions/django-cerber.svg?style=flat-square&logo=python&logoColor=fff" alt="Supported Python versions">
|
|
64
|
+
<img src="https://img.shields.io/pypi/l/django-cerber.svg?style=flat-square" alt="License">
|
|
65
|
+
</p>
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
**Documentation**: <a href="https://django-cerber.readthedocs.io" target="_blank">https://django-cerber.readthedocs.io </a>
|
|
70
|
+
|
|
71
|
+
**Source Code**: <a href="https://github.com/iddqd1/django-cerber" target="_blank">https://github.com/iddqd1/django-cerber </a>
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
Login anomaly detection for Django: country changes, Tor and session revocation.
|
|
76
|
+
|
|
77
|
+
## Installation
|
|
78
|
+
|
|
79
|
+
Install this via pip (or your favourite package manager):
|
|
80
|
+
|
|
81
|
+
`pip install django-cerber`
|
|
82
|
+
|
|
83
|
+
Add the app to your `INSTALLED_APPS`:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
INSTALLED_APPS = [
|
|
87
|
+
# ...
|
|
88
|
+
"django_cerber",
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
MIDDLEWARE = [
|
|
92
|
+
# ...
|
|
93
|
+
"django.contrib.sessions.middleware.SessionMiddleware",
|
|
94
|
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
95
|
+
# ...
|
|
96
|
+
"django_cerber.middleware.CerberMiddleware",
|
|
97
|
+
]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Then run `python manage.py migrate`. For the GeoIP country source install the
|
|
101
|
+
extra: `pip install "django-cerber[geoip]"`.
|
|
102
|
+
|
|
103
|
+
## Configuration
|
|
104
|
+
|
|
105
|
+
All settings are optional. Add them to your Django settings module to override
|
|
106
|
+
the defaults, for example:
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from datetime import timedelta
|
|
110
|
+
|
|
111
|
+
CERBER_WINDOW = timedelta(minutes=30)
|
|
112
|
+
CERBER_COUNTRY_SOURCES = ("cloudflare", "geoip")
|
|
113
|
+
CERBER_GEOIP_DATABASE = "/var/lib/geoip/GeoLite2-Country.mmdb"
|
|
114
|
+
CERBER_BLOCKED_COUNTRIES = ("KP",)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
<!-- CERBER-SETTINGS:START - generated by scripts/readme_settings.py -->
|
|
118
|
+
|
|
119
|
+
| Setting | Default | Description |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| `CERBER_ENABLED` | `True` | Whether login checks are performed at all. |
|
|
122
|
+
| `CERBER_WINDOW` | `timedelta(hours=1)` | A country change faster than this is treated as impossible travel. |
|
|
123
|
+
| `CERBER_COUNTRY_SOURCES` | `('cloudflare',)` | Where the client's country comes from, tried in order until one knows it.<br><br>`"cloudflare"` reads the `CF-IPCountry` header, `"geoip"` looks the client IP up in `CERBER_GEOIP_DATABASE`. E.g. `("cloudflare", "geoip")` uses GeoIP as a fallback when the request didn't come through Cloudflare. |
|
|
124
|
+
| `CERBER_GEOIP_DATABASE` | `None` | Path to a MaxMind GeoIP2/GeoLite2 (or DB-IP) Country or City `.mmdb` file.<br><br>Required by the `"geoip"` source; needs `pip install django-cerber[geoip]`. |
|
|
125
|
+
| `CERBER_PROXY_IP_HEADER` | `None` | Header set by *your* reverse proxy with the address it received the request from, e.g. `"X-Forwarded-For"` or `"X-Real-IP"`. `None` = use `REMOTE_ADDR`.<br><br>Only set this when every request goes through that proxy, otherwise clients can forge it. |
|
|
126
|
+
| `CERBER_PROXY_COUNT` | `1` | Number of your own proxies that append to `CERBER_PROXY_IP_HEADER`.<br><br>For a comma separated list such as `X-Forwarded-For: client, proxy1`, the address is taken this many entries from the right; anything further left was sent by the client and is ignored. |
|
|
127
|
+
| `CERBER_VERIFY_CLOUDFLARE_IP` | `True` | Trust Cloudflare's headers only when the peer address is a Cloudflare IP. |
|
|
128
|
+
| `CERBER_BLOCKED_COUNTRIES` | `()` | ISO-3166 alpha-2 codes whose logins are always blocked, e.g. `("RU", "KP")`.<br><br>Takes precedence over everything else, including impossible-travel exceptions. Use `"T1"` here to block Tor via Cloudflare as well. |
|
|
129
|
+
| `CERBER_BLOCK_TOR` | `False` | Block logins from Tor exit nodes (Cloudflare reports them as `T1`). |
|
|
130
|
+
| `CERBER_LOGOUT_ALL_SESSIONS` | `True` | On a blocked login, also end every other session of that user. |
|
|
131
|
+
| `CERBER_REDIRECT_URL` | `None` | Where blocked browser logins are redirected. Defaults to `LOGIN_URL`. |
|
|
132
|
+
|
|
133
|
+
<!-- CERBER-SETTINGS:END -->
|
|
134
|
+
|
|
135
|
+
## Contributors ✨
|
|
136
|
+
|
|
137
|
+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
138
|
+
|
|
139
|
+
<!-- prettier-ignore-start -->
|
|
140
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
141
|
+
<!-- prettier-ignore-start -->
|
|
142
|
+
<!-- markdownlint-disable -->
|
|
143
|
+
<table>
|
|
144
|
+
<tbody>
|
|
145
|
+
<tr>
|
|
146
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/iddqd1"><img src="https://avatars.githubusercontent.com/u/472451?v=4&size=80" width="80px" height="80px" alt="Tomasz Drobiszewski"/><br /><sub><b>Tomasz Drobiszewski</b></sub></a><br /><a href="https://github.com/iddqd1/django-cerber/commits?author=iddqd1" title="Code">💻</a> <a href="https://github.com/iddqd1/django-cerber/commits?author=iddqd1" title="Documentation">📖</a> <a href="#ideas-iddqd1" title="Ideas, Planning, & Feedback">🤔</a> <a href="#maintenance-iddqd1" title="Maintenance">🚧</a> <a href="https://github.com/iddqd1/django-cerber/commits?author=iddqd1" title="Tests">⚠️</a></td>
|
|
147
|
+
</tr>
|
|
148
|
+
</tbody>
|
|
149
|
+
</table>
|
|
150
|
+
|
|
151
|
+
<!-- markdownlint-restore -->
|
|
152
|
+
<!-- prettier-ignore-end -->
|
|
153
|
+
|
|
154
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
155
|
+
<!-- prettier-ignore-end -->
|
|
156
|
+
|
|
157
|
+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
158
|
+
|
|
159
|
+
## Credits
|
|
160
|
+
|
|
161
|
+
[](https://github.com/copier-org/copier)
|
|
162
|
+
|
|
163
|
+
This package was created with
|
|
164
|
+
[Copier](https://copier.readthedocs.io/) and the
|
|
165
|
+
[browniebroke/pypackage-template](https://github.com/browniebroke/pypackage-template)
|
|
166
|
+
project template.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Django Cerber
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://github.com/iddqd1/django-cerber/actions/workflows/ci.yml?query=branch%3Amain">
|
|
5
|
+
<img src="https://img.shields.io/github/actions/workflow/status/iddqd1/django-cerber/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://django-cerber.readthedocs.io">
|
|
8
|
+
<img src="https://img.shields.io/readthedocs/django-cerber.svg?logo=read-the-docs&logoColor=fff&style=flat-square" alt="Documentation Status">
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://codecov.io/gh/iddqd1/django-cerber">
|
|
11
|
+
<img src="https://img.shields.io/codecov/c/github/iddqd1/django-cerber.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">
|
|
12
|
+
</a>
|
|
13
|
+
</p>
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://github.com/astral-sh/uv">
|
|
16
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv">
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://github.com/astral-sh/ruff">
|
|
19
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff">
|
|
20
|
+
</a>
|
|
21
|
+
<a href="https://github.com/j178/prek">
|
|
22
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json" alt="prek">
|
|
23
|
+
</a>
|
|
24
|
+
</p>
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://pypi.org/project/django-cerber/">
|
|
27
|
+
<img src="https://img.shields.io/pypi/v/django-cerber.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">
|
|
28
|
+
</a>
|
|
29
|
+
<img src="https://img.shields.io/pypi/pyversions/django-cerber.svg?style=flat-square&logo=python&logoColor=fff" alt="Supported Python versions">
|
|
30
|
+
<img src="https://img.shields.io/pypi/l/django-cerber.svg?style=flat-square" alt="License">
|
|
31
|
+
</p>
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
**Documentation**: <a href="https://django-cerber.readthedocs.io" target="_blank">https://django-cerber.readthedocs.io </a>
|
|
36
|
+
|
|
37
|
+
**Source Code**: <a href="https://github.com/iddqd1/django-cerber" target="_blank">https://github.com/iddqd1/django-cerber </a>
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
Login anomaly detection for Django: country changes, Tor and session revocation.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Install this via pip (or your favourite package manager):
|
|
46
|
+
|
|
47
|
+
`pip install django-cerber`
|
|
48
|
+
|
|
49
|
+
Add the app to your `INSTALLED_APPS`:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
INSTALLED_APPS = [
|
|
53
|
+
# ...
|
|
54
|
+
"django_cerber",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
MIDDLEWARE = [
|
|
58
|
+
# ...
|
|
59
|
+
"django.contrib.sessions.middleware.SessionMiddleware",
|
|
60
|
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
61
|
+
# ...
|
|
62
|
+
"django_cerber.middleware.CerberMiddleware",
|
|
63
|
+
]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Then run `python manage.py migrate`. For the GeoIP country source install the
|
|
67
|
+
extra: `pip install "django-cerber[geoip]"`.
|
|
68
|
+
|
|
69
|
+
## Configuration
|
|
70
|
+
|
|
71
|
+
All settings are optional. Add them to your Django settings module to override
|
|
72
|
+
the defaults, for example:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from datetime import timedelta
|
|
76
|
+
|
|
77
|
+
CERBER_WINDOW = timedelta(minutes=30)
|
|
78
|
+
CERBER_COUNTRY_SOURCES = ("cloudflare", "geoip")
|
|
79
|
+
CERBER_GEOIP_DATABASE = "/var/lib/geoip/GeoLite2-Country.mmdb"
|
|
80
|
+
CERBER_BLOCKED_COUNTRIES = ("KP",)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
<!-- CERBER-SETTINGS:START - generated by scripts/readme_settings.py -->
|
|
84
|
+
|
|
85
|
+
| Setting | Default | Description |
|
|
86
|
+
| --- | --- | --- |
|
|
87
|
+
| `CERBER_ENABLED` | `True` | Whether login checks are performed at all. |
|
|
88
|
+
| `CERBER_WINDOW` | `timedelta(hours=1)` | A country change faster than this is treated as impossible travel. |
|
|
89
|
+
| `CERBER_COUNTRY_SOURCES` | `('cloudflare',)` | Where the client's country comes from, tried in order until one knows it.<br><br>`"cloudflare"` reads the `CF-IPCountry` header, `"geoip"` looks the client IP up in `CERBER_GEOIP_DATABASE`. E.g. `("cloudflare", "geoip")` uses GeoIP as a fallback when the request didn't come through Cloudflare. |
|
|
90
|
+
| `CERBER_GEOIP_DATABASE` | `None` | Path to a MaxMind GeoIP2/GeoLite2 (or DB-IP) Country or City `.mmdb` file.<br><br>Required by the `"geoip"` source; needs `pip install django-cerber[geoip]`. |
|
|
91
|
+
| `CERBER_PROXY_IP_HEADER` | `None` | Header set by *your* reverse proxy with the address it received the request from, e.g. `"X-Forwarded-For"` or `"X-Real-IP"`. `None` = use `REMOTE_ADDR`.<br><br>Only set this when every request goes through that proxy, otherwise clients can forge it. |
|
|
92
|
+
| `CERBER_PROXY_COUNT` | `1` | Number of your own proxies that append to `CERBER_PROXY_IP_HEADER`.<br><br>For a comma separated list such as `X-Forwarded-For: client, proxy1`, the address is taken this many entries from the right; anything further left was sent by the client and is ignored. |
|
|
93
|
+
| `CERBER_VERIFY_CLOUDFLARE_IP` | `True` | Trust Cloudflare's headers only when the peer address is a Cloudflare IP. |
|
|
94
|
+
| `CERBER_BLOCKED_COUNTRIES` | `()` | ISO-3166 alpha-2 codes whose logins are always blocked, e.g. `("RU", "KP")`.<br><br>Takes precedence over everything else, including impossible-travel exceptions. Use `"T1"` here to block Tor via Cloudflare as well. |
|
|
95
|
+
| `CERBER_BLOCK_TOR` | `False` | Block logins from Tor exit nodes (Cloudflare reports them as `T1`). |
|
|
96
|
+
| `CERBER_LOGOUT_ALL_SESSIONS` | `True` | On a blocked login, also end every other session of that user. |
|
|
97
|
+
| `CERBER_REDIRECT_URL` | `None` | Where blocked browser logins are redirected. Defaults to `LOGIN_URL`. |
|
|
98
|
+
|
|
99
|
+
<!-- CERBER-SETTINGS:END -->
|
|
100
|
+
|
|
101
|
+
## Contributors ✨
|
|
102
|
+
|
|
103
|
+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
104
|
+
|
|
105
|
+
<!-- prettier-ignore-start -->
|
|
106
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
107
|
+
<!-- prettier-ignore-start -->
|
|
108
|
+
<!-- markdownlint-disable -->
|
|
109
|
+
<table>
|
|
110
|
+
<tbody>
|
|
111
|
+
<tr>
|
|
112
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/iddqd1"><img src="https://avatars.githubusercontent.com/u/472451?v=4&size=80" width="80px" height="80px" alt="Tomasz Drobiszewski"/><br /><sub><b>Tomasz Drobiszewski</b></sub></a><br /><a href="https://github.com/iddqd1/django-cerber/commits?author=iddqd1" title="Code">💻</a> <a href="https://github.com/iddqd1/django-cerber/commits?author=iddqd1" title="Documentation">📖</a> <a href="#ideas-iddqd1" title="Ideas, Planning, & Feedback">🤔</a> <a href="#maintenance-iddqd1" title="Maintenance">🚧</a> <a href="https://github.com/iddqd1/django-cerber/commits?author=iddqd1" title="Tests">⚠️</a></td>
|
|
113
|
+
</tr>
|
|
114
|
+
</tbody>
|
|
115
|
+
</table>
|
|
116
|
+
|
|
117
|
+
<!-- markdownlint-restore -->
|
|
118
|
+
<!-- prettier-ignore-end -->
|
|
119
|
+
|
|
120
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
121
|
+
<!-- prettier-ignore-end -->
|
|
122
|
+
|
|
123
|
+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
124
|
+
|
|
125
|
+
## Credits
|
|
126
|
+
|
|
127
|
+
[](https://github.com/copier-org/copier)
|
|
128
|
+
|
|
129
|
+
This package was created with
|
|
130
|
+
[Copier](https://copier.readthedocs.io/) and the
|
|
131
|
+
[browniebroke/pypackage-template](https://github.com/browniebroke/pypackage-template)
|
|
132
|
+
project template.
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "setuptools.build_meta"
|
|
3
|
+
requires = [ "setuptools>=77.0.3" ]
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django-cerber"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Login anomaly detection for Django: country changes, Tor and session revocation."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Tomasz Drobiszewski", email = "tdrobiszewski@gmail.com" },
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.11"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
17
|
+
"Framework :: Django",
|
|
18
|
+
"Framework :: Django :: 5.2",
|
|
19
|
+
"Framework :: Django :: 6.0",
|
|
20
|
+
"Framework :: Django :: 6.1",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Natural Language :: English",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Programming Language :: Python :: 3.14",
|
|
29
|
+
"Programming Language :: Python :: 3.15",
|
|
30
|
+
"Topic :: Software Development :: Libraries",
|
|
31
|
+
]
|
|
32
|
+
dependencies = [
|
|
33
|
+
"django>=5.2",
|
|
34
|
+
"django-model-utils>=5",
|
|
35
|
+
]
|
|
36
|
+
optional-dependencies.geoip = [
|
|
37
|
+
"geoip2>=5.4",
|
|
38
|
+
]
|
|
39
|
+
urls."Bug Tracker" = "https://github.com/iddqd1/django-cerber/issues"
|
|
40
|
+
urls.Changelog = "https://github.com/iddqd1/django-cerber/blob/main/CHANGELOG.md"
|
|
41
|
+
urls.documentation = "https://django-cerber.readthedocs.io"
|
|
42
|
+
urls.repository = "https://github.com/iddqd1/django-cerber"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"coverage",
|
|
47
|
+
"django-stubs>=6.0.9",
|
|
48
|
+
"geoip2>=5.4",
|
|
49
|
+
"mypy>=2.3",
|
|
50
|
+
"pytest==9.*",
|
|
51
|
+
"pytest-django==4.*",
|
|
52
|
+
"sybil[pytest]",
|
|
53
|
+
]
|
|
54
|
+
docs = [
|
|
55
|
+
"furo; python_version>='3.14'",
|
|
56
|
+
"myst-parser; python_version>='3.14'",
|
|
57
|
+
"sphinx==9.*; python_version>='3.14'",
|
|
58
|
+
"sphinx-autobuild; python_version>='3.14'",
|
|
59
|
+
]
|
|
60
|
+
django52 = [ "django>=5.2a1,<6" ]
|
|
61
|
+
django60 = [ "django>=6.0a1,<6.1; python_version>='3.12'" ]
|
|
62
|
+
django61 = [ "django>=6.1a1,<6.2; python_version>='3.12'" ]
|
|
63
|
+
|
|
64
|
+
[tool.setuptools]
|
|
65
|
+
package-data.django_cerber = [ "templates/**/*.html" ]
|
|
66
|
+
|
|
67
|
+
[tool.uv]
|
|
68
|
+
conflicts = [
|
|
69
|
+
[
|
|
70
|
+
{ group = "django52" },
|
|
71
|
+
{ group = "django60" },
|
|
72
|
+
{ group = "django61" },
|
|
73
|
+
],
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
[tool.ruff]
|
|
77
|
+
line-length = 88
|
|
78
|
+
lint.select = [
|
|
79
|
+
"B", # flake8-bugbear
|
|
80
|
+
"C4", # flake8-comprehensions
|
|
81
|
+
"D", # flake8-docstrings
|
|
82
|
+
"E", # pycodestyle
|
|
83
|
+
"F", # pyflake
|
|
84
|
+
"I", # isort
|
|
85
|
+
"RUF", # ruff specific
|
|
86
|
+
"S", # flake8-bandit
|
|
87
|
+
"UP", # pyupgrade
|
|
88
|
+
"W", # pycodestyle
|
|
89
|
+
]
|
|
90
|
+
lint.ignore = [
|
|
91
|
+
"D100", # Missing docstring in public module
|
|
92
|
+
"D104", # Missing docstring in public package
|
|
93
|
+
"D107", # Missing docstring in `__init__`
|
|
94
|
+
"D203", # 1 blank line required before class docstring
|
|
95
|
+
"D212", # Multi-line docstring summary should start at the first line
|
|
96
|
+
"D401", # First line of docstring should be in imperative mood
|
|
97
|
+
]
|
|
98
|
+
lint.per-file-ignores."conftest.py" = [ "D100" ]
|
|
99
|
+
lint.per-file-ignores."docs/conf.py" = [ "D100" ]
|
|
100
|
+
lint.per-file-ignores."setup.py" = [ "D100" ]
|
|
101
|
+
lint.per-file-ignores."src/django_cerber/migrations/*" = [ "D101", "E501", "RUF012" ]
|
|
102
|
+
lint.per-file-ignores."tests/**/*" = [
|
|
103
|
+
"D100",
|
|
104
|
+
"D101",
|
|
105
|
+
"D102",
|
|
106
|
+
"D103",
|
|
107
|
+
"D104",
|
|
108
|
+
"S101",
|
|
109
|
+
]
|
|
110
|
+
lint.isort.known-first-party = [ "django_cerber", "tests" ]
|
|
111
|
+
|
|
112
|
+
[tool.pyproject-fmt]
|
|
113
|
+
max_supported_python = "3.15"
|
|
114
|
+
|
|
115
|
+
[tool.mypy]
|
|
116
|
+
mypy_path = "src/"
|
|
117
|
+
exclude = [
|
|
118
|
+
"docs/.*",
|
|
119
|
+
"setup.py",
|
|
120
|
+
]
|
|
121
|
+
disallow_any_generics = true
|
|
122
|
+
disallow_untyped_defs = true
|
|
123
|
+
disallow_incomplete_defs = true
|
|
124
|
+
check_untyped_defs = true
|
|
125
|
+
warn_unused_ignores = true
|
|
126
|
+
warn_unreachable = true
|
|
127
|
+
plugins = [ "mypy_django_plugin.main" ]
|
|
128
|
+
overrides = [
|
|
129
|
+
{ module = "tests.*", allow_untyped_defs = true },
|
|
130
|
+
{ module = "docs.*", ignore_errors = true },
|
|
131
|
+
]
|
|
132
|
+
no_implicit_optional = true
|
|
133
|
+
show_error_codes = true
|
|
134
|
+
|
|
135
|
+
[tool.django-stubs]
|
|
136
|
+
django_settings_module = "tests.settings"
|
|
137
|
+
|
|
138
|
+
[tool.pytest]
|
|
139
|
+
addopts = [
|
|
140
|
+
"-v",
|
|
141
|
+
"-Wonce",
|
|
142
|
+
"-p no:doctest",
|
|
143
|
+
]
|
|
144
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
145
|
+
minversion = "9.0"
|
|
146
|
+
pythonpath = [ "src" ]
|
|
147
|
+
|
|
148
|
+
[tool.coverage]
|
|
149
|
+
run.source = [ "django_cerber" ]
|
|
150
|
+
run.branch = true
|
|
151
|
+
run.parallel = true
|
|
152
|
+
paths.source = [
|
|
153
|
+
"src",
|
|
154
|
+
".tox/**/site-packages",
|
|
155
|
+
]
|
|
156
|
+
report.exclude_lines = [
|
|
157
|
+
"@overload",
|
|
158
|
+
'if __name__ == "__main__":',
|
|
159
|
+
"if TYPE_CHECKING",
|
|
160
|
+
"pragma: no cover",
|
|
161
|
+
"raise NotImplementedError",
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
[tool.semantic_release]
|
|
165
|
+
allow_zero_version = true
|
|
166
|
+
version_variables = [
|
|
167
|
+
"docs/conf.py:release",
|
|
168
|
+
"src/django_cerber/__init__.py:__version__",
|
|
169
|
+
]
|
|
170
|
+
version_toml = [ "pyproject.toml:project.version" ]
|
|
171
|
+
build_command = """
|
|
172
|
+
pip install uv
|
|
173
|
+
uv lock
|
|
174
|
+
git add uv.lock
|
|
175
|
+
uv build
|
|
176
|
+
"""
|
|
177
|
+
branches.main.match = "main"
|
|
178
|
+
branches.noop.match = "(?!main$)"
|
|
179
|
+
branches.noop.prerelease = true
|
|
180
|
+
changelog.environment.keep_trailing_newline = true
|
|
181
|
+
changelog.exclude_commit_patterns = [
|
|
182
|
+
"chore(?:\\([^)]*?\\))?: .+",
|
|
183
|
+
"ci(?:\\([^)]*?\\))?: .+",
|
|
184
|
+
"refactor(?:\\([^)]*?\\))?: .+",
|
|
185
|
+
"style(?:\\([^)]*?\\))?: .+",
|
|
186
|
+
"test(?:\\([^)]*?\\))?: .+",
|
|
187
|
+
"build\\((?!deps\\): .+)",
|
|
188
|
+
"Merged? .*",
|
|
189
|
+
"Initial [Cc]ommit.*", # codespell:ignore
|
|
190
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.1"
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Admin actions for logging users out.
|
|
3
|
+
|
|
4
|
+
They are already on the Cerber admin pages. To have them on your user admin
|
|
5
|
+
too::
|
|
6
|
+
|
|
7
|
+
from django_cerber.actions import logout_all_users_action, logout_selected_users
|
|
8
|
+
|
|
9
|
+
class MyUserAdmin(UserAdmin):
|
|
10
|
+
actions = [logout_selected_users, logout_all_users_action]
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from django.contrib import admin, messages
|
|
18
|
+
from django.contrib.admin import helpers
|
|
19
|
+
from django.contrib.auth import get_user_model
|
|
20
|
+
from django.db.models import QuerySet
|
|
21
|
+
from django.http import HttpRequest
|
|
22
|
+
from django.template.response import TemplateResponse
|
|
23
|
+
from django.utils.translation import gettext_lazy as _
|
|
24
|
+
from django.utils.translation import ngettext
|
|
25
|
+
|
|
26
|
+
from .sessions import logout_all_users, logout_users
|
|
27
|
+
|
|
28
|
+
CONFIRM_TEMPLATE = "admin/django_cerber/logout_all_confirmation.html"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _current_session_key(request: HttpRequest) -> str | None:
|
|
32
|
+
session = getattr(request, "session", None)
|
|
33
|
+
return session.session_key if session is not None else None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _user_ids(queryset: QuerySet[Any]) -> list[Any]:
|
|
37
|
+
"""User PKs from a queryset of users or of objects with a ``user`` FK."""
|
|
38
|
+
if issubclass(queryset.model, get_user_model()):
|
|
39
|
+
return list(queryset.values_list("pk", flat=True))
|
|
40
|
+
return list(queryset.order_by().values_list("user_id", flat=True).distinct())
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@admin.action(description=_("Log out selected users"), permissions=["change"])
|
|
44
|
+
def logout_selected_users(
|
|
45
|
+
modeladmin: admin.ModelAdmin[Any], request: HttpRequest, queryset: QuerySet[Any]
|
|
46
|
+
) -> None:
|
|
47
|
+
"""End every session of the selected users (or the users of the rows)."""
|
|
48
|
+
user_ids = _user_ids(queryset)
|
|
49
|
+
ended = logout_users(user_ids, keep_session_key=_current_session_key(request))
|
|
50
|
+
modeladmin.message_user(
|
|
51
|
+
request,
|
|
52
|
+
ngettext(
|
|
53
|
+
"Logged out %(users)d user(s), %(count)d session ended.",
|
|
54
|
+
"Logged out %(users)d user(s), %(count)d sessions ended.",
|
|
55
|
+
ended,
|
|
56
|
+
)
|
|
57
|
+
% {"users": len(user_ids), "count": ended},
|
|
58
|
+
messages.SUCCESS,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@admin.action(description=_("Log out ALL users"), permissions=["change"])
|
|
63
|
+
def logout_all_users_action(
|
|
64
|
+
modeladmin: admin.ModelAdmin[Any], request: HttpRequest, queryset: QuerySet[Any]
|
|
65
|
+
) -> TemplateResponse | None:
|
|
66
|
+
"""
|
|
67
|
+
End the sessions of every user, after a confirmation page.
|
|
68
|
+
|
|
69
|
+
The selection is ignored. Superusers only; your own session is kept.
|
|
70
|
+
"""
|
|
71
|
+
if not request.user.is_superuser:
|
|
72
|
+
modeladmin.message_user(
|
|
73
|
+
request, _("Only superusers can log out all users."), messages.ERROR
|
|
74
|
+
)
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
if request.POST.get("post") == "yes":
|
|
78
|
+
ended = logout_all_users(keep_session_key=_current_session_key(request))
|
|
79
|
+
modeladmin.message_user(
|
|
80
|
+
request,
|
|
81
|
+
ngettext(
|
|
82
|
+
"Logged out all users, %(count)d session ended.",
|
|
83
|
+
"Logged out all users, %(count)d sessions ended.",
|
|
84
|
+
ended,
|
|
85
|
+
)
|
|
86
|
+
% {"count": ended},
|
|
87
|
+
messages.SUCCESS,
|
|
88
|
+
)
|
|
89
|
+
return None
|
|
90
|
+
|
|
91
|
+
opts = modeladmin.model._meta
|
|
92
|
+
context = {
|
|
93
|
+
**modeladmin.admin_site.each_context(request),
|
|
94
|
+
"title": _("Log out all users?"),
|
|
95
|
+
"opts": opts,
|
|
96
|
+
"queryset": queryset,
|
|
97
|
+
"action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
|
|
98
|
+
"action_name": request.POST.get("action", "logout_all_users_action"),
|
|
99
|
+
"select_across": request.POST.get("select_across", "0"),
|
|
100
|
+
"media": modeladmin.media,
|
|
101
|
+
}
|
|
102
|
+
return TemplateResponse(request, CONFIRM_TEMPLATE, context)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from django.contrib import admin
|
|
6
|
+
from django.http import HttpRequest
|
|
7
|
+
|
|
8
|
+
from .actions import logout_all_users_action, logout_selected_users
|
|
9
|
+
from .models import ImpossibleTravelException, LoginEvent
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
_ModelAdmin = admin.ModelAdmin[LoginEvent]
|
|
13
|
+
_ExceptionAdmin = admin.ModelAdmin[ImpossibleTravelException]
|
|
14
|
+
else:
|
|
15
|
+
_ModelAdmin = _ExceptionAdmin = admin.ModelAdmin
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@admin.register(LoginEvent)
|
|
19
|
+
class LoginEventAdmin(_ModelAdmin):
|
|
20
|
+
"""Audit log of logins."""
|
|
21
|
+
|
|
22
|
+
list_display = ("id", "created", "user", "country", "ip", "blocked", "reason")
|
|
23
|
+
list_filter = ("blocked", "reason", "country")
|
|
24
|
+
search_fields = ("ip", "session_key")
|
|
25
|
+
date_hierarchy = "created"
|
|
26
|
+
raw_id_fields = ("user",)
|
|
27
|
+
readonly_fields = ("created", "modified")
|
|
28
|
+
actions = (logout_selected_users, logout_all_users_action)
|
|
29
|
+
|
|
30
|
+
def has_add_permission(self, request: HttpRequest) -> bool:
|
|
31
|
+
"""Login events are created by the guard only."""
|
|
32
|
+
return False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@admin.register(ImpossibleTravelException)
|
|
36
|
+
class ImpossibleTravelExceptionAdmin(_ExceptionAdmin):
|
|
37
|
+
"""Per-user countries that don't trigger impossible-travel blocks."""
|
|
38
|
+
|
|
39
|
+
list_display = ("id", "user", "country", "valid_until", "note", "created")
|
|
40
|
+
list_filter = ("country",)
|
|
41
|
+
search_fields = ("country", "note")
|
|
42
|
+
raw_id_fields = ("user",)
|
|
43
|
+
readonly_fields = ("created", "modified")
|
|
44
|
+
actions = (logout_selected_users, logout_all_users_action)
|