Skip to content

Commit d84d11f

Browse files
committed
split out validate_auth! to its own file
1 parent e1cbb4f commit d84d11f

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

lib/hooks/app/api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "json"
55
require "securerandom"
66
require_relative "helpers"
7+
require_relative "auth/auth"
78
require_relative "../handlers/base"
89
require_relative "../handlers/default"
910
require_relative "../core/logger_factory"
@@ -19,6 +20,7 @@ module App
1920
# Factory for creating configured Grape API classes
2021
class API
2122
include Hooks::App::Helpers
23+
include Hooks::App::Auth
2224

2325
# Expose start_time for endpoint modules
2426
def self.start_time
@@ -52,7 +54,7 @@ def self.create(config:, endpoints:, log:)
5254
# Use class_eval to dynamically define routes
5355
api_class.class_eval do
5456
# Define helper methods first, before routes
55-
helpers Helpers
57+
helpers Helpers, Auth
5658

5759
# Mount core operational endpoints
5860
mount Hooks::App::HealthEndpoint => config[:health_path]

lib/hooks/app/auth/auth.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
module Hooks
4+
module App
5+
module Auth
6+
# Verify the incoming request using the configured authentication method
7+
def validate_auth!(payload, headers, endpoint_config)
8+
auth_config = endpoint_config[:auth]
9+
auth_plugin_type = auth_config[:type].downcase
10+
secret_env_key = auth_config[:secret_env_key]
11+
12+
return unless secret_env_key
13+
14+
secret = ENV[secret_env_key]
15+
unless secret
16+
error!("secret '#{secret_env_key}' not found in environment", 500)
17+
end
18+
19+
auth_class = nil
20+
21+
case auth_plugin_type
22+
when "hmac"
23+
auth_class = Plugins::Auth::HMAC
24+
when "shared_secret"
25+
auth_class = Plugins::Auth::SharedSecret
26+
else
27+
error!("Custom validators not implemented in POC", 500)
28+
end
29+
30+
unless auth_class.valid?(
31+
payload:,
32+
headers:,
33+
secret:,
34+
config: endpoint_config
35+
)
36+
error!("authentication failed", 401)
37+
end
38+
end
39+
end
40+
end
41+
end

lib/hooks/app/helpers.rb

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,6 @@ def enforce_request_limits(config)
2929
# Note: Timeout enforcement would typically be handled at the server level (Puma, etc.)
3030
end
3131

32-
# Verify the incoming request using the configured authentication method
33-
def validate_auth!(payload, headers, endpoint_config)
34-
auth_config = endpoint_config[:auth]
35-
auth_plugin_type = auth_config[:type].downcase
36-
secret_env_key = auth_config[:secret_env_key]
37-
38-
return unless secret_env_key
39-
40-
secret = ENV[secret_env_key]
41-
unless secret
42-
error!("secret '#{secret_env_key}' not found in environment", 500)
43-
end
44-
45-
auth_class = nil
46-
47-
case auth_plugin_type
48-
when "hmac"
49-
auth_class = Plugins::Auth::HMAC
50-
when "shared_secret"
51-
auth_class = Plugins::Auth::SharedSecret
52-
else
53-
error!("Custom validators not implemented in POC", 500)
54-
end
55-
56-
unless auth_class.valid?(
57-
payload:,
58-
headers:,
59-
secret:,
60-
config: endpoint_config
61-
)
62-
error!("authentication failed", 401)
63-
end
64-
end
65-
6632
# Parse request payload
6733
def parse_payload(raw_body, headers, symbolize: true)
6834
content_type = headers["Content-Type"] || headers["CONTENT_TYPE"] || headers["content-type"] || headers["HTTP_CONTENT_TYPE"]

0 commit comments

Comments
 (0)