Skip to main content

Django authentication backends and views for Vector-generated apps

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

govector-uga-auth

Django authentication backends and views for Vector-generated apps. Handles JWT verification from Vector's auth proxy, session cookie management, and dev-mode auto-authentication.

Install

pip install govector-uga-auth

Setup

Authentication Backends

Add to your Django settings:

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "govector_auth.JWTCookieAuthentication",
        "govector_auth.DevAutoAuthentication",
    ],
}
  • JWTCookieAuthentication reads JWT from HttpOnly cookies with CSRF enforcement
  • DevAutoAuthentication provides a fallback dev user when auth is not configured (when VECTOR_AUTH_PROXY_URL is not set)

URL Configuration

Add the auth views to your urls.py:

from govector_auth import AuthTokenView, TokenRefreshView, CurrentUserView, LogoutView

urlpatterns = [
    path("api/accounts/auth/token", AuthTokenView.as_view()),
    path("api/accounts/auth/refresh", TokenRefreshView.as_view()),
    path("api/accounts/me/", CurrentUserView.as_view()),
    path("api/accounts/logout/", LogoutView.as_view()),
]

Required Settings

# Cookie names
ACCESS_TOKEN_COOKIE = "access_token"
REFRESH_TOKEN_COOKIE = "refresh_token"

# Cookie config
ACCESS_TOKEN_COOKIE_MAX_AGE = 60 * 5  # 5 minutes
ACCESS_TOKEN_COOKIE_HTTPONLY = True
ACCESS_TOKEN_COOKIE_SECURE = True  # False for local dev
ACCESS_TOKEN_COOKIE_SAMESITE = "Lax"
ACCESS_TOKEN_COOKIE_PATH = "/"

REFRESH_TOKEN_COOKIE_MAX_AGE = 60 * 60 * 24 * 7  # 7 days
REFRESH_TOKEN_COOKIE_HTTPONLY = True
REFRESH_TOKEN_COOKIE_SECURE = True  # False for local dev
REFRESH_TOKEN_COOKIE_SAMESITE = "Lax"
REFRESH_TOKEN_COOKIE_PATH = "/"

Required Environment Variables

VECTOR_AUTH_PROXY_URL=https://auth.govector.ai  # Vector's hosted auth proxy
DEV_USER_EMAIL=dev@localhost                     # Fallback email when auth is disabled

User Model

Your User model should include these fields for full compatibility:

class User(AbstractBaseUser):
    email = models.EmailField(unique=True)
    first_name = models.CharField(max_length=150, blank=True)
    last_name = models.CharField(max_length=150, blank=True)
    picture = models.URLField(max_length=500, blank=True, default="")
    vector_uga_user_id = models.CharField(max_length=255, blank=True, default="")
    is_active = models.BooleanField(default=True)

    @property
    def full_name(self) -> str:
        return f"{self.first_name} {self.last_name}".strip() or self.email

Auth Flow

  1. Frontend POSTs JWT from auth proxy to AuthTokenView
  2. View fetches RS256 public key from VECTOR_AUTH_PROXY_URL/.well-known/jwks.json
  3. Verifies JWT signature, extracts user claims
  4. Creates or updates local User record
  5. Issues SimpleJWT access + refresh tokens as HttpOnly cookies
  6. Subsequent requests authenticated via JWTCookieAuthentication
  7. TokenRefreshView rotates tokens when access token expires

API

JWTCookieAuthentication

DRF authentication backend. Reads JWT from the ACCESS_TOKEN_COOKIE cookie, falls back to Authorization header. Enforces CSRF on unsafe methods when using cookies.

DevAutoAuthentication

DRF authentication backend. When VECTOR_AUTH_PROXY_URL is not set, auto-authenticates with a shared dev user. When auth is enabled, returns None (passes through to 401).

Views

View Method Path Description
AuthTokenView POST /api/accounts/auth/token Exchange auth proxy JWT for session
TokenRefreshView POST /api/accounts/auth/refresh Rotate refresh token
CurrentUserView GET /api/accounts/me/ Get current user
LogoutView POST /api/accounts/logout/ Clear auth cookies

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

govector_uga_auth-0.1.4.tar.gz (48.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

govector_uga_auth-0.1.4-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file govector_uga_auth-0.1.4.tar.gz.

File metadata

  • Download URL: govector_uga_auth-0.1.4.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for govector_uga_auth-0.1.4.tar.gz
Algorithm Hash digest
SHA256 fc5a90dc44c9a0a35e1f1e6fde090de1657b6ff3e29140c8e2e66be3e1ef829a
MD5 e44f4f53864a80b01b8dba821ba66bfd
BLAKE2b-256 9108435bcdd3c7c97fd11cea7ceaec2a0caca22e54f07332ee162570e6de40ee

See more details on using hashes here.

File details

Details for the file govector_uga_auth-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: govector_uga_auth-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for govector_uga_auth-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6a026a9846b7f81f4831dff5514ece9098dac6f0326751e89db845d95c360104
MD5 19a6ff4c68c7a8038a8f2586b7d792c2
BLAKE2b-256 92e837a44e08f6f9fd01ad06f3c733fd7270eeed0f46d5f9acb87f40428c43c9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page