Class: Lago::Api::Resources::Webhook

Inherits:
Base
  • Object
show all
Defined in:
lib/lago/api/resources/webhook.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#create, #destroy, #get, #get_all, #initialize, #update, #whitelist_params

Constructor Details

This class inherits a constructor from Lago::Api::Resources::Base

Instance Method Details

#api_resourceObject



7
8
9
# File 'lib/lago/api/resources/webhook.rb', line 7

def api_resource
  'webhooks'
end

#public_keyObject



15
16
17
18
19
20
21
22
23
# File 'lib/lago/api/resources/webhook.rb', line 15

def public_key
  path = '/api/v1/webhooks/json_public_key'

  response = connection.get(path, identifier: nil)[root_name]

  webhook_details = JSON.parse(response.to_json, object_class: OpenStruct)

  OpenSSL::PKey::RSA.new(Base64.decode64(webhook_details.public_key))
end

#root_nameObject



11
12
13
# File 'lib/lago/api/resources/webhook.rb', line 11

def root_name
  'webhook'
end

#valid_signature?(signature, webhook_payload, cached_key = public_key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lago/api/resources/webhook.rb', line 25

def valid_signature?(signature, webhook_payload, cached_key = public_key)
  decoded_signature = JWT.decode(
    signature,
    cached_key,
    true,
    {
      algorithm: 'RS256',
      iss: client.api_url || Lago::Api::BASE_URL,
      verify_iss: true,
    },
  ).first

  decoded_signature['data'] == webhook_payload.to_json
rescue JWT::InvalidIssuerError, JWT::VerificationError
  false
end