Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExTwilio.Signature for creating a "x-twilio-signiture" string #108

Closed
estermer opened this issue Oct 20, 2018 · 0 comments
Closed

ExTwilio.Signature for creating a "x-twilio-signiture" string #108

estermer opened this issue Oct 20, 2018 · 0 comments

Comments

@estermer
Copy link

I was recently writing some testing for an authorization plug that I wrote that uses the ExTwilio.RequestValidator and could not find a module or method that can create a x-twilio-token to put to my tests Plug.Conn header.

I propose we take this functionality that builds the token in the validator and make a module that creates a twilio signature if you pass in say url and params which may making unit testing controllers and plugs that are using the validator easier!

def create_signature(url, params) do
    token = Config.auth_token()

    url
    |> data_for(params)
    |> compute_hmac(token)
    |> Base.encode64()
  end

  defp data_for(url, params), do: url <> combine(params)

  defp combine(params) do
    params
    |> Map.to_list()
    |> Enum.sort()
    |> Enum.map(fn {key, value} -> key <> value end)
    |> Enum.join()
  end

  defp compute_hmac(data, key), do: :crypto.hmac(:sha, key, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants