Skip to content

Commit 1caf733

Browse files
committed
Add request context to authentication validation and logging
1 parent fd9d934 commit 1caf733

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

lib/hooks/app/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def self.create(config:, endpoints:, log:)
9999
raw_body = request.body.read
100100

101101
if endpoint_config[:auth]
102-
validate_auth!(raw_body, headers, endpoint_config, config)
102+
validate_auth!(raw_body, headers, endpoint_config, config, request_context)
103103
end
104104

105105
payload = parse_payload(raw_body, headers, symbolize: false)

lib/hooks/app/auth/auth.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,44 @@ module Auth
1616
# @param headers [Hash] The request headers.
1717
# @param endpoint_config [Hash] The endpoint configuration, must include :auth key.
1818
# @param global_config [Hash] The global configuration (optional, for compatibility).
19+
# @param request_context [Hash] Context for the request, e.g. request ID, path, handler (optional).
1920
# @raise [StandardError] Raises error if authentication fails or is misconfigured.
2021
# @return [void]
2122
# @note This method will halt execution with an error if authentication fails.
22-
def validate_auth!(payload, headers, endpoint_config, global_config = {})
23+
def validate_auth!(payload, headers, endpoint_config, global_config = {}, request_context = {})
2324
auth_config = endpoint_config[:auth]
25+
request_id = request_context&.dig(:request_id)
2426

2527
# Ensure auth type is present and valid
2628
auth_type = auth_config&.dig(:type)
2729
unless auth_type&.is_a?(String) && !auth_type.strip.empty?
30+
log.error("authentication configuration missing or invalid - request_id: #{request_id}")
2831
error!({
2932
error: "authentication_configuration_error",
30-
message: "authentication configuration missing or invalid"
33+
message: "authentication configuration missing or invalid",
34+
request_id:
3135
}, 500)
3236
end
3337

3438
# Get auth plugin from loaded plugins registry (boot-time loaded only)
3539
begin
3640
auth_class = Core::PluginLoader.get_auth_plugin(auth_type)
3741
rescue => e
38-
log.error("failed to load auth plugin '#{auth_type}': #{e.message}")
42+
log.error("failed to load auth plugin '#{auth_type}': #{e.message} - request_id: #{request_id}")
3943
error!({
4044
error: "authentication_plugin_error",
41-
message: "unsupported auth type '#{auth_type}'"
45+
message: "unsupported auth type '#{auth_type}'",
46+
request_id:
4247
}, 400)
4348
end
4449

4550
log.debug("validating auth for request with auth_class: #{auth_class.name}")
4651
unless auth_class.valid?(payload:, headers:, config: endpoint_config)
47-
log.warn("authentication failed for request with auth_class: #{auth_class.name}")
52+
log.warn("authentication failed for request with auth_class: #{auth_class.name} - request_id: #{request_id}")
4853
error!({
4954
error: "authentication_failed",
50-
message: "authentication failed"
55+
message: "authentication failed",
56+
request_id:
5157
}, 401)
5258
end
5359
end

spec/unit/lib/hooks/app/auth/auth_security_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ def error!(message, code)
2727
context "with missing auth configuration" do
2828
it "rejects request with no auth config" do
2929
endpoint_config = {}
30+
global_config = {}
31+
request_context = { request_id: "test-request-id" }
32+
log_msg = "authentication configuration missing or invalid - request_id: #{request_context[:request_id]}"
33+
34+
expect(log).to receive(:error).with(log_msg)
3035

3136
expect do
32-
instance.validate_auth!(payload, headers, endpoint_config)
37+
instance.validate_auth!(payload, headers, endpoint_config, global_config, request_context)
3338
end.to raise_error(StandardError, /authentication configuration missing or invalid/)
3439
end
3540

0 commit comments

Comments
 (0)