Class: Billy::Paddle::Webhooks::SignatureVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/billy/paddle/webhooks/signature_verifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ SignatureVerifier

Returns a new instance of SignatureVerifier.



9
10
11
12
# File 'lib/billy/paddle/webhooks/signature_verifier.rb', line 9

def initialize(data)
  @data = data
  @public_key_file = Billy.paddle_public_key_file
end

Instance Method Details

#verifyObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/billy/paddle/webhooks/signature_verifier.rb', line 14

def verify
  data = @data
  public_key = File.read(@public_key_file) if @public_key_file
  return false unless data && data["p_signature"] && public_key

  # 'data' represents all of the POST fields sent with the request.
  # Get the p_signature parameter & base64 decode it.
  signature = Base64.decode64(data["p_signature"])

  # Remove the p_signature parameter
  data.delete("p_signature")

  # Ensure all the data fields are strings
  data.each { |key, value| data[key] = String(value) }

  # Sort the data
  data_sorted = data.sort_by { |key, value| key }

  # and serialize the fields
  # serialization library is available here: https://github.com/jqr/php-serialize
  data_serialized = serialize(data_sorted, true)

  # verify the data
  digest = OpenSSL::Digest.new("SHA1")
  pub_key = OpenSSL::PKey::RSA.new(public_key)
  pub_key.verify(digest, signature, data_serialized)
end