webhook_agent.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require 'uri'
  2. module Agents
  3. class WebhookAgent < Agent
  4. include WebRequestConcern
  5. cannot_be_scheduled!
  6. cannot_receive_events!
  7. description do <<-MD
  8. The Webhook Agent will create events by receiving webhooks from any source. In order to create events with this agent, make a POST request to:
  9. ```
  10. https://#{ENV['DOMAIN']}/users/#{user.id}/web_requests/#{id || ':id'}/#{options['secret'] || ':secret'}
  11. ```
  12. #{'The placeholder symbols above will be replaced by their values once the agent is saved.' unless id}
  13. Options:
  14. * `secret` - A token that the host will provide for authentication.
  15. * `expected_receive_period_in_days` - How often you expect to receive
  16. events this way. Used to determine if the agent is working.
  17. * `payload_path` - JSONPath of the attribute in the POST body to be
  18. used as the Event payload. Set to `.` to return the entire message.
  19. If `payload_path` points to an array, Events will be created for each element.
  20. * `verbs` - Comma-separated list of http verbs your agent will accept.
  21. For example, "post,get" will enable POST and GET requests. Defaults
  22. to "post".
  23. * `response` - The response message to the request. Defaults to 'Event Created'.
  24. * `recaptcha_secret` - Setting this to a reCAPTCHA "secret" key makes your agent verify incoming requests with reCAPTCHA. Don't forget to embed a reCAPTCHA snippet including your "site" key in the originating form(s).
  25. * `recaptcha_send_remote_addr` - Set this to true if your server is properly configured to set REMOTE_ADDR to the IP address of each visitor (instead of that of a proxy server).
  26. MD
  27. end
  28. event_description do
  29. <<-MD
  30. The event payload is based on the value of the `payload_path` option,
  31. which is set to `#{interpolated['payload_path']}`.
  32. MD
  33. end
  34. def default_options
  35. { "secret" => "supersecretstring",
  36. "expected_receive_period_in_days" => 1,
  37. "payload_path" => "some_key"
  38. }
  39. end
  40. def receive_web_request(params, method, format)
  41. # check the secret
  42. secret = params.delete('secret')
  43. return ["Not Authorized", 401] unless secret == options['secret']
  44. # check the verbs
  45. # verbs = (interpolated['verbs'] || 'post').split(/,/).map { |x| x.strip.downcase }.select { |x| x.present? }
  46. # return ["Please use #{verbs.join('/').upcase} requests only", 401] unless verbs.include?(method)
  47. # check the reCAPTCHA response if required
  48. if recaptcha_secret = interpolated['recaptcha_secret'].presence
  49. recaptcha_response = params.delete('g-recaptcha-response') or
  50. return ["Not Authorized", 401]
  51. parameters = {
  52. secret: recaptcha_secret,
  53. response: recaptcha_response,
  54. }
  55. if boolify(interpolated['recaptcha_send_remote_addr'])
  56. parameters[:remoteip] = request.env['REMOTE_ADDR']
  57. end
  58. begin
  59. response = faraday.post('https://www.google.com/recaptcha/api/siteverify',
  60. parameters)
  61. rescue => e
  62. error "Verification failed: #{e.message}"
  63. return ["Not Authorized", 401]
  64. end
  65. JSON.parse(response.body)['success'] or
  66. return ["Not Authorized", 401]
  67. end
  68. [payload_for(params)].flatten.each do |payload|
  69. create_event(payload: payload)
  70. end
  71. [response_message, 200]
  72. end
  73. def working?
  74. event_created_within?(interpolated['expected_receive_period_in_days']) && !recent_error_logs?
  75. end
  76. def validate_options
  77. unless options['secret'].present?
  78. errors.add(:base, "Must specify a secret for 'Authenticating' requests")
  79. end
  80. end
  81. def payload_for(params)
  82. Utils.value_at(params, interpolated['payload_path']) || {}
  83. end
  84. def response_message
  85. interpolated['response'] || 'Event Created'
  86. end
  87. end
  88. end