Skip to content

Commit

Permalink
Feature: HTTP Basic Auth support for webhook delivery (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantownsend committed Feb 9, 2017
1 parent 42dbb15 commit 80379ab
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 11 deletions.
14 changes: 13 additions & 1 deletion app/resources/api/v1/webhook_resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
class Api::V1::WebhookResource < JSONAPI::Resource
attributes :identifier, :url, :headers, :body
attributes :identifier, :url, :headers, :body, :auth

# associations
has_many :deliveries

def auth
{
username: _model.basic_auth_username,
password: _model.basic_auth_password
}.compact
end

def auth=(value)
_model.basic_auth_username = value[:username]
_model.basic_auth_password = value[:password]
end
end
4 changes: 3 additions & 1 deletion app/workers/webhook_delivery_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def perform(webhook_id, delivery_service: DeliverWebhook, record_service: Record
delivery_service.call({
url: webhook.url,
headers: webhook.headers,
body: webhook.body
body: webhook.body,
username: webhook.basic_auth_username,
password: webhook.basic_auth_password
})
end
# return true or false to requeue failures
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddHttpBasicAuthCredentialsToWebhooks < ActiveRecord::Migration[5.0]
def change
add_column :webhooks, :basic_auth_username, :string
add_column :webhooks, :basic_auth_password, :string
end
end
16 changes: 9 additions & 7 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170206200008) do
ActiveRecord::Schema.define(version: 20170209210923) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -28,12 +28,14 @@
end

create_table "webhooks", force: :cascade do |t|
t.uuid "identifier", null: false
t.string "url", null: false
t.jsonb "headers", default: {}, null: false
t.text "body", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "identifier", null: false
t.string "url", null: false
t.jsonb "headers", default: {}, null: false
t.text "body", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "basic_auth_username"
t.string "basic_auth_password"
t.index ["identifier"], name: "index_webhooks_on_identifier", unique: true, using: :btree
t.index ["url"], name: "index_webhooks_on_url", using: :btree
end
Expand Down
6 changes: 5 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ Deliveries are attempted 3 times before the background queue will give up.
"headers": {
"Content-Type": "application/json"
},
"body": "{\"key\":\"value\"}"
"body": "{\"key\":\"value\"}",
"auth": {
"username": "testuser001",
"password": "arandompassword"
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions spec/integration/webhooks_api_v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
expect(record_b.id).to eq(record_a.id)
end

specify 'creating a webhook should accept HTTP Basic Auth credentials' do
record = TestModels::Webhook.create({
identifier: SecureRandom.uuid,
url: 'http://example.com/',
headers: { 'Content-Type': 'text/plain' },
body: 'Some plain text',
auth: { username: 'test', password: 'password123' }
})

aggregate_failures do
expect(record).to be_persisted
expect(record.errors).to be_empty
expect(record.auth).to match({
username: 'test',
password: 'password123'
})
end
end

specify 'reading a webhook after creation' do
record = TestModels::Webhook.create({
identifier: SecureRandom.uuid,
Expand Down
1 change: 1 addition & 0 deletions spec/support/test_models/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Webhook < Base
property :url
property :headers
property :body
property :auth

# associations
has_many :deliveries
Expand Down
5 changes: 4 additions & 1 deletion spec/workers/webhook_delivery_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
describe '#perform' do
it 'should be deliver the webhook' do
stub_request(:post, 'example.com').
with(basic_auth: ['test', 'password123']).
to_return(body: 'okay!', status: 200)

record = Webhook.create({
identifier: SecureRandom.uuid,
url: 'http://example.com/',
headers: { 'Content-Type' => 'text/plain' },
body: 'Some plain text'
body: 'Some plain text',
basic_auth_username: 'test',
basic_auth_password: 'password123'
})

result = described_class.new.perform(record.id)
Expand Down

0 comments on commit 80379ab

Please sign in to comment.