Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/invrs/slack_verify
A plug to verify Slack requests
https://github.com/invrs/slack_verify
events hmac slack verification
Last synced: 8 days ago
JSON representation
A plug to verify Slack requests
- Host: GitHub
- URL: https://github.com/invrs/slack_verify
- Owner: invrs
- License: other
- Created: 2018-08-27T21:40:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-09-06T02:07:01.000Z (about 2 years ago)
- Last Synced: 2024-03-14T18:19:19.607Z (8 months ago)
- Topics: events, hmac, slack, verification
- Language: Elixir
- Homepage: https://hex.pm/packages/slack_verify
- Size: 21.5 KB
- Stars: 0
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# SlackVerify
SlackVerify is a plug that enables [verifying Slack requests](https://api.slack.com/docs/verifying-requests-from-slack).
## Installation
Installation is a five-step process.1) Add the dependency in your mix.deps:
```elixir
def deps do
[
{:slack_verify, "~> 0.2.2"}
]
end
```2) Configure your application's Slack signing secret:
```elixir
# config.exs
config :slack_verify, slack_signing_secret: System.get_env("MY_SLACK_SIGNING_SECRET")
```3) SlackVerify relies on the raw request body for signature verification. To achieve this,
configure the `body_reader` option on `Plug.Parsers`:
```elixir
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
body_reader: {CacheBodyReader, :read_body, []}, # <-- right here
json_decoder: Poison
```4) Then, per [the Plug.Parsers documentation](https://hexdocs.pm/plug/Plug.Parsers.html#module-custom-body-reader) define the CacheBodyReader module like so:
```elixir
defmodule CacheBodyReader do
def read_body(conn, opts) do
{:ok, body, conn} = Plug.Conn.read_body(conn, opts)
conn = update_in(conn.assigns[:raw_body], &[body | (&1 || [])])
{:ok, body, conn}
end
end
```5) Plug it in!
```elixir
plug SlackVerify
```## Common Patterns
It's advisable to configure a controller specifically to handle Slack requests. Then you can only
plug SlackVerify in your Slack controller and leave other controller logic in your app untouched.```elixir
defmodule MyAppWeb.MySlackController do
use MyAppWeb, :controllerplug SlackVerify
def handle(conn, _params) do
text conn, "Hello, Slack!"
end
end
```Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/slack_verify](https://hexdocs.pm/slack_verify).