lib/webhook_system/encoder.rb in webhook_system-2.4.0 vs lib/webhook_system/encoder.rb in webhook_system-2.4.1

- old
+ new

@@ -26,24 +26,22 @@ def self.decode(secret_string, payload_string, headers = {}) signature = headers['X-Hub-Signature'] format = format_for_content_type(headers.fetch('Content-Type')) payload_signature = hub_signature(payload_string, secret_string) - if signature && signature != payload_signature - raise DecodingError, 'signature mismatch' - end + raise DecodingError, 'signature mismatch' if signature && signature != payload_signature Payload.decode(payload_string, secret: secret_string, format: format) end class << self private def content_type_format_map { 'base64+aes256' => 'application/json; base64+aes256', - 'json' => 'application/json' + 'json' => 'application/json', } end def format_for_content_type(content_type) content_type_format_map.invert.fetch(content_type) @@ -52,11 +50,11 @@ def content_type_for_format(format) content_type_format_map.fetch(format) end def hub_signature(payload_string, secret) - 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload_string) + "sha1=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload_string)}" end end end module Payload @@ -71,11 +69,11 @@ raise ArgumentError, "don't know how to handle: #{payload['format']} payload" end end def decode(response_body, secret:, format:) - payload = JSON.load(response_body) + payload = JSON.parse(response_body) case format when 'base64+aes256' decode_aes(payload, secret) when 'json' @@ -86,11 +84,11 @@ end private def encode_aes(payload, secret) - cipher = OpenSSL::Cipher::AES256.new(:CBC) + cipher = OpenSSL::Cipher.new('aes-256-cbc') cipher.encrypt iv = cipher.random_iv cipher.key = key_from_secret(iv, secret) encoded = cipher.update(payload.to_json) + cipher.final @@ -103,16 +101,16 @@ def decode_aes(payload, secret) encoded = Base64.decode64(payload['payload']) iv = Base64.decode64(payload['iv']) - cipher = OpenSSL::Cipher::AES256.new(:CBC) + cipher = OpenSSL::Cipher.new('aes-256-cbc') cipher.decrypt cipher.iv = iv cipher.key = key_from_secret(iv, secret) decoded = cipher.update(encoded) + cipher.final - JSON.load(decoded) + JSON.parse(decoded) rescue OpenSSL::Cipher::CipherError raise DecodingError, 'Decoding Failed, probably mismatched secret' end def key_from_secret(iv, secret_string)