SimpleTokenAuthentication
Usage
Phoenix Integration
Inside web/router.ex file, add plug to your pipeline like so:
defmodule MyApp.Router
use Phoenix.Router
pipeline :api do
plug SimpleTokenAuthentication
end
scope "/", MyApp do
pipe_through :api
get "/hello", HelloController, :hello
end
endInstallation
-
Add
simple_token_authenticationto your list of dependencies inmix.exs:
def deps do
[{:simple_token_authentication, "~> 0.6.0"}]
end-
Ensure
simple_token_authenticationis started before your application:
def application do
[applications: [:simple_token_authentication]]
endConfigure your token in
config.exs:config :simple_token_authentication, token: "your-token-here", service_tokens: [ service_a: "service-a-token", service_b: "service-b-token" ]Configure your connecting application to pass a token in the
authorizationheader, e.g.:put_header("authorization", "your-token-here")
Realm
Optionally, you can add a realm to the simple auth. This allows services to use the plug multiple times with different realms For example:
Configure your token under a named realm in config.exs:
config :simple_token_authentication,
another_realm: [
service_tokens: [
service_a: "service-a-token",
service_b: "service-b-token"
]
]Pass that name when configuring the plug:
defmodule MyApp.Router
use Phoenix.Router
pipeline :api do
plug SimpleTokenAuthentication, auth_realm: :another_realm
end
scope "/", MyApp do
pipe_through :api
get "/hello", HelloController, :hello
end
endNotes
- Token value can be a comma-separated list of tokens
-
Specifying
service_tokensis optional -
Auth will succeed if token exists in either list (
tokenorservice_tokens) -
Use of a service token will add "service_name" to
Logging.metadata - Service can be identified in the conn.assigns[:simple_token_auth_service]. Will be the name of the service or :global when matching the token key
Copyright and License
Copyright (c) 2025 Podium
This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.