Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Elixir
  3. bodyguard

bodyguard

MITElixir

Simple authorization conventions for Phoenix applications with context-based policies and schema scoping.

Visit WebsiteGitHubGitHub
796 stars45 forks0 contributors

What is bodyguard?

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.

Target Audience

Elixir developers building Phoenix applications who need a clean, maintainable way to implement authorization rules across their business logic and web interfaces.

Value Proposition

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.

Overview

Simple authorization conventions for Phoenix apps

Use Cases

Best For

  • Implementing role-based access control in Phoenix applications
  • Adding authorization checks to Phoenix contexts and controllers
  • Scoping database queries based on user permissions
  • Handling authorization failures with Phoenix fallback controllers
  • Securing LiveView components with permission checks
  • Testing authorization logic in Elixir applications

Not Ideal For

  • Projects using Elixir without Phoenix, as it's tightly integrated with Phoenix's context and controller layers
  • Teams needing dynamic, database-driven permissions that change at runtime without code deploys
  • Applications requiring hierarchical or inheritance-based permission models, like multi-tenant systems with complex role hierarchies
  • Developers preferring declarative YAML/JSON configuration over code-based policy definitions

Pros & Cons

Pros

Context-Integrated Policies

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.

Schema Scoping for Queries

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.

Seamless Phoenix Stack Support

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.

Clean Delegation Patterns

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.

Cons

No Built-in Role Management

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.

Potential Policy Duplication

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.

Complex Fallback Setup

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.

Frequently Asked Questions

Quick Stats

Stars796
Forks45
Contributors0
Open Issues0
Last commit2 years ago
CreatedSince 2016

Tags

#elixir#phoenix#ecto#phoenix-framework#policy-management#authorization#security#access-control#backend#web-development

Built With

E
Ecto
E
Elixir
P
Phoenix

Links & Resources

Website

Included in

Elixir13.1k
Auto-fetched 10 hours ago

Related Projects

canarycanary

:hatching_chick: Elixir authorization and resource-loading library for Plug applications.

Stars485
Forks48
Last commit1 year ago
canadacanada

Easy permission definitions in Elixir apps!

Stars459
Forks28
Last commit5 years ago
authorizeauthorize

Rule based authorization for Elixir

Stars100
Forks5
Last commit6 years ago
speakeasyspeakeasy

Middleware based authorization for Absinthe GraphQL powered by Bodyguard

Stars82
Forks7
Last commit3 years ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub