Metadata-Version: 2.4
Name: ldapgate
Version: 0.2.0
Summary: Lightweight LDAP/AD authentication proxy and FastAPI middleware
Author-email: Anudeep Dhavaleswarapu <anudeepd2@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/anudeepd/ldapgate
Project-URL: Repository, https://github.com/anudeepd/ldapgate
Project-URL: Issues, https://github.com/anudeepd/ldapgate/issues
Keywords: ldap,authentication,proxy,fastapi,middleware,webdav,active-directory,sso
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Security
Classifier: Topic :: System :: Networking
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi<0.116,>=0.111.0
Requires-Dist: starlette<0.47,>=0.37.2
Requires-Dist: anyio<4.7,>=4.0
Requires-Dist: uvicorn[standard]<1.0,>=0.29.0
Requires-Dist: ldap3<3.0,>=2.9.1
Requires-Dist: httpx<1.0,>=0.27
Requires-Dist: pydantic<3.0,>=2.7.0
Requires-Dist: pydantic-settings<3.0,>=2.0
Requires-Dist: click<9.0,>=8.1.0
Requires-Dist: pyyaml<7.0,>=6.0
Requires-Dist: jinja2<4.0,>=3.1.0
Requires-Dist: python-multipart<1.0,>=0.0.18
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
Requires-Dist: httpx[http2]>=0.27; extra == "dev"
Requires-Dist: ruff>=0.15.0; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="https://raw.githubusercontent.com/anudeepd/ldapgate/main/assets/logo.svg" alt="LDAPGate" width="120"/>
</p>

<h1 align="center">LDAPGate</h1>

<p align="center">Lightweight LDAP/AD authentication gateway for Python web apps. Install it, configure it, done.</p>

## Features

- **Two deployment modes** — standalone reverse proxy or drop-in FastAPI middleware
- **WebDAV + browser in one** — browsers get a login form; WebDAV clients (Windows, macOS Finder, curl) get a Basic auth challenge — same endpoint, no extra config
- **Pure Python LDAP** — no OS-level libs required, uses `ldap3`
- **Signed cookie sessions** — stateless, no server-side session storage
- **OpenLDAP and Active Directory** — `uid=` and `sAMAccountName=` out of the box
- **Optional group gating** — restrict access to members of a specific LDAP group
- **Header injection** — injects `X-Forwarded-User` for downstream apps
- **Bundled login form** — responsive, dark/light mode, customisable, works air-gapped

## Install

```bash
pip install ldapgate
```

## Config file

Both modes share the same `ldapgate.yaml`:

```yaml
ldap:
  url: ldaps://dc.example.com:636
  bind_dn: CN=svc,CN=Users,DC=example,DC=com
  bind_password: secret
  base_dn: DC=example,DC=com
  user_filter: "(sAMAccountName={username})"         # AD; OpenLDAP: (uid={username})
  # LDAPGate requires at least one authorization rule by default:
  group_dn: CN=app-users,CN=Users,DC=example,DC=com  # restrict by group
  allowed_users:                                      # or restrict by local allowlist
    - alice
    - bob
  timeout: 10                                      # LDAP operation and total login deadline
  tls_validate: REQUIRED                             # NONE | OPTIONAL | REQUIRED
  tls_ca_cert_file: /etc/ssl/certs/internal-ca.pem  # optional — custom CA bundle
  tls_client_cert_file: /etc/ssl/certs/client.pem   # optional — mutual TLS client cert
  tls_client_key_file: /etc/ssl/private/client.key  # optional — mutual TLS client key

proxy:
  listen_host: 0.0.0.0
  listen_port: 9000
  backend_url: http://localhost:8080
  secret_key: change-me-to-something-random
  session_ttl: 3600                                # absolute cookie and token lifetime
  idle_timeout: 0                                  # inactivity limit; must be <= session_ttl
  user_header: X-Forwarded-User
  login_path: /_auth/login
  logout_path: /_auth/logout
  app_name: MyApp
  mask_usernames_in_logs: true                    # set false to log full usernames
  secure_cookies: false                              # set true when behind HTTPS
```

`proxy.session_ttl` is the absolute lifetime of the signed session cookie and token. `proxy.idle_timeout` is the inactivity limit. The idle limit must not exceed the absolute lifetime, because the cookie expires first and the idle timer can never fire. LDAPGate rejects that configuration at startup. The effective expiry is the earlier of the two.

