Class: CloudConvert::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudconvert/webhook.rb

Defined Under Namespace

Modules: Processor

Constant Summary collapse

Error =

Raised when a webhook is malformed

Class.new(CloudConvert::Error)
InvalidSignature =

Raised when a webhook does not match the CloudConvert-Signature

Class.new(Error)
MissingSignature =

Raised when a webhook does not have a CloudConvert-Signature

Class.new(Error)

Class Method Summary collapse

Class Method Details

.event(payload) ⇒ Event

Parameters:

  • payload (String)

    The full request body (the JSON string) of our request to the webhook URL.

Returns:



15
16
17
# File 'lib/cloudconvert/webhook.rb', line 15

def event(payload)
  Event.new(JSON.parse(payload, object_class: OpenStruct))
end

.verify(payload, signature, secret) ⇒ Boolean

Parameters:

  • payload (String)

    The full request body (the JSON string) of our request to the webhook URL.

  • signature (String)

    The value from the CloudConvert-Signature.

  • secret (String)

    The signing secret from for your webhook settings.

Returns:

  • (Boolean)


23
24
25
# File 'lib/cloudconvert/webhook.rb', line 23

def verify(payload, signature, secret)
  OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, payload.to_s) == signature
end

.verify!(payload, signature, secret) ⇒ Boolean

Parameters:

  • payload (String)

    The full request body (the JSON string) of our request to the webhook URL.

  • signature (String)

    The value from the CloudConvert-Signature.

  • secret (String)

    The signing secret from for your webhook settings.

Returns:

  • (Boolean)

Raises:



32
33
34
35
36
# File 'lib/cloudconvert/webhook.rb', line 32

def verify!(payload, signature, secret)
  raise MissingSignature.new("Missing webhook signature") if signature.to_s.empty?
  raise InvalidSignature.new("Invalid webhook signature") unless verify(payload, signature, secret)
  true
end

.verify_request(request, secret) ⇒ Boolean

Parameters:

  • request (Request)

    The request to the webhook URL from CloudConvert.

  • secret (String)

    The signing secret from for your webhook settings.

Returns:

  • (Boolean)


41
42
43
# File 'lib/cloudconvert/webhook.rb', line 41

def verify_request(request, secret)
  verify request.body.rewind && request.body.read, request.get_header("HTTP_CLOUDCONVERT_SIGNATURE"), secret
end

.verify_request!(request, secret) ⇒ Boolean

Parameters:

  • request (Request)

    The request to the webhook URL from CloudConvert.

  • secret (String)

    The signing secret from for your webhook settings.

Returns:

  • (Boolean)

Raises:



49
50
51
# File 'lib/cloudconvert/webhook.rb', line 49

def verify_request!(request, secret)
  verify! request.body.rewind && request.body.read, request.get_header("HTTP_CLOUDCONVERT_SIGNATURE"), secret
end