Class: Coolhand::OpenAi::WebhookValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/coolhand/open_ai/webhook_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, webhook_secret) ⇒ WebhookValidator

Returns a new instance of WebhookValidator.



10
11
12
13
14
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 10

def initialize(request, webhook_secret)
  @request = request
  @errors = []
  @webhook_secret = webhook_secret
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 8

def errors
  @errors
end

#payloadObject (readonly)

Returns the value of attribute payload.



8
9
10
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 8

def payload
  @payload
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 8

def request
  @request
end

Instance Method Details

#error_messageObject



31
32
33
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 31

def error_message
  @errors.join(", ")
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 16

def valid?
  @errors.clear
  @payload = request.raw_post || request.body.read

  return false unless payload_valid?
  return validate_in_non_production_env unless Coolhand.required_field?(webhook_secret)

  secret_bytes = extract_secret_bytes
  webhook_signature, webhook_timestamp, webhook_id = extract_webhook_headers

  return validate_headers_in_non_production_env unless webhook_signature && webhook_timestamp

  verify_signature(webhook_signature, webhook_timestamp, webhook_id, secret_bytes)
end