Skip to content

Commit eb634bb

Browse files
committed
add okta acceptance test case using shared secrets
1 parent 92a6dcf commit eb634bb

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

lib/hooks/plugins/request_validator/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "rack/utils"
4+
35
module Hooks
46
module Plugins
57
module RequestValidator

lib/hooks/plugins/request_validator/hmac.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "openssl"
4-
require "rack/utils"
54
require "time"
65
require_relative "base"
76

lib/hooks/plugins/request_validator/shared_secret.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "rack/utils"
43
require_relative "base"
54

65
module Hooks
@@ -16,11 +15,13 @@ module RequestValidator
1615
# @example Basic configuration
1716
# request_validator:
1817
# type: shared_secret
18+
# secret_env_key: WEBHOOK_SECRET
1919
# header: Authorization
2020
#
2121
# @example Custom header configuration
2222
# request_validator:
2323
# type: shared_secret
24+
# secret_env_key: SOME_OTHER_WEBHOOK_SECRET
2425
# header: X-API-Key
2526
#
2627
# @note This validator performs direct string comparison of the shared secret.
@@ -78,7 +79,6 @@ def self.valid?(payload:, headers:, secret:, config:)
7879

7980
return false if raw_secret.nil? || raw_secret.empty?
8081

81-
# Cache the stripped value of raw_secret
8282
stripped_secret = raw_secret.strip
8383

8484
# Security: Reject secrets with leading/trailing whitespace

spec/acceptance/acceptance_tests.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
FAKE_HMAC_SECRET = "octoawesome-secret"
44
FAKE_ALT_HMAC_SECRET = "octoawesome-2-secret"
5+
FAKE_SHARED_SECRET = "octoawesome-shared-secret"
56

67
require "rspec"
78
require "net/http"
@@ -126,5 +127,26 @@
126127
expect(response.body).to include("request validation failed")
127128
end
128129
end
130+
131+
describe "okta" do
132+
it "receives a POST request but contains an invalid shared secret" do
133+
payload = { event: "user.login", user: { id: "12345" } }
134+
headers = { "Content-Type" => "application/json", "Authorization" => "badvalue" }
135+
response = http.post("/webhooks/okta", payload.to_json, headers)
136+
137+
expect(response).to be_a(Net::HTTPUnauthorized)
138+
expect(response.body).to include("request validation failed")
139+
end
140+
141+
it "successfully processes a valid POST request with shared secret" do
142+
payload = { event: "user.login", user: { id: "12345" } }
143+
headers = { "Content-Type" => "application/json", "Authorization" => FAKE_SHARED_SECRET }
144+
response = http.post("/webhooks/okta", payload.to_json, headers)
145+
146+
expect(response).to be_a(Net::HTTPSuccess)
147+
body = JSON.parse(response.body)
148+
expect(body["status"]).to eq("success")
149+
end
150+
end
129151
end
130152
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
path: /okta
2+
handler: OktaHandler
3+
4+
request_validator:
5+
type: shared_secret
6+
secret_env_key: SHARED_SECRET # the name of the environment variable containing the shared secret
7+
header: Authorization

spec/acceptance/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
LOG_LEVEL: DEBUG
1111
GITHUB_WEBHOOK_SECRET: "octoawesome-secret"
1212
ALT_WEBHOOK_SECRET: "octoawesome-too-secret"
13+
SHARED_SECRET: "octoawesome-shared-secret"
1314
command: ["script/server"]
1415
healthcheck:
1516
test: ["CMD", "curl", "-f", "http://0.0.0.0:8080/health"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class OktaHandler < Hooks::Handlers::Base
4+
def call(payload:, headers:, config:)
5+
return {
6+
status: "success"
7+
}
8+
end
9+
end

0 commit comments

Comments
 (0)