https webhooks
This commit is contained in:
@@ -3,10 +3,26 @@
|
|||||||
module SendWebhookRequest
|
module SendWebhookRequest
|
||||||
USER_AGENT = 'DocuSeal.com Webhook'
|
USER_AGENT = 'DocuSeal.com Webhook'
|
||||||
|
|
||||||
|
LOCALHOSTS = %w[0.0.0.0 127.0.0.1 localhost].freeze
|
||||||
|
|
||||||
|
HttpsError = Class.new(StandardError)
|
||||||
|
LocalhostError = Class.new(StandardError)
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def call(webhook_url, event_type:, data:)
|
def call(webhook_url, event_type:, data:)
|
||||||
Faraday.post(webhook_url.url) do |req|
|
uri = begin
|
||||||
|
URI(webhook_url.url)
|
||||||
|
rescue URI::Error
|
||||||
|
Addressable::URI.parse(webhook_url.url).normalize
|
||||||
|
end
|
||||||
|
|
||||||
|
if Docuseal.multitenant?
|
||||||
|
raise HttpsError, 'Only HTTPS is allowed.' if uri.scheme != 'https'
|
||||||
|
raise LocalhostError, "Can't send to localhost." if uri.host.in?(LOCALHOSTS)
|
||||||
|
end
|
||||||
|
|
||||||
|
Faraday.post(uri) do |req|
|
||||||
req.headers['Content-Type'] = 'application/json'
|
req.headers['Content-Type'] = 'application/json'
|
||||||
req.headers['User-Agent'] = USER_AGENT
|
req.headers['User-Agent'] = USER_AGENT
|
||||||
req.headers.merge!(webhook_url.secret.to_h) if webhook_url.secret.present?
|
req.headers.merge!(webhook_url.secret.to_h) if webhook_url.secret.present?
|
||||||
|
|||||||
Reference in New Issue
Block a user