How barndalion/authbarn works
AuthBarn is a Python authentication library, not an application server. Its main boundary is a reusable package that other apps call directly, especially web APIs like Flask or FastAPI. The package exposes a small public surface from authbarn/__init__.py, centered on an Action class and helper setup functions.
The core runtime lives in authbarn/authbarn.py. This is where the authentication and authorization use cases are implemented: registration, login, password reset, token generation, role management, user management, permission checks, and bulk user creation. These methods return structured dictionaries so the library can be dropped into API handlers without translation.
Authentication is stateless and token-based. User login/registration produces JWTs, and protected operations accept a token parameter that is validated before privileged actions proceed. Passwords are hashed with bcrypt before storage, so the system separates credential verification from token verification.
Persistence is MySQL-backed. The README says the project migrated from JSON to MySQL in version 0.2.8, so the architectural data store is now an external database rather than local files. Connection and environment setup are handled in authbarn/config.py, which likely reads .env values for host, port, user, password, database, and secret key.
Authorization is role-based and permission-driven. The static file authbarn/data/permission.json defines role-to-permission mappings such as Admin and User, and the runtime methods add or remove roles and check whether a token’s role permits an action. This creates a clear split between identity verification and capability authorization.
Logging is a separate subsystem in authbarn/logger.py. The package maintains two log outputs under authbarn/logfiles/: one for general system activity and one for user-facing or custom events. Logging is configurable through enable_logging, so the same library can run quietly in production or verbosely in development.
Error handling is controlled by dev_mode. In production mode the library returns failure dictionaries, while in developer mode it raises exceptions. That means the consumer application owns the HTTP/UI error presentation, and AuthBarn only decides whether to signal failures as data or as exceptions.
Configuration is environment-driven. The README describes a one-time helper, write_credentials_to_env, which generates a .env file and secret key. That makes the secret and database credentials external to the codebase, which is important for diagramming the deployment boundary: app code reads configuration, but does not embed secrets.
The package layout is intentionally small and modular: authbarn.py for business logic, config.py for environment/database wiring, logger.py for observability, and data/permission.json for permission definitions. There is no separate API layer, UI, or background worker in this repository; those would be external consumers of the library.
Operationally, the main data flow is: caller invokes library method → auth logic checks token/permissions → config opens MySQL connection and/or reads env values → bcrypt/JWT operations are applied → result is returned as a dictionary, with optional logging to file. This makes the library easy to embed in larger systems while keeping its internal responsibilities narrow.
Packaging is standard Python distribution tooling (pyproject.toml, root setup.py, package-level pyproject.toml). The repo is meant to be installed as a dependency, not deployed as a standalone service.
One architectural detail to capture in a diagram: permission.json and the log files are local repository assets, but they behave differently. Permissions are part of runtime policy, while logs are runtime output. The former influences authorization decisions; the latter is purely observational.
Main entry points: authbarn.py Auth Engine · __init__.py Package API · pyproject.toml Python Dist · logger.py Logger
Components 16
Public API
Reusable package surface and install entry points
- Package API · Public exports from the library root
authbarn/__init__.py - Python Dist · Project packaging and distribution metadata
pyproject.toml
Auth Core
Primary authentication and authorization runtime
- Auth Engine · Implements registration, login, reset, roles, permissions, and user operations
authbarn/authbarn.py - dev_mode · Switches between returned failure dictionaries and raised exceptions
- Auth Flows · Registration, login, token issuance, and password reset
- Access Control · Role management, permission checks, and protected operations
- User Admin · User management and bulk user creation
Infrastructure
Environment, database, and credential wiring
- Setup Helpers · Helpers for one-time environment and credential setup
authbarn/config.py - Env Vars · External configuration for database credentials and secret key
Policy & Observability
Permission policy and logging outputs
- Logger · Configurable file-based logging for system and user events
authbarn/logger.py - Permission Policy · Role-to-permission mappings used by authorization checks
authbarn/data/permission.json - Log Files · General and user-facing log outputs stored by the package
authbarn/logfiles/general_logs.log
External Boundary
Consumers and dependencies outside the repository
- MySQL · External persistence store for user and auth state
- bcrypt · Password hashing and verification dependency
- JWT · Stateless access token generation and validation
- Host Apps · Flask, FastAPI, or other applications embedding the library
Connections 17
- Host Apps → Package API: imports · Consumers import the package surface
- Package API → Auth Engine: exposes · Public API re-exports the main action surface
- Auth Engine → Auth Flows: implements · Handles registration, login, reset, and token issuance
- Auth Engine → Access Control: implements · Handles role and permission enforcement
- Auth Engine → User Admin: implements · Handles user management workflows
- Auth Engine → dev_mode: branches on · Selects exception or failure-dict behavior
- Auth Flows → bcrypt: hashes · Uses bcrypt for password hashing and verification
- Auth Flows → JWT: issues tokens · Creates JWTs during authentication flows
- Access Control → JWT: validates · Checks token authenticity before privileged actions
- Access Control → Permission Policy: reads policy · Looks up role-to-permission rules
- Auth Engine → MySQL: persists to · Stores and retrieves user/auth data from MySQL
- Setup Helpers → Env Vars: writes/reads · Creates or consumes environment credentials
- Auth Engine → Env Vars: reads config · Consumes runtime database and secret settings
- Auth Engine → Logger: logs via · Emits operational and user activity logs
- Logger → Log Files: writes · Appends records to general and user log files
- Python Dist → Package API: packages · Distribution metadata includes the library package
- Python Dist → Auth Engine: packages · Build/install tooling ships the core module