Class: MonopayRuby::Webhooks::Validator
- Inherits:
-
Object
- Object
- MonopayRuby::Webhooks::Validator
- Defined in:
- lib/monopay-ruby/webhooks/validator.rb
Instance Attribute Summary collapse
-
#error_messages ⇒ Object
readonly
Returns the value of attribute error_messages.
Instance Method Summary collapse
-
#initialize(request) ⇒ Validator
constructor
Initialize service.
-
#valid? ⇒ Boolean
Validate webhook data signature with public key.
Constructor Details
#initialize(request) ⇒ Validator
Initialize service
class SomeController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:webhook]
def webhook
validator = MonopayRuby::Webhooks::Validator.new(request)
if validator.valid?
# do something with webhook data
head :ok
else
flash[:error] = validator..join(", ")
# do something
head :unprocessable_entity
end
end
end
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/monopay-ruby/webhooks/validator.rb', line 29 def initialize(request) @request = request @webhook_data = @request.raw_post # @key = Rails.cache.fetch(:monobank_webhook_key) do # Monobank::Webhooks::KeyService.new.key # end @public_key_service = MonopayRuby::Webhooks::PublicKey.new @error_messages = [] end |
Instance Attribute Details
#error_messages ⇒ Object (readonly)
Returns the value of attribute error_messages.
4 5 6 |
# File 'lib/monopay-ruby/webhooks/validator.rb', line 4 def @error_messages end |
Instance Method Details
#valid? ⇒ Boolean
Validate webhook data signature with public key
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/monopay-ruby/webhooks/validator.rb', line 43 def valid? return false if @webhook_data.nil? return false if signature.nil? if @public_key_service.request_key @public_key ||= @public_key_service.key openssl_ec = OpenSSL::PKey::EC.new(@public_key) openssl_ec.check_key return true if openssl_ec.verify(digest, signature, @webhook_data) @error_messages << "Webhook aren't authorized. Might be signature is invalid or webhook data is modified." else @error_messages << @public_key_service. end false end |