Class: AlexaRuby::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/alexa_ruby/request/base_request/validator.rb

Overview

Validator is responsible for Amazon request validation:

- SignatureCertChainUrl validation
- Amazon Alexa request signature validation

Constant Summary collapse

TIMESTAMP_TOLERANCE =
150

Instance Method Summary collapse

Constructor Details

#initialize(cert_chain_url, signature, request, timestamp_diff = nil) ⇒ Validator

Setup new validator

Parameters:

  • cert_chain_url (String)

    SSL certificates chain URI

  • signature (String)

    HTTP request signature

  • request (Object)

    json request

  • timestamp_diff (Integer) (defaults to: nil)

    valid distance in seconds between current time and the request timestamp


17
18
19
20
21
22
# File 'lib/alexa_ruby/request/base_request/validator.rb', line 17

def initialize(cert_chain_url, signature, request, timestamp_diff = nil)
  @chain_url = cert_chain_url
  @signature = signature
  @request = request
  @timestamp_diff = timestamp_diff || TIMESTAMP_TOLERANCE
end

Instance Method Details

#valid_request?Boolean

Check if it is a valid Amazon request

Returns:

  • (Boolean)

27
28
29
30
31
32
33
34
# File 'lib/alexa_ruby/request/base_request/validator.rb', line 27

def valid_request?
  unless timestamp_tolerant?
    raise ArgumentError,
          'Outdated request: request timestamp is more than ' \
          "#{@timestamp_diff} seconds later than current time"
  end
  valid_uri? && valid_certificates?
end