Class: Lithic::Resources::Webhooks
- Inherits:
-
Object
- Object
- Lithic::Resources::Webhooks
- Defined in:
- lib/lithic/resources/webhooks.rb
Instance Method Summary collapse
-
#initialize(client:) ⇒ Webhooks
constructor
private
A new instance of Webhooks.
-
#parse(payload, headers:, secret: nil) ⇒ Lithic::Models::AccountHolderCreatedWebhookEvent, ...
Parses and verifies a webhook payload.
-
#parse_unsafe(payload) ⇒ Lithic::Models::AccountHolderCreatedWebhookEvent, ...
Parses a webhook payload without verifying the signature.
-
#verify_signature(payload:, headers:, secret: nil) ⇒ Hash
Verifies the signature of a webhook payload using the Standard Webhooks specification.
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.
82 83 84 |
# File 'lib/lithic/resources/webhooks.rb', line 82 def initialize(client:) @client = client end |
Instance Method Details
#parse(payload, headers:, secret: nil) ⇒ Lithic::Models::AccountHolderCreatedWebhookEvent, ...
Parses and verifies a webhook payload. Verifies the signature before parsing.
23 24 25 26 |
# File 'lib/lithic/resources/webhooks.rb', line 23 def parse(payload, headers:, secret: nil) verified_json = verify_signature(payload: payload, headers: headers, secret: secret) Lithic::Internal::Type::Converter.coerce(Lithic::Models::ParsedWebhookEvent, verified_json) end |
#parse_unsafe(payload) ⇒ Lithic::Models::AccountHolderCreatedWebhookEvent, ...
Parses a webhook payload without verifying the signature.
WARNING: This method does not verify the webhook signature. Use only for testing or when signature verification is not required.
36 37 38 39 |
# File 'lib/lithic/resources/webhooks.rb', line 36 def parse_unsafe(payload) parsed = JSON.parse(payload, symbolize_names: true) Lithic::Internal::Type::Converter.coerce(Lithic::Models::ParsedWebhookEvent, parsed) end |
#verify_signature(payload:, headers:, secret: nil) ⇒ Hash
Verifies the signature of a webhook payload using the Standard Webhooks specification.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lithic/resources/webhooks.rb', line 60 def verify_signature(payload:, headers:, secret: nil) secret ||= ENV["LITHIC_WEBHOOK_SECRET"] if secret.nil? || secret.empty? raise ArgumentError, "Webhook secret must be provided or set in LITHIC_WEBHOOK_SECRET environment variable" end begin require("standardwebhooks") rescue LoadError raise Lithic::Errors::MissingDependencyError.new( gem_name: "standardwebhooks", feature: "webhook signature verification" ) end wh = StandardWebhooks::Webhook.new(secret) wh.verify(payload, headers) end |