I have a requirement to check the digest of JSON content sent to a phoenix server. To check the digest the raw body is needed. Is there any way to access the raw content in a plug later in the pipeline than the parsers. I want to add the following Digest verification plug to the end of the pipeline but cannot work out how it accesses the raw content that was sent.
plug Plug.Parsers,
parsers: [:urlencoded, :json],
pass: ["*/*"],
json_decoder: Poison
plug Plug.MethodOverride
plug Plug.Head
plug Plug.VerifyDigest
I was facing a similar problem, and I wrote a Plug along the lines of this (note I'm still learning so this might be done better):
The above request compares a HMAC signed body with the HMAC signature in a request header.
I circumvented the
read_body-problem by parsing the JSON within the same code when the signature matched the expected signature. Theconnection is passed through if the request doesn't fit a typical API call (in my case doesn't have a HMAC-header-token), hence leaving the body_params unread.I then plugged the above Plug in
endpoint.exjust before the Plug.Parsers is plugged in.I got some inspiration from the discussion in this Phoenix issue: Way to Read Request Body As String