Class: InstagramConnect::WebhooksController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/instagram_connect/webhooks_controller.rb

Overview

Receives Meta webhooks. Inherits ActionController::Base directly (not the engine ApplicationController) so it bypasses the host's session auth and CSRF, and instead authenticates the GET handshake by verify_token and the POST body by HMAC signature. ACKs fast and hands persistence to a job.

Instance Method Summary collapse

Instance Method Details

#receiveObject

POST — a batch of events. Verify the signature, then enqueue ingestion.



27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/instagram_connect/webhooks_controller.rb', line 27

def receive
  unless SignatureVerifier.valid?(raw_body: request.raw_post, signature: request.headers["X-Hub-Signature-256"])
    return head :unauthorized
  end

  payload = parse_json(request.raw_post)
  return head :bad_request if payload.nil?

  IngestJob.perform_later(payload)
  head :ok
end

#verifyObject

GET — Meta's subscription verification handshake.



18
19
20
21
22
23
24
# File 'app/controllers/instagram_connect/webhooks_controller.rb', line 18

def verify
  if valid_verification?
    render plain: params["hub.challenge"].to_s
  else
    head :forbidden
  end
end