Skip to content
GitHubDiscord

Auth (lexigram-auth)

Authentication and authorization for the Lexigram Framework — JWT, OAuth2, SAML, RBAC, and multi-tenancy.


Complete authentication and authorization stack for Lexigram — JWT, OAuth2, RBAC, SAML, passkeys, and MFA. Provides a production-ready auth layer with multiple authentication strategies, policy-based access control, session management, and seamless integration with lexigram-web middleware.

Use AuthModule.configure() to register the auth bundle and protect routes with @require_auth, @require_roles, and @require_permissions decorators.

Terminal window
uv add lexigram-auth
# Optional extras
uv add "lexigram-auth[oauth2,saml,ldap]"
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.auth import AuthModule, AuthConfig, JWTConfig
@module(imports=[
AuthModule.configure(
config=AuthConfig(
secret_key="your-secret-key",
token=JWTConfig(secret_key="your-jwt-secret"),
)
)
])
class AppModule(Module):
pass
app = Application(modules=[AppModule])
if __name__ == "__main__":
app.run()

Zero-config usage: Call AuthModule.configure() with no arguments to use defaults.

application.yaml
auth:
jwt:
secret_key: "${JWT_SECRET_KEY}"
algorithm: "HS256"
expiration_hours: 24
rbac:
enabled: true
default_role: "viewer"
session:
timeout_minutes: 60
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_AUTH__JWT__SECRET_KEY=your-secret
export LEX_AUTH__JWT__ALGORITHM=HS256
export LEX_AUTH__RBAC__DEFAULT_ROLE=viewer
from lexigram.auth import AuthModule, AuthConfig, JWTConfig
config = AuthConfig(
secret_key="your-secret-key",
token=JWTConfig(
secret_key="your-jwt-secret",
algorithm="HS256",
access_token_expire_minutes=30,
),
)
AuthModule.configure(config)
FieldDefaultEnv varDescription
jwt.secret_keyLEX_AUTH__JWT__SECRET_KEYJWT signing secret (required)
jwt.algorithmHS256LEX_AUTH__JWT__ALGORITHMJWT algorithm: HS256, RS256, ES256
jwt.access_token_expire_minutes30LEX_AUTH__JWT__ACCESS_TOKEN_EXPIRE_MINUTESAccess token lifetime
rbac.enabledTrueLEX_AUTH__RBAC__ENABLEDEnable RBAC
rbac.default_roleviewerLEX_AUTH__RBAC__DEFAULT_ROLEDefault role for new users
session.timeout_minutes60LEX_AUTH__SESSION__TIMEOUT_MINUTESSession inactivity timeout
MethodDescription
AuthModule.configure(...)Configure with explicit AuthConfig
AuthModule.stub()Minimal config for testing
  • JWT authentication — HS256/RS256, key rotation, token blacklisting
  • OAuth2 / OIDC — authlib-backed: Google, GitHub, custom providers
  • SAML 2.0 — Enterprise SSO via python3-saml
  • Passkeys (WebAuthn) — FIDO2 device-based authentication
  • MFA (TOTP) — Time-based one-time passwords
  • RBAC — Role/permission inheritance with policy expressions
  • Session management — Device-aware sessions with concurrency limits
  • Token binding — MTLS / IP binding to prevent token theft
async with Application.boot(modules=[AuthModule.stub()]) as app:
# your test code
...
FileWhat it contains
src/lexigram/auth/module.pyAuthModule definition
src/lexigram/auth/config.pyAuthConfig, JWTConfig, RBACConfig
src/lexigram/auth/di/bundle_provider.pyAuthBundleProvider wiring
src/lexigram/auth/authn/jwt.pyJWTTokenManager implementation
src/lexigram/auth/authz/service.pyAuthorizationService