-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
defmodule Stripe.Webhook do | ||
@moduledoc """ | ||
Creates a Stripe Event from webhook's payload if signature is valid. | ||
Use `construct_event/3` to verify the authenticity of a webhook request and | ||
convert its payload into a `Stripe.Event` struct. | ||
case Stripe.Webhook.construct_event(payload, signature, secret) do | ||
{:ok, %Stripe.Event{} = event} -> | ||
# Return 200 to Stripe and handle event | ||
{:error, reason} -> | ||
# Reject webhook by responding with non-2XX | ||
end | ||
""" | ||
alias Stripe.Util | ||
|
||
@type webhook_error :: :invalid_payload | :invalid_signature | :missing_signature | :missing_timestamp | :expired_timestamp | :invalid_timestamp | ||
|
||
@default_tolerance 300 | ||
@expected_scheme "v1" | ||
|
||
@spec construct_event(String.t, String.t, String.t, integer) :: {:ok, Stripe.Event.t} | {:error, webhook_error} | ||
def construct_event(payload, signature_header, secret, tolerance \\ @default_tolerance) do | ||
case verify_signature(payload, signature_header, secret, tolerance) do | ||
:ok -> | ||
case convert_to_event(payload) do | ||
%Stripe.Event{} = event -> | ||
{:ok, event} | ||
_ -> | ||
{:error, :invalid_payload} | ||
end | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
defp verify_signature(payload, signature_header, secret, tolerance) do | ||
with {:ok, timestamp} <- get_timestamp(signature_header, tolerance), | ||
{:ok, signatures} <- get_signatures(signature_header) do | ||
signed_payload = "#{timestamp}.#{payload}" | ||
expected_signature = compute_signature(signed_payload, secret) | ||
if Enum.any?(signatures, & Util.secure_equals?(&1, expected_signature)) do | ||
:ok | ||
else | ||
{:error, :invalid_signature} | ||
end | ||
end | ||
end | ||
|
||
defp get_timestamp(signature_header, tolerance) do | ||
timestamp = | ||
signature_header | ||
|> String.split(",") | ||
|> Enum.find_value(:not_found, fn | ||
"t=" <> timestamp -> | ||
timestamp | ||
_ -> | ||
false | ||
end) | ||
|
||
with {:ok, timestamp} <- parse_timestamp(timestamp) do | ||
if timestamp < (System.system_time(:seconds) - tolerance) do | ||
{:error, :expired_timestamp} | ||
else | ||
{:ok, timestamp} | ||
end | ||
end | ||
end | ||
|
||
defp parse_timestamp(:not_found), do: {:error, :missing_timestamp} | ||
defp parse_timestamp(timestamp) do | ||
case Integer.parse(timestamp) do | ||
{timestamp, _} -> | ||
{:ok, timestamp} | ||
:error -> | ||
{:error, :invalid_timestamp} | ||
end | ||
end | ||
|
||
defp get_signatures(signature_header, scheme \\ @expected_scheme) do | ||
signatures = | ||
signature_header | ||
|> String.split(",") | ||
|> Enum.reduce([], fn item, acc -> | ||
case String.split(item, "=") do | ||
[^scheme, value] -> | ||
[value | acc] | ||
_ -> | ||
acc | ||
end | ||
end) | ||
|
||
case signatures do | ||
[] -> | ||
{:error, :missing_signature} | ||
signatures -> | ||
{:ok, signatures} | ||
end | ||
end | ||
|
||
defp compute_signature(payload, secret) do | ||
:crypto.hmac(:sha256, secret, payload) | ||
|> Base.encode16(case: :lower) | ||
end | ||
|
||
defp convert_to_event(payload) do | ||
payload | ||
|> Poison.decode!() | ||
|> Stripe.Converter.convert_result() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule Stripe.WebhookTest do | ||
use ExUnit.Case | ||
|
||
import Stripe.Webhook | ||
|
||
@valid_payload ~S({"object": "event"}) | ||
@invalid_payload "{}" | ||
|
||
@valid_scheme "v1" | ||
@invalid_scheme "v0" | ||
|
||
@secret "secret" | ||
|
||
defp generate_signature(timestamp, payload, secret \\ @secret) do | ||
:crypto.hmac(:sha256, secret, "#{timestamp}.#{payload}") | ||
|> Base.encode16(case: :lower) | ||
end | ||
|
||
defp create_signature_header(timestamp, scheme, signature) do | ||
"t=#{timestamp},#{scheme}=#{signature}" | ||
end | ||
|
||
test "payload with a valid signature should return event" do | ||
timestamp = System.system_time(:seconds) | ||
payload = @valid_payload | ||
signature = generate_signature(timestamp, payload) | ||
signature_header = create_signature_header(timestamp, @valid_scheme, signature) | ||
|
||
assert {:ok, %Stripe.Event{}} = construct_event(payload, signature_header, @secret) | ||
end | ||
|
||
test "payload with an invalid signature should fail" do | ||
timestamp = System.system_time(:seconds) | ||
payload = @valid_payload | ||
signature = generate_signature(timestamp, "random") | ||
signature_header = create_signature_header(timestamp, @valid_scheme, signature) | ||
|
||
assert construct_event(payload, signature_header, @secret) == {:error, :invalid_signature} | ||
end | ||
|
||
test "payload with wrong secret should fail" do | ||
timestamp = System.system_time(:seconds) | ||
payload = @valid_payload | ||
signature = generate_signature(timestamp, payload, "wrong") | ||
signature_header = create_signature_header(timestamp, @valid_scheme, signature) | ||
|
||
assert construct_event(payload, signature_header, @secret) == {:error, :invalid_signature} | ||
end | ||
|
||
test "payload with missing signature scheme should fail" do | ||
timestamp = System.system_time(:seconds) | ||
payload = @valid_payload | ||
signature = generate_signature(timestamp, payload) | ||
signature_header = create_signature_header(timestamp, @invalid_scheme, signature) | ||
|
||
assert construct_event(payload, signature_header, @secret) == {:error, :missing_signature} | ||
end | ||
|
||
test "invalid payload with a valid signature should fail" do | ||
timestamp = System.system_time(:seconds) | ||
payload = @invalid_payload | ||
signature = generate_signature(timestamp, payload) | ||
signature_header = create_signature_header(timestamp, @valid_scheme, signature) | ||
|
||
assert construct_event(payload, signature_header, @secret) == {:error, :invalid_payload} | ||
end | ||
end |