The Siftsciex Plug provides a very small API and logic for helping process Sift Science Decisions. If you need to handle a Decision Web Hook from Sift Science then you may find this useful.
The Decision Plug really does three things:
- Checks for the Sift Science Signature in the request
- Processes the body and transforms it into a
Siftsciex.Decision.t
struct - Maps specific endpoints to specific handlers
Available in Hex, the package can be installed
by adding siftsciex_plug
to your list of dependencies in mix.exs
:
def deps do
[
{:siftsciex_plug, "~> 0.1.0"}
]
end
The first thing you need to do is configure the expected Signature key from Sift Science, as well as the header where the signature should be found:
config :siftsciex_plug,
hook_key: <sift_science_signature>,
sig_header: "x-sift-science-signature"
Then you can configure the Plug
to process and route specific paths for you.
alias Siftsciex.DecisionPlug
forward "/sift_science", DecisionPlug, %{
"bad_user" => {User, :sift_ban},
"bad_listing" => {Listing, :sift_delete}
}
If you are using any Plug.Parsers
then you will need to make sure that siftsciex_plug
checks the body before the parser consumes it. To do this simply add the following to your Parser opts:
body_reader: {Siftsciex.HookValidator, :validate, []}
In the case of a JSON
parser the full config would look something like this:
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
body_reader: {Siftsciex.HookValidator, :validate, []},
json_decoder: Poison
Doing this allows siftsciex_plug
to calculate the signature against the raw body.
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/siftsciex_plug.