module MAuth::Client::Signer

Constants

UNABLE_TO_SIGN_ERR

Public Instance Methods

signature_v1(string_to_sign) click to toggle source
# File lib/mauth/client/signer.rb, line 57
def signature_v1(string_to_sign)
  assert_private_key(UNABLE_TO_SIGN_ERR)
  hashed_string_to_sign = Digest::SHA512.hexdigest(string_to_sign)
  Base64.encode64(private_key.private_encrypt(hashed_string_to_sign)).delete("\n")
end
signature_v2(string_to_sign) click to toggle source
# File lib/mauth/client/signer.rb, line 63
def signature_v2(string_to_sign)
  assert_private_key(UNABLE_TO_SIGN_ERR)
  Base64.encode64(private_key.sign(SIGNING_DIGEST, string_to_sign)).delete("\n")
end
signed(object, attributes = {}) click to toggle source

takes an outgoing request or response object, and returns an object of the same class whose headers are updated to include mauth's signature headers

# File lib/mauth/client/signer.rb, line 15
def signed(object, attributes = {})
  object.merge_headers(signed_headers(object, attributes))
end
signed_headers(object, attributes = {}) click to toggle source

takes a signable object (outgoing request or response). returns a hash of headers to be applied to the object which comprises its signature.

# File lib/mauth/client/signer.rb, line 30
def signed_headers(object, attributes = {})
  if v2_only_sign_requests?
    signed_headers_v2(object, attributes)
  elsif v1_only_sign_requests?
    signed_headers_v1(object, attributes)
  else # by default sign with both the v1 and v2 protocol
    signed_headers_v1(object, attributes).merge(signed_headers_v2(object, attributes))
  end
end
signed_headers_v1(object, attributes = {}) click to toggle source
# File lib/mauth/client/signer.rb, line 40
def signed_headers_v1(object, attributes = {})
  attributes = { time: Time.now.to_i.to_s, app_uuid: client_app_uuid }.merge(attributes)
  string_to_sign = object.string_to_sign_v1(attributes)
  signature = self.signature_v1(string_to_sign)
  { 'X-MWS-Authentication' => "#{MWS_TOKEN} #{client_app_uuid}:#{signature}", 'X-MWS-Time' => attributes[:time] }
end
signed_headers_v2(object, attributes = {}) click to toggle source
# File lib/mauth/client/signer.rb, line 47
def signed_headers_v2(object, attributes = {})
  attributes = { time: Time.now.to_i.to_s, app_uuid: client_app_uuid }.merge(attributes)
  string_to_sign = object.string_to_sign_v2(attributes)
  signature = self.signature_v2(string_to_sign)
  {
    'MCC-Authentication' => "#{MWSV2_TOKEN} #{client_app_uuid}:#{signature}#{AUTH_HEADER_DELIMITER}",
    'MCC-Time' => attributes[:time]
  }
end
signed_v1(object, attributes = {}) click to toggle source

signs with v1 only. used when signing responses to v1 requests.

# File lib/mauth/client/signer.rb, line 20
def signed_v1(object, attributes = {})
  object.merge_headers(signed_headers_v1(object, attributes))
end
signed_v2(object, attributes = {}) click to toggle source
# File lib/mauth/client/signer.rb, line 24
def signed_v2(object, attributes = {})
  object.merge_headers(signed_headers_v2(object, attributes))
end