Skip to content

Latest commit

 

History

History
68 lines (51 loc) · 1.38 KB

README.md

File metadata and controls

68 lines (51 loc) · 1.38 KB

ExTrueLayerSigning

Module for signing and verifying HTTP requests to the TrueLayer API.

Installation

The package can be installed by adding ex_truelayer_signing to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_truelayer_signing, "~> 0.1.0"}
  ]
end

Usage

Via middleware

The easiest way to use this library is via the Tesla middleware it ships with, ExTrueLayerSigning.SigningMiddleware.

defmodule TrueLayer do
  use Tesla

  plug ExTrueLayerSigning.SigningMiddleware
  
  def create_payment(mandate_id, amount) do
    request_body = %{...}
    headers = [{"idempotency-key", UUID.uuid4()}]

    post("/payments", request_body, %{headers: headers})
  end

end

Alternatively, you can sign and verify requests manually

Signing a request

request = %ExTrueLayerSigning.Request{
  method: :post,
  path: "/mandates",
  body: Jason.encode!(%{"foo" => "bar"}),
  headers: [{"Idempotency-Key", "123"}]
}

{:ok, tl_signature} = ExTrueLayerSigning.sign(request)
put_header(request, "tl-signature", tl_signature)

Verify a request signature

signed_request = %ExTrueLayerSigning.Request{
  method: :post,
  path: "/mandates",
  body: Jason.encode!(%{"foo" => "bar"}),
  headers: [
    {"Idempotency-Key", "123"},
    {"tl-signature", "eyJhbGciOiJFUzUxM..."}
  ]
}
:ok = ExTrueLayerSigning.verify(signed_request)