Class: Twilio::Util::RequestValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-ruby/util/request_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_token = nil) ⇒ RequestValidator

Returns a new instance of RequestValidator.

Raises:

  • (ArgumentError)


5
6
7
8
# File 'lib/twilio-ruby/util/request_validator.rb', line 5

def initialize(auth_token = nil)
  @auth_token = auth_token || Twilio.auth_token
  raise ArgumentError, 'Auth token is required' if @auth_token.nil?
end

Instance Method Details

#build_signature_for(url, params) ⇒ Object



15
16
17
18
19
# File 'lib/twilio-ruby/util/request_validator.rb', line 15

def build_signature_for(url, params)
  data = url + params.sort.join
  digest = OpenSSL::Digest.new('sha1')
  Base64.encode64(OpenSSL::HMAC.digest(digest, @auth_token, data)).strip
end

#validate(url, params, signature) ⇒ Object



10
11
12
13
# File 'lib/twilio-ruby/util/request_validator.rb', line 10

def validate(url, params, signature)
  expected = build_signature_for url, params
  secure_compare(expected, signature)
end