Class: OpenAI::Resources::Webhooks
- Inherits:
-
Object
- Object
- OpenAI::Resources::Webhooks
- Defined in:
- lib/openai/resources/webhooks.rb,
sig/openai/resources/webhooks.rbs
Instance Method Summary collapse
-
#initialize(client:) ⇒ Webhooks
constructor
private
A new instance of Webhooks.
-
#unwrap(payload, headers = {}, webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"]) ⇒ OpenAI::Models::Webhooks::BatchCancelledWebhookEvent, ...
Validates that the given payload was sent by OpenAI and parses the payload.
-
#verify_signature(payload, headers, webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"], tolerance = 300) ⇒ Object
Validates whether or not the webhook payload was sent by OpenAI.
Constructor Details
#initialize(client:) ⇒ Webhooks
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Webhooks.
128 129 130 |
# File 'lib/openai/resources/webhooks.rb', line 128 def initialize(client:) @client = client end |
Instance Method Details
#unwrap(payload, headers = {}, webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"]) ⇒ OpenAI::Models::Webhooks::BatchCancelledWebhookEvent, ...
Validates that the given payload was sent by OpenAI and parses the payload.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/openai/resources/webhooks.rb', line 15 def unwrap( payload, headers = {}, webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"] ) verify_signature(payload, headers, webhook_secret) parsed = JSON.parse(payload, symbolize_names: true) OpenAI::Internal::Type::Converter.coerce(OpenAI::Models::Webhooks::UnwrapWebhookEvent, parsed) end |
#verify_signature(payload, headers, webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"], tolerance = 300) ⇒ Object
Validates whether or not the webhook payload was sent by OpenAI.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/openai/resources/webhooks.rb', line 34 def verify_signature( payload, headers, webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"], tolerance = 300 ) if webhook_secret.nil? || webhook_secret.strip.empty? raise ArgumentError, "The webhook secret must either be set using the env var, OPENAI_WEBHOOK_SECRET, " \ "or passed to this function" end header_names = %w[webhook-signature webhook-timestamp webhook-id] normalized_headers = {} headers.each do |name, value| name = name.to_s.downcase.delete_prefix("http_").tr("_", "-") next unless header_names.include?(name) if normalized_headers.key?(name) && normalized_headers[name] != value raise ArgumentError, "Conflicting values for #{name} header" end normalized_headers[name] = value end signature_header, , webhook_id = normalized_headers.values_at(*header_names) if signature_header.nil? raise ArgumentError, "Missing required webhook-signature header" end if .nil? raise ArgumentError, "Missing required webhook-timestamp header" end if webhook_id.nil? raise ArgumentError, "Missing required webhook-id header" end # Validate timestamp to prevent replay attacks begin = .to_i rescue ArgumentError raise ArgumentError, "Invalid webhook timestamp format" end now = Time.now.to_i if now - > tolerance raise OpenAI::Errors::InvalidWebhookSignatureError, "Webhook timestamp is too old" end if > now + tolerance raise OpenAI::Errors::InvalidWebhookSignatureError, "Webhook timestamp is too new" end # Extract signatures from v1,<base64> format # The signature header can have multiple values, separated by spaces. # Each value is in the format v1,<base64>. We should accept if any match. signatures = signature_header.split.map do |part| if part.start_with?("v1,") part[3..] else part end end # Decode the secret if it starts with whsec_ decoded_secret = if webhook_secret.start_with?("whsec_") Base64.strict_decode64(webhook_secret[6..]) else webhook_secret end raise ArgumentError, "The webhook secret must not be empty" if decoded_secret.empty? # Create the signed payload: {webhook_id}.{timestamp}.{payload} signed_payload = "#{webhook_id}.#{}.#{payload}" # Compute HMAC-SHA256 signature expected_signature = Base64.encode64( OpenSSL::HMAC.digest("sha256", decoded_secret, signed_payload) ).strip # Accept if any signature matches using timing-safe comparison return if signatures.any? { |signature| OpenSSL.secure_compare(expected_signature, signature) } raise OpenAI::Errors::InvalidWebhookSignatureError, "The given webhook signature does not match the expected signature" end |