Skip to content

Commit

Permalink
add webhook secret
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Aug 5, 2024
1 parent 6b1c3bb commit 478167e
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 12 deletions.
29 changes: 29 additions & 0 deletions app/controllers/webhook_secret_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class WebhookSecretController < ApplicationController
before_action :load_encrypted_config
authorize_resource :encrypted_config, parent: false

def index; end

def create
@encrypted_config.assign_attributes(value: {
encrypted_config_params[:key] => encrypted_config_params[:value]
}.compact_blank)

@encrypted_config.value.present? ? @encrypted_config.save! : @encrypted_config.delete

redirect_back(fallback_location: settings_webhooks_path, notice: 'Webhook Secret has been saved.')
end

private

def load_encrypted_config
@encrypted_config =
current_account.encrypted_configs.find_or_initialize_by(key: EncryptedConfig::WEBHOOK_SECRET_KEY)
end

def encrypted_config_params
params.require(:encrypted_config).permit(value: %i[key value]).fetch(:value, {})
end
end
14 changes: 10 additions & 4 deletions app/jobs/send_form_completed_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def perform(params = {})

attempt = params['attempt'].to_i

url = load_url(submitter, params)
url, secret = load_url_and_secret(submitter, params)

return if url.blank?

Expand All @@ -29,6 +29,7 @@ def perform(params = {})
timestamp: Time.current,
data: Submitters::SerializeForWebhook.call(submitter)
}.to_json,
**secret.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand All @@ -45,17 +46,22 @@ def perform(params = {})
end
end

def load_url(submitter, params)
def load_url_and_secret(submitter, params)
if params['encrypted_config_id']
url = EncryptedConfig.find(params['encrypted_config_id']).value
config = EncryptedConfig.find(params['encrypted_config_id'])

url = config.value

return if url.blank?

preferences = Accounts.load_webhook_preferences(submitter.submission.account)

return if preferences['form.completed'] == false

url
secret = EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h

[url, secret]
elsif params['webhook_url_id']
webhook_url = submitter.account.webhook_urls.find(params['webhook_url_id'])

Expand Down
5 changes: 4 additions & 1 deletion app/jobs/send_form_started_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def perform(params = {})
submitter = Submitter.find(params['submitter_id'])

attempt = params['attempt'].to_i
url = Accounts.load_webhook_url(submitter.submission.account)
config = Accounts.load_webhook_config(submitter.submission.account)
url = config&.value.presence

return if url.blank?

Expand All @@ -30,6 +31,8 @@ def perform(params = {})
timestamp: Time.current,
data: Submitters::SerializeForWebhook.call(submitter)
}.to_json,
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand Down
5 changes: 4 additions & 1 deletion app/jobs/send_form_viewed_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def perform(params = {})
submitter = Submitter.find(params['submitter_id'])

attempt = params['attempt'].to_i
url = Accounts.load_webhook_url(submitter.submission.account)
config = Accounts.load_webhook_config(submitter.submission.account)
url = config&.value.presence

return if url.blank?

Expand All @@ -30,6 +31,8 @@ def perform(params = {})
timestamp: Time.current,
data: Submitters::SerializeForWebhook.call(submitter)
}.to_json,
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand Down
6 changes: 5 additions & 1 deletion app/jobs/send_submission_archived_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def perform(params = {})
submission = Submission.find(params['submission_id'])

attempt = params['attempt'].to_i
url = Accounts.load_webhook_url(submission.account)

config = Accounts.load_webhook_config(submission.account)
url = config&.value.presence

return if url.blank?

Expand All @@ -28,6 +30,8 @@ def perform(params = {})
timestamp: Time.current,
data: submission.as_json(only: %i[id archived_at])
}.to_json,
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand Down
6 changes: 5 additions & 1 deletion app/jobs/send_submission_created_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def perform(params = {})
submission = Submission.find(params['submission_id'])

attempt = params['attempt'].to_i
url = Accounts.load_webhook_url(submission.account)

config = Accounts.load_webhook_config(submission.account)
url = config&.value.presence

return if url.blank?

Expand All @@ -28,6 +30,8 @@ def perform(params = {})
timestamp: Time.current,
data: Submissions::SerializeForApi.call(submission)
}.to_json,
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand Down
6 changes: 5 additions & 1 deletion app/jobs/send_template_created_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def perform(params = {})
template = Template.find(params['template_id'])

