# barndalion/authbarn architecture

> Architecture diagram of the GitHub repository barndalion/authbarn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/barndalion/authbarn
- Repository: https://github.com/barndalion/authbarn

## Overview

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/authbarn.py`](https://github.com/barndalion/authbarn/blob/main/authbarn/authbarn.py) (Auth Engine), [`authbarn/__init__.py`](https://github.com/barndalion/authbarn/blob/main/authbarn/__init__.py) (Package API), [`pyproject.toml`](https://github.com/barndalion/authbarn/blob/main/pyproject.toml) (Python Dist), [`authbarn/logger.py`](https://github.com/barndalion/authbarn/blob/main/authbarn/logger.py) (Logger)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_public["Public API"]
  node_n_init["Package API<br/>python package<br/>[__init__.py]"]
  node_n_pkg["Python Dist<br/>packaging<br/>[pyproject.toml]"]
end

subgraph group_grp_core["Auth Core"]
  node_n_authbarn["Auth Engine<br/>service module<br/>[authbarn.py]"]
  node_n_devmode(("dev_mode<br/>error mode"))
  node_n_reglogin["Auth Flows<br/>use-case cluster"]
  node_n_access["Access Control<br/>use-case cluster"]
  node_n_users["User Admin<br/>use-case cluster"]
end

subgraph group_grp_infra["Infrastructure"]
  node_n_setup["Setup Helpers<br/>utility module<br/>[config.py]"]
  node_n_env["Env Vars<br/>runtime config"]
end

subgraph group_grp_policy["Policy & Observability"]
  node_n_logger["Logger<br/>logging module<br/>[logger.py]"]
  node_n_perms["Permission Policy<br/>policy data<br/>[permission.json]"]
  node_n_logs["Log Files<br/>runtime output<br/>[general_logs.log]"]
end

subgraph group_grp_external["External Boundary"]
  node_n_mysql[("MySQL<br/>database")]
  node_n_bcrypt{{"bcrypt<br/>crypto dependency"}}
  node_n_jwt{{"JWT<br/>token dependency"}}
  node_n_apps(("Host Apps<br/>consumer"))
end

node_n_apps -->|"imports"| node_n_init
node_n_init -->|"exposes"| node_n_authbarn
node_n_authbarn -->|"implements"| node_n_reglogin
node_n_authbarn -->|"implements"| node_n_access
node_n_authbarn -->|"implements"| node_n_users
node_n_authbarn -->|"branches on"| node_n_devmode
node_n_reglogin -->|"hashes"| node_n_bcrypt
node_n_reglogin -->|"issues tokens"| node_n_jwt
node_n_access -->|"validates"| node_n_jwt
node_n_access -->|"reads policy"| node_n_perms
node_n_authbarn -->|"persists to"| node_n_mysql
node_n_setup -->|"writes/reads"| node_n_env
node_n_authbarn -->|"reads config"| node_n_env
node_n_authbarn -->|"logs via"| node_n_logger
node_n_logger -->|"writes"| node_n_logs
node_n_pkg -->|"packages"| node_n_init
node_n_pkg -->|"packages"| node_n_authbarn

click node_n_init "https://github.com/barndalion/authbarn/blob/main/authbarn/__init__.py"
click node_n_authbarn "https://github.com/barndalion/authbarn/blob/main/authbarn/authbarn.py"
click node_n_setup "https://github.com/barndalion/authbarn/blob/main/authbarn/config.py"
click node_n_logger "https://github.com/barndalion/authbarn/blob/main/authbarn/logger.py"
click node_n_perms "https://github.com/barndalion/authbarn/blob/main/authbarn/data/permission.json"
click node_n_logs "https://github.com/barndalion/authbarn/blob/main/authbarn/logfiles/general_logs.log"
click node_n_pkg "https://github.com/barndalion/authbarn/blob/main/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_init,node_n_pkg toneBlue
class node_n_authbarn,node_n_devmode,node_n_reglogin,node_n_access,node_n_users toneAmber
class node_n_setup,node_n_env toneMint
class node_n_logger,node_n_perms,node_n_logs toneRose
class node_n_mysql,node_n_bcrypt,node_n_jwt,node_n_apps toneIndigo
```

## Components

### Public API

Reusable package surface and install entry points

- [Package API](https://github.com/barndalion/authbarn/blob/main/authbarn/__init__.py): python package · Public exports from the library root · `authbarn/__init__.py`
- [Python Dist](https://github.com/barndalion/authbarn/blob/main/pyproject.toml): packaging · Project packaging and distribution metadata · `pyproject.toml`

### Auth Core

Primary authentication and authorization runtime

- [Auth Engine](https://github.com/barndalion/authbarn/blob/main/authbarn/authbarn.py): service module · Implements registration, login, reset, roles, permissions, and user operations · `authbarn/authbarn.py`
- **dev_mode**: error mode · Switches between returned failure dictionaries and raised exceptions
- **Auth Flows**: use-case cluster · Registration, login, token issuance, and password reset
- **Access Control**: use-case cluster · Role management, permission checks, and protected operations
- **User Admin**: use-case cluster · User management and bulk user creation

### Infrastructure

Environment, database, and credential wiring

- [Setup Helpers](https://github.com/barndalion/authbarn/blob/main/authbarn/config.py): utility module · Helpers for one-time environment and credential setup · `authbarn/config.py`
- **Env Vars**: runtime config · External configuration for database credentials and secret key

### Policy & Observability

Permission policy and logging outputs

- [Logger](https://github.com/barndalion/authbarn/blob/main/authbarn/logger.py): logging module · Configurable file-based logging for system and user events · `authbarn/logger.py`
- [Permission Policy](https://github.com/barndalion/authbarn/blob/main/authbarn/data/permission.json): policy data · Role-to-permission mappings used by authorization checks · `authbarn/data/permission.json`
- [Log Files](https://github.com/barndalion/authbarn/blob/main/authbarn/logfiles/general_logs.log): runtime output · General and user-facing log outputs stored by the package · `authbarn/logfiles/general_logs.log`

### External Boundary

Consumers and dependencies outside the repository

- **MySQL**: database · External persistence store for user and auth state
- **bcrypt**: crypto dependency · Password hashing and verification dependency
- **JWT**: token dependency · Stateless access token generation and validation
- **Host Apps**: consumer · Flask, FastAPI, or other applications embedding the library

## Connections

- 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
