Module: MAuth::Client::Signer

Included in:
MAuth::Client
Defined in:
lib/mauth/client/signer.rb

Constant Summary collapse

UNABLE_TO_SIGN_ERR =
UnableToSignError.new('mAuth client cannot sign without a private key!')

Instance Method Summary collapse

Instance Method Details

#signature_v1(string_to_sign) ⇒ Object



57
58
59
60
61
# 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) ⇒ Object



63
64
65
66
# 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 = {}) ⇒ Object

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



15
16
17
# File 'lib/mauth/client/signer.rb', line 15

def signed(object, attributes = {})
  object.merge_headers(signed_headers(object, attributes))
end

#signed_headers(object, attributes = {}) ⇒ Object

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



30
31
32
33
34
35
36
37
38
# 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 = {}) ⇒ Object



40
41
42
43
44
45
# 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 = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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 = {}) ⇒ Object

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



20
21
22
# 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 = {}) ⇒ Object



24
25
26
# File 'lib/mauth/client/signer.rb', line 24

def signed_v2(object, attributes = {})
  object.merge_headers(signed_headers_v2(object, attributes))
end