Simple authorization conventions for Phoenix applications with context-based policies and schema scoping.
Bodyguard is an authorization library for Phoenix applications that provides simple conventions for implementing permission checks. It solves the problem of scattered authorization logic by centralizing policy definitions within context modules while supporting checks from controllers, views, sockets, and tests.
Elixir developers building Phoenix applications who need a clean, maintainable way to implement authorization rules across their business logic and web interfaces.
Developers choose Bodyguard for its convention-over-configuration approach that keeps authorization logic close to business contexts, its seamless integration with Phoenix components, and its support for both action-based policies and data-scoping patterns.
Simple authorization conventions for Phoenix apps
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Authorization logic is embedded directly in Phoenix context modules, keeping business rules close to the code that enforces them, as shown in the blog example where MyApp.Blog implements Bodyguard.Policy.
The Bodyguard.Schema behaviour automatically filters database results based on user permissions, preventing data leaks without manual query adjustments, demonstrated in the Post.scope/3 callback.
It works out-of-the-box with controllers via action_fallback, LiveView mount/handle_event, plugs for pipeline checks, and includes testing utilities, ensuring consistent authorization across the entire application.
Policies can be separated into dedicated modules using defdelegate, allowing for better code organization while maintaining the same interface, as mentioned in the policy delegation section.
Bodyguard only provides the policy enforcement layer; you must implement role definitions, permission storage, and dynamic updates yourself, which adds overhead for complex authorization needs.
Since policies are defined per context, similar rules across different contexts (e.g., user management and blog posts) can lead to code repetition without a central way to share logic.
Handling authorization failures requires manual configuration of fallback controllers and plug options, which the README admits can be tricky, especially for beginners integrating with Phoenix pipelines.