https://github.com/podium/simple_token_authentication
Elixir Plug that checks for presence of a simple token for authentication
https://github.com/podium/simple_token_authentication
authentication elixir elixir-lang phoenix phoenix-framework simple-auth
Last synced: 4 months ago
JSON representation
Elixir Plug that checks for presence of a simple token for authentication
- Host: GitHub
- URL: https://github.com/podium/simple_token_authentication
- Owner: podium
- License: mit
- Created: 2016-10-13T22:56:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2025-09-15T21:07:06.000Z (4 months ago)
- Last Synced: 2025-10-07T01:14:10.826Z (4 months ago)
- Topics: authentication, elixir, elixir-lang, phoenix, phoenix-framework, simple-auth
- Language: Elixir
- Homepage: https://hexdocs.pm/simple_token_authentication
- Size: 187 KB
- Stars: 13
- Watchers: 46
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# SimpleTokenAuthentication
[](https://github.com/podium/simple_token_authentication/actions/workflows/ci.yml) [](https://hex.pm/packages/simple_token_authentication) [](https://hexdocs.pm/simple_token_authentication)
[](https://hex.pm/packages/simple_token_authentication)
[](https://github.com/podium/simple_token_authentication/blob/master/LICENSE.md)
## Usage
### Phoenix Integration
Inside `web/router.ex` file, add plug to your pipeline like so:
```elixir
defmodule MyApp.Router
use Phoenix.Router
pipeline :api do
plug SimpleTokenAuthentication
end
scope "/", MyApp do
pipe_through :api
get "/hello", HelloController, :hello
end
end
```
## Installation
1. Add `simple_token_authentication` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:simple_token_authentication, "~> 0.6.0"}]
end
```
2. Ensure `simple_token_authentication` is started before your application:
```elixir
def application do
[applications: [:simple_token_authentication]]
end
```
3. Configure your token in `config.exs`:
```elixir
config :simple_token_authentication,
token: "your-token-here",
service_tokens: [
service_a: "service-a-token",
service_b: "service-b-token"
]
```
4. Configure your connecting application to pass a token in the `authorization` header, e.g.:
```elixir
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`:
```elixir
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:
```elixir
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
end
```
## Notes
- Token value can be a comma-separated list of tokens
- Specifying `service_tokens` is optional
- Auth will succeed if token exists in *either* list (`token` or `service_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](./LICENSE.md) file for more details.