attempt = params['attempt'].to_i
url = Accounts.load_webhook_url(template.account)

config = Accounts.load_webhook_config(template.account)
url = config&.value.presence

return if url.blank?

Expand All @@ -28,6 +30,8 @@ def perform(params = {})
timestamp: Time.current,
data: Templates::SerializeForApi.call(template)
}.to_json,
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand Down
6 changes: 5 additions & 1 deletion app/jobs/send_template_updated_webhook_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def perform(params = {})
template = Template.find(params['template_id'])

attempt = params['attempt'].to_i
url = Accounts.load_webhook_url(template.account)

config = Accounts.load_webhook_config(template.account)
url = config&.value.presence

return if url.blank?

Expand All @@ -28,6 +30,8 @@ def perform(params = {})
timestamp: Time.current,
data: Templates::SerializeForApi.call(template)
}.to_json,
**EncryptedConfig.find_or_initialize_by(account_id: config.account_id,
key: EncryptedConfig::WEBHOOK_SECRET_KEY)&.value.to_h,
'Content-Type' => 'application/json',
'User-Agent' => USER_AGENT)
rescue Faraday::Error
Expand Down
3 changes: 2 additions & 1 deletion app/models/encrypted_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class EncryptedConfig < ApplicationRecord
ESIGN_CERTS_KEY = 'esign_certs',
TIMESTAMP_SERVER_URL_KEY = 'timestamp_server_url',
APP_URL_KEY = 'app_url',
WEBHOOK_URL_KEY = 'webhook_url'
WEBHOOK_URL_KEY = 'webhook_url',
WEBHOOK_SECRET_KEY = 'webhook_secret'
].freeze

belongs_to :account
Expand Down
19 changes: 19 additions & 0 deletions app/views/webhook_secret/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= render 'shared/turbo_modal', title: 'Webhook Secret' do %>
<%= form_for @encrypted_config, url: webhook_secret_index_path, method: :post, html: { class: 'space-y-4' }, data: { turbo_frame: :_top } do |f| %>
<div class="space-y-2">
<%= f.fields_for :value, Struct.new(:key, :value).new(*@encrypted_config.value.to_a.first) do |ff| %>
<div class="form-control">
<%= ff.label :key, class: 'label' %>
<%= ff.text_field :key, class: 'base-input', placeholder: 'X-Example-Header' %>
</div>
<div class="form-control">
<%= ff.label :value, class: 'label' %>
<%= ff.text_field :value, class: 'base-input' %>
</div>
<% end %>
</div>
<div class="form-control pt-2">
<%= f.button button_title, class: 'base-button' %>
</div>
<% end %>
<% end %>
5 changes: 4 additions & 1 deletion app/views/webhook_settings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
<div class="card-body p-6">
<%= form_for @encrypted_config, url: settings_webhooks_path, method: :post, html: { autocomplete: 'off' } do |f| %>
<%= f.label :value, 'Webhook URL', class: 'text-sm font-semibold' %>
<div class="flex flex-row flex-wrap space-y-2 md:space-y-0 md:flex-nowrap md:space-x-4 mt-2">
<div class="flex flex-row flex-wrap space-y-2 md:space-y-0 md:flex-nowrap md:space-x-2 mt-2">
<%= f.url_field :value, class: 'input font-mono input-bordered w-full', placeholder: 'https://example.com/hook' %>
<%= f.button button_title(title: 'Save', disabled_with: 'Saving'), class: 'base-button w-full md:w-32' %>
<a href="<%= webhook_secret_index_path %>" data-turbo-frame="modal" class="white-button w-full md:w-auto">
Add Secret
</a>
</div>
<% end %>
<% preference = current_account.account_configs.find_by(key: AccountConfig::WEBHOOK_PREFERENCES_KEY)&.value || {} %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
resources :submitters_autocomplete, only: %i[index]
resources :template_folders_autocomplete, only: %i[index]
resources :webhook_preferences, only: %i[create]
resources :webhook_secret, only: %i[index create]
resource :templates_upload, only: %i[create]
authenticated do
resource :templates_upload, only: %i[show], path: 'new'
Expand Down

0 comments on commit 478167e

Please sign in to comment.