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.



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

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 6

def errors
  @errors
end

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 6

def payload
  @payload
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 6

def request
  @request
end

#webhook_secretObject (readonly)

Returns the value of attribute webhook_secret.



6
7
8
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 6

def webhook_secret
  @webhook_secret
end

Instance Method Details

#error_messageObject



29
30
31
# File 'lib/coolhand/open_ai/webhook_validator.rb', line 29

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

#valid?Boolean

Returns:

  • (Boolean)


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

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

  return false unless payload_valid?
  return validate_in_non_production_env unless 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