Class: MasterCard::Security::OAuth::OAuthAuthentication

Inherits:
Authentication
  • Object
show all
Includes:
Core::Util
Defined in:
lib/mastercard/security/oauth.rb

Instance Method Summary collapse

Methods included from Core::Util

#base64Encode, #getReplacedPath, #normalizeParams, #normalizeUrl, #sha1Base64Encode, #sha256Base64Encode, #subMap, #uriRfc3986Encode, #validateURL

Constructor Details

#initialize(clientId, privateKey, key_alias, password) ⇒ OAuthAuthentication

Returns a new instance of OAuthAuthentication.



40
41
42
43
44
45
46
47
# File 'lib/mastercard/security/oauth.rb', line 40

def initialize(clientId,privateKey,key_alias,password)

  @clientId   = clientId
  @privateKey = privateKey
  @alias      = key_alias
  @password   = password

end

Instance Method Details

#getBaseString(url, method, params, oAuthParams) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mastercard/security/oauth.rb', line 81

def getBaseString(url,method,params,oAuthParams)
  #Merge the query string parameters
  unless params.nil?
    mergeParams = oAuthParams.merge(params)
  else
    mergeParams = oAuthParams
  end

  normalParams = normalizeParams(url,mergeParams)
  normalUrl    = normalizeUrl(url)
  method       = method.upcase

  return "#{uriRfc3986Encode(method)}&#{uriRfc3986Encode(normalUrl)}&#{uriRfc3986Encode(normalParams)}"
end

#getClientIdObject



49
50
51
# File 'lib/mastercard/security/oauth.rb', line 49

def getClientId
  return @clientId
end

#getOAuthBaseParameters(url, method, body) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mastercard/security/oauth.rb', line 57

def getOAuthBaseParameters(url, method, body)
  oAuthParameters = OAuthParameters.new
  oAuthParameters.setOAuthConsumerKey(@clientId)
  oAuthParameters.setOAuthNonce(Util.getNonce())
  oAuthParameters.setOAuthTimestamp(Util.getTimestamp())
  oAuthParameters.setOAuthSignatureMethod("RSA-SHA256")
  oAuthParameters.setOAuthVersion("1.0")

  if body.nil?
    body = ""
  end

  begin
    body = body.to_json
  rescue
    #Do nothing and hope for the best
  end

  encodedHash = sha256Base64Encode(body)
  oAuthParameters.setOAuthBodyHash(encodedHash)

  return oAuthParameters
end

#getOAuthKey(url, method, body, params) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mastercard/security/oauth.rb', line 105

def getOAuthKey(url,method,body,params)

  #Get all the base parameters such as nonce and timestamp
  oAuthBaseParameters = getOAuthBaseParameters(url,method,body)
  #Get the base string
  baseString = getBaseString(url, method, params,oAuthBaseParameters.getBaseParametersHash())

  #Sign the base string using the private key
  signature = signMessage(baseString)

  #Set the signature in the Base parameters
  oAuthBaseParameters.setOAuthSignature(signature)

  #Get the updated base parameteres dict
  oAuthBaseParametersHash = oAuthBaseParameters.getBaseParametersHash()

  paramStr = oAuthBaseParametersHash.map { |k,v|  "#{uriRfc3986Encode(k)}=\"#{uriRfc3986Encode(v.to_s)}\""}.join(",")

  #Generate the header value for OAuth Header
  return "#{OAuthParameters::OAUTH_KEY} #{paramStr}"


end

#getPrivateKeyObject



53
54
55
# File 'lib/mastercard/security/oauth.rb', line 53

def getPrivateKey
  return @privateKey
end

#signMessage(message) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mastercard/security/oauth.rb', line 130

def signMessage(message)
  #
  #Signs the message using the private key with sha1 as digest
  privateKeyFile = File.read(@privateKey)

  p12 = OpenSSL::PKCS12.new(privateKeyFile, @password)
  sign = p12.key.sign OpenSSL::Digest::SHA256.new, message

  return base64Encode(sign)

end

#signRequest(url, request, method, data, params) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/mastercard/security/oauth.rb', line 97

def signRequest(url,request,method,data,params)

  oauth_key = getOAuthKey(url,method,data,params)
  request.add_field(OAuthParameters::AUTHORIZATION,oauth_key)
  return request

end