Metadata-Version: 2.5
Name: django-auth-events
Version: 0.1.1
Summary: Auth session event consumer for Django — bridges streambus events with token invalidation
Author-email: Jose Meira <jmeiracorbal@gmail.com>
License: MIT License
        
        Copyright (c) 2026 dankkomcg
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: authentication,django,events,jwt,streambus
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.12
Requires-Dist: django>=4.2
Requires-Dist: streambus>=0.2.0
Description-Content-Type: text/markdown

# django-auth-events

[![PyPI version](https://img.shields.io/pypi/v/django-auth-events.svg)](https://pypi.org/project/django-auth-events/)
[![Python](https://img.shields.io/pypi/pyversions/django-auth-events.svg)](https://pypi.org/project/django-auth-events/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Tests](https://github.com/jmeiracorbal/django-auth-events/actions/workflows/tests.yml/badge.svg)](https://github.com/jmeiracorbal/django-auth-events/actions)

Auth session event consumer for Django. Bridges event-driven session invalidation with JWT token validation.

Combines `streambus` (event consumption) with `django-auth-external` (token validation) to reject tokens whose sessions have been explicitly invalidated — without tight coupling between services.

## how it works

One service publishes a `SessionInvalidatedEvent` to an event stream whenever a user's access changes. Each Django service runs `consume_auth_events`, which receives those events and stores the invalidation state in Django's cache. The `AuthEventsMiddleware` checks that state on every request.

Token validation and invalidation are both in the middleware — no Redis dependency in the library itself. The cache backend is whatever you configure in Django's `CACHES` setting.

## install

```bash
pip install django-auth-events
```

## setup

Add the middleware (replaces `AuthExternalMiddleware` from `django-auth-external`):

```python
MIDDLEWARE = [
    ...
    "django_auth_events.middleware.AuthEventsMiddleware",
]
```

Configure auth and event settings:

```python
AUTH_EXTERNAL = {
    "SECRET_KEY": "your-secret-key",
    "CLAIMS": ["user_id", "email"],
}

AUTH_EVENTS = {
    "STREAM": "auth:session:events",
    "GROUP": "my-service",
    "REDIS_URL": "redis://localhost:6379",
    "CACHE_TTL": 86400,
}
```

Configure the cache backend (Redis recommended for multi-process deployments):

```python
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://localhost:6379",
    }
}
```

Run the consumer:

```bash
python manage.py consume_auth_events
```

## events

Publish a `SessionInvalidatedEvent` from any service to invalidate a user's active tokens:

```python
from django_auth_events import SessionInvalidatedEvent
from streambus import EventPublisher
from streambus.transports.redis_streams import RedisStreamsTransport
from streambus.config import RedisConfig

transport = RedisStreamsTransport(RedisConfig(url="redis://localhost:6379"))
publisher = EventPublisher(stream="auth:session:events", transport=transport)
publisher.publish(SessionInvalidatedEvent(event_type="session.invalidated", user_id="abc-123"))
```

## configuration

| Key | Default | Description |
|-----|---------|-------------|
| `STREAM` | `"auth:session:events"` | Redis Stream key to consume from. |
| `GROUP` | `"django-auth-events"` | Consumer group name. Use a unique name per service. |
| `REDIS_URL` | `"redis://localhost:6379"` | Redis URL for the event stream. |
| `CACHE_TTL` | `86400` | Invalidation record TTL in seconds (24 hours). |

## license

MIT
