oliver-kriska/claude-elixir-phoenix / plugins/elixir-phoenix/skills/security
security
FlaggedBuild and harden Phoenix auth and security — OAuth login, password hashing, sessions, RBAC, rate limiting, CSRF, XSS, SQL injection, secrets. Use when adding login flows or permissions, or handling user input.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.3 KB |
| references/advanced-patterns.md | markdown | 7.0 KB |
| references/authentication.md | markdown | 2.1 KB |
| references/authorization.md | markdown | 2.1 KB |
| references/input-validation.md | markdown | 3.2 KB |
| references/oauth-linking.md | markdown | 4.5 KB |
| references/rate-limiting.md | markdown | 6.2 KB |
| references/security-headers.md | markdown | 2.5 KB |
references/authorization.md
View on GitHub# Authorization Patterns Reference
## Bodyguard (Recommended)
```elixir
# In context module
defmodule MyApp.Blog do
@behaviour Bodyguard.Policy
# Policy callbacks
def authorize(:view_post, _user, %Post{published: true}), do: :ok
def authorize(:view_post, %User{id: id}, %Post{user_id: id}), do: :ok
def authorize(:update_post, %User{id: id}, %Post{user_id: id}), do: :ok
def authorize(:delete_post, %User{role: :admin}, _post), do: :ok
def authorize(_, _, _), do: {:error, :unauthorized}
# Usage in context functions
def update_post(user, post, attrs) do
with :ok <- Bodyguard.permit(__MODULE__, :update_post, user, post) do
post
|> Post.changeset(attrs)
|> Repo.update()
end
end
end
```
## Scope-Based Queries
```elixir
# Ensure users only see their own data
def list_posts(%Scope{user: user}) do
from(p in Post, where: p.user_id == ^user.id)
|> Repo.all()
end
```
## LiveView Authorization
```elixir
# on_mount hook for authentication
def on_mount(:require_authenticated, _params, session, socket) do
case session["user_token"] do
nil ->
{:halt, socket |> put_flash(:error, "Login required") |> redirect(to: ~p"/login")}
token ->
user = Accounts.get_user_by_session_token(token)
{:cont, assign(socket, :current_user, user)}
end
end
# RE-AUTHORIZE IN EVERY EVENT HANDLER
def handle_event("delete", %{"id" => id}, socket) do
post = Blog.get_post!(id)
# Don't trust that mount authorized this action!
with :ok <- Bodyguard.permit(Blog, :delete_post, socket.assigns.current_user, post) do
Blog.delete_post(post)
{:noreply, stream_delete(socket, :posts, post)}
else
_ -> {:noreply, put_flash(socket, :error, "Unauthorized")}
end
end
```
## Anti-patterns
```elixir
# ❌ Authorization only in mount (socket state can be stale)
def mount(_params, _session, socket) do
if authorized?(socket.assigns.user), do: {:ok, socket}
end
# User then calls handle_event without re-checking!
# ✅ Re-authorize in every event handler
def handle_event("action", params, socket) do
with :ok <- authorize(socket.assigns.current_user, :action, resource) do
# ... perform action
end
end
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |