Created
July 14, 2018 15:11
-
-
Save AndrewDryga/af6a2a8338784b1b9eb6f7de8c537db6 to your computer and use it in GitHub Desktop.
Deciding Facebook signed request in Elixir
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
app_secret = Keyword.fetch!(config, :app_secret) | |
with [encoded_signature, encoded_payload] <- String.split(signed_request, "."), | |
{:ok, signature} <- Base.url_decode64(encoded_signature, padding: false), | |
{:ok, payload} <- Base.url_decode64(encoded_payload, padding: false), | |
{:ok, payload} <- Jason.decode(payload), | |
%{"algorithm" => "HMAC-SHA256"} <- payload, | |
payload_signature = :crypto.hmac(:sha256, app_secret, encoded_payload), | |
true <- Plug.Crypto.secure_compare(signature, payload_signature) do | |
{:ok, payload} | |
else | |
_error -> | |
{:error, :invalid_signature} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment