Gateway Defender
gateway-defender is a small, reusable Django app that provides authentication helpers (login/logout views), a protected portal view, session timeout handling, and packaged templates/static assets. The PyPI distribution name is gateway-defender; the importable Python package is gateway_defender.
Development
This project uses uv for dependency management. To set up a local development environment:
uv sync
This creates a .venv and installs the package (editable) along with the dev dependency group (build, twine) declared in pyproject.toml.
Run the test suite with:
uv run devsite/manage.py test gateway_defender
When preparing a release:
- Update
pyproject.tomlversion and metadata. - Build with
uv build. - Upload with
uv run twine upload dist/*.
Quick start
- Add to
INSTALLED_APPSin your Django settings:
INSTALLED_APPS = [
# ...
"django.contrib.sites",
"gateway_defender",
]
- Add the middleware (required for multi-site support):
MIDDLEWARE = [
# ...
"django.contrib.sites.middleware.CurrentSiteMiddleware",
]
- Include the URLs in your project
urls.py:
from django.urls import include, path
urlpatterns = [
path("", include("gateway_defender.urls")),
]
- Configure the portal template to render after successful login:
Add this inside your settings.py
# Defaults to "gateway_defender/portal.html"
GATEWAY_PORTAL_TEMPLATE = "your_app/portal.html"
Ensure the template exists within one of the template directories defined in TEMPLATES in your settings.
- Run migrations:
python manage.py migrate
- Routes provided by the app:
GET /— login page (usesgateway_defender/gateway.html)GET /portal/— protected portal pageGET /logout/— logout and redirect toindex
Multi-Site Configuration (v0.1.4+)
Problem Statement: We share the same PostgreSQL DB instance across different Django projects: tarot_juicer, hypno_mixer, and a CV_Portfolio, all three of which are hosted and deployed to Heroku. When we install this gateway-defender package for all three, in the Admin Dashboard for any one project, originally when we toggled the is_protected feature boolean switch to “Off”, that would remove the protective gateway for all three at the same time. That’s a major problem. If is_protected is set to Off on one project, it needs to respect the fact that the other two projects could be set to "On".
Solution: Introduced in gateway-defender v0.1.4 the app now uses Django's highly advanced built-in sites framework so as to enable us to configure different authentication settings per domain while sharing the same database.
Configuration and Usage
Important: You must have at least one Site configured. Create the Site record(s) in the admin panel. Without a valid Site, Django's Sites framework will likely raise a Site.DoesNotExist error when resolving the current site. When installing gateway-defender for the first time in a new Django project, to avoid this error you may need to temporarily add a line inside settings.py such as SITE_ID = 1 (or any such integer) locally for both database instances to enable access into Django Admin Dashboard. This is just a work around/fallback. Once you establish access you can then proceed with the instructions below. After completing the steps, remember to delete the SITE_ID = 1 from settings.py.
Steps
- Create a Site from the Admin Dashboard (usually at the bottom of the menu panel) for each domain you want to support. Then click "Add Site" in the top right corner.
- Two fields are available: For
Domain name, for local testing in the development environment, add:localhost:8000or127.0.0.1:8000. TheDisplay namefield is used for annotating for your personal reference. - Similar to your local configuration, continue adding sites with your production database configured for your one (or multiple) production domain(s).
- Keep in mind that including a
https://prefix or even a trailing/at the end could cause Django to throw a 404 so avoid those characters. Plus for production domains, you will likely need to create two sites: One withwwwprefix and one without any prefix, depending on how you you've configured your domain with your registrar.
- Keep in mind that including a
- In the Admin Dashboard, add an
AuthToggle(top left menu panel). In theSitedropdown box, select the domain and then choose your dynamic options such as "Is protected", "Gateway image" (Placecats, Wings, Muerte, or None), and/or "Nuclear", and enter theEmailaddress. All of these options will be assigned to the specified Site. - It's worth reiterating how important it is to remove the intial usage of
SITE_ID = 1fallback insettings.pyonce all custom data is entered, especially in production.
API / Files included
Key modules included in the package:
gateway_defender.models—AuthToggle,PassPhrasegateway_defender.views—Gateway(login view) andEndSession(logout view)gateway_defender.urls— URL patterns included abovegateway_defender.custom_decorator—protected_redirectdecoratorgateway_defender/templates/— packaged templates (gateway_defender/gateway.html, gateway_defender/logged_out.html)gateway_defender/static/— CSS and images
Refer to module docstrings and the source for details.
Compatibility
- Python: 3.10 — 3.13
- Django: >= 4.2 (tested with Django 5.2)
Contributing
Contributions are welcome. Please open issues or PRs and include tests for new behavior. Keep public APIs stable where possible.
License
MIT — see the LICENSE file included in the repository.
Project & PyPI
PyPI package name: gateway-defender
Python package (import): gateway_defender
Update the repository or PyPI URLs here if you host the project on GitHub or another platform.
Metadata
Release files for gateway-defender 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gateway_defender-0.4.3.tar.gz | 197.3 kB | Without provenance | Details for gateway_defender-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gateway_defender-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for gateway_defender-0.4.3-py3-none-any.whl |
Total release size: 414.7 kB
Release files / gateway_defender-0.4.3.tar.gz
| Download URL | gateway_defender-0.4.3.tar.gz |
|---|---|
| Size | 197.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0c312cf4bfba61aa382b58080f9a3894ddb4793eabcd4458f9bb764605bf628
|
|
BLAKE2b-256 checksum How to use checksums |
fce29a65940db1fc3be386738f78dbc1f37d1670d8f835815e6fdc0615f25efa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.10
|
Release files / gateway_defender-0.4.3-py3-none-any.whl
| Download URL | gateway_defender-0.4.3-py3-none-any.whl |
|---|---|
| Size | 217.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a876725655df0f07cecb1f58057702fa3edf31bb562b15b01c034b74cde8ab4
|
|
BLAKE2b-256 checksum How to use checksums |
d2bb529b40534ff46f09cd8111f8e1d45c88baeae8cda16c878452a4a8113e2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.10
|
Security for gateway-defender 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.