All settings can also be provided via environment variables using `__` as a separator — e.g. `LDAP__URL`, `PROXY__SECRET_KEY`.

## Corporate / Active Directory setup

For corp environments with internal CAs where cert validation isn't feasible:

```yaml
ldap:
  url: ldaps://dc.example.com:636
  tls_validate: NONE
  # ... other settings ...
```

> ⚠️ **Warning:** `tls_validate: NONE` disables all TLS certificate validation and makes you vulnerable to man-in-the-middle attacks. Whenever possible, use `tls_ca_cert_file` to trust your internal CA instead.

For plain LDAP with STARTTLS:

```yaml
ldap:
  url: ldap://dc.example.com:389
  use_starttls: true
```

---

## Mode 1 — Standalone Reverse Proxy

Run ldapgate as a standalone process in front of any app.

```
Browser / WebDAV client → ldapgate :9000 → backend app :8080
```

```bash
ldapgate serve --config ldapgate.yaml
```

All traffic is intercepted by ldapgate before reaching the backend. Authenticated requests are forwarded with the `X-Forwarded-User` header set to the verified username. Apps can point their logout link at the configured `logout_path` (default `/_auth/logout`) to clear the session.

**WebDAV clients** receive a `401 WWW-Authenticate: Basic` challenge automatically and authenticate per-request via HTTP Basic auth — no session cookie needed.

---

## Mode 2 — FastAPI Middleware

Drop ldapgate auth directly into an existing FastAPI app — no separate process.

```python
from fastapi import FastAPI
from ldapgate.config import load_config
from ldapgate.middleware import add_ldap_auth

app = FastAPI()
config = load_config("ldapgate.yaml")
add_ldap_auth(app, config)

@app.get("/api/data")
async def data(request):
    return {"user": request.state.user}  # authenticated username
```

`add_ldap_auth` registers the login/logout routes and attaches the middleware in one call. The authenticated username is available as `request.state.user` and is also injected as the configured `user_header` into the request headers.

### WebDAV with middleware

The middleware handles both browser and WebDAV clients on the same app instance:

| Client | Auth flow |
|--------|-----------|
| Browser | Redirected to login form → session cookie |
| WebDAV (Windows, macOS Finder, curl) | `401 WWW-Authenticate: Basic` challenge → Basic auth per-request |

No extra routes or config needed — if a request arrives without a session cookie and without `Accept: text/html`, the middleware issues a `401` with a `WWW-Authenticate: Basic` header. The client sends credentials, the middleware validates against LDAP, and the request proceeds.

Successful Basic auth checks are cached briefly in memory by default (`proxy.basic_auth_cache_ttl: 60`) so chatty WebDAV clients like macOS Finder do not force a fresh LDAP bind for every metadata request. Set it to `0` to disable the cache.

`allowed_users` and `group_dn` apply to both flows — a user blocked by those settings is rejected regardless of whether they authenticated via cookie or Basic auth.

**Example — xwing file server with WebDAV:**

```python
from fastapi import FastAPI
from ldapgate.config import load_config
from ldapgate.middleware import add_ldap_auth
from xwing.app import create_app
from xwing.config import Settings

xwing_settings = Settings(root_dir="/srv/files", users_config="users.yaml")
app = create_app(xwing_settings)

ldap_config = load_config("ldapgate.yaml")
add_ldap_auth(app, ldap_config)
```

Windows users can now map `http://your-server:8989/` as a network drive:

```cmd
net use Z: http://your-server:8989/ /user:alice /persistent:yes
```

macOS Finder: **Go → Connect to Server** (⌘K) → `http://your-server:8989/`

---

## CLI reference

```
ldapgate serve [OPTIONS]

  --config PATH     Path to ldapgate.yaml (reads env vars if omitted)
  --host TEXT       Override listen host
  --port INTEGER    Override listen port
  --backend TEXT    Override backend URL
  --reload          Enable auto-reload (dev only)
```

---

## Development

Requires [uv](https://github.com/astral-sh/uv).

```bash
git clone https://github.com/anudeepd/ldapgate
cd ldapgate
uv sync
uv run pytest
```

## License

MIT
