class BarkingIguana::Verify::Signature

Constants

DIGEST
MAXIMUM_VALIDITY
SEPARATOR

Attributes

action[RW]
expires_at[RW]
public_key[RW]
secret[RW]
signed_at[RW]

Public Class Methods

new(public_key, action, secret, expires_at, signed_at = nil) click to toggle source
# File lib/barking_iguana/verify/signature.rb, line 24
def initialize public_key, action, secret, expires_at, signed_at = nil
  self.public_key = public_key
  self.action     = action
  self.secret     = secret
  self.expires_at = expires_at
  self.signed_at  = signed_at
end
verify(ascii, action, secret, now = Time.now) click to toggle source

Verify that a signature is valid, not expired, and not for an insanely far future date.

# File lib/barking_iguana/verify/signature.rb, line 57
def self.verify ascii, action, secret, now = Time.now
  this_second = Time.at now.to_i
  params = Base64.decode64 ascii
  _, public_key, expires, signed = params.split /#{SEPARATOR}/, 4
  expires_at = Time.at expires.to_i
  signed_at = Time.at signed.to_i
  raise WindowTooLarge.new "Time between now and expiry is more than #{MAXIMUM_VALIDITY}" if expires_at - signed_at > MAXIMUM_VALIDITY
  raise SignatureExpired.new "#{expires_at} vs #{this_second}" if this_second > expires_at
  raise FarFutureExpiry.new "#{expires} vs #{this_second + MAXIMUM_VALIDITY}" if expires_at > this_second + MAXIMUM_VALIDITY
  expected_signature = Signature.new public_key, action, secret,
    expires_at, signed_at
  raise TokenMismatch.new "#{expected_signature.to_s} vs #{ascii}" if expected_signature.to_s != ascii
end

Public Instance Methods

inspect() click to toggle source
# File lib/barking_iguana/verify/signature.rb, line 47
def inspect
  s = "#<#{self.class.name}: @public_key=#{public_key.inspect}, @action=#{action.inspect}, @secret=(hidden), @expires_at=#{expires_at.inspect}"
  unless signed_at.nil?
    s += ", @signed_at=#{signed_at.inspect}"
  end
  s + '>'
end
to_s() click to toggle source

Get an ASCII representation of this signature

# File lib/barking_iguana/verify/signature.rb, line 36
def to_s
  signed = (signed_at || Time.now).to_i.to_s
  expires = expires_at.to_i.to_s
  signature = "#{public_key}#{expires}#{signed}#{action}"
  token = OpenSSL::HMAC.hexdigest DIGEST, secret, signature
  encoded_token = Base64.encode64(token)
  encoded_token.gsub! /\n/, ''
  params = [ encoded_token, public_key, expires, signed ].join SEPARATOR
  Base64.encode64(params).gsub(/\n/, '')
end