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



59
60
61
62
63
# File 'lib/mauth/client/signer.rb', line 59

def signature_v1(string_to_sign)
  assert_private_key(UNABLE_TO_SIGN_ERR)
  hashed_string_to_sign = OpenSSL::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



65
66
67
68
# File 'lib/mauth/client/signer.rb', line 65

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



17
18
19
# File 'lib/mauth/client/signer.rb', line 17

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.



32
33
34
35
36
37
38
39
40
# File 'lib/mauth/client/signer.rb', line 32

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



42
43
44
45
46
47
# File 'lib/mauth/client/signer.rb', line 42

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 = 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



49
50
51
52
53
54
55
56
57
# File 'lib/mauth/client/signer.rb', line 49

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 = 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.



22
23
24
# File 'lib/mauth/client/signer.rb', line 22

def signed_v1(object, attributes = {})
  object.merge_headers(signed_headers_v1(object, attributes))
end

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



26
27
28
# File 'lib/mauth/client/signer.rb', line 26

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