Pike Quick Start Guide
View SourcePike is a lightweight Elixir library for API key authentication and fine-grained authorization.
Installation
Add Pike to your dependencies in mix.exs:
def deps do
[
{:pike, "~> 0.1.0"}
]
endThen run:
mix deps.get
Basic Setup
1. Configure Pike
Create a configuration in your config.exs:
config :pike,
store: Pike.Store.ETS2. Create your ETS Table Module
defmodule YourApp.APIStore do
use Pike.Store.ETS, table_name: :default_api_keys
end3. Add the Authentication Plug to Your Pipeline
# In your router or pipeline
pipeline :api do
plug :accepts, ["json"]
plug Pike.AuthorizationPlug
endBasic Usage
Creating and Managing API Keys
# Create an API key with permissions
api_key = %{
key: "abc123",
permissions: [
%{resource: "Products", scopes: [:read, :write]},
%{resource: "Orders", scopes: [:read]}
]
}
# Insert the key
Pike.insert(api_key)Requiring Permissions in Controllers
defmodule MyApp.ProductController do
use MyApp.Web, :controller
use Pike.Authorization
@require_permission "Products:read"
def index(conn, _params) do
# Only accessible with Products:read permission
# ...
end
@require_permission "Products:write"
def create(conn, _params) do
# Only accessible with Products:write permission
# ...
end
endChecking Permissions Programmatically
# Get an API key
api_key = Pike.get_key("abc123")
# Check if the key allows a specific action
if Pike.action?(api_key, "Products", :read) do
# Perform the operation
endNext Steps
For more advanced configuration and usage, see the full documentation.