Class: MasterCard::Security::OAuth::OAuthAuthentication
Instance Method Summary
collapse
-
#getBaseString(url, method, params, oAuthParams) ⇒ Object
-
#getClientId ⇒ Object
-
#getOAuthBaseParameters(url, method, body) ⇒ Object
-
#getOAuthKey(url, method, body, params) ⇒ Object
-
#getPrivateKey ⇒ Object
-
#initialize(clientId, privateKey, key_alias, password) ⇒ OAuthAuthentication
constructor
A new instance of OAuthAuthentication.
-
#signMessage(message) ⇒ Object
-
#signRequest(url, request, method, data, params) ⇒ Object
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)
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
|
#getClientId ⇒ Object
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
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)
oAuthBaseParameters = getOAuthBaseParameters(url,method,body)
baseString = getBaseString(url, method, params,oAuthBaseParameters.getBaseParametersHash())
signature = signMessage(baseString)
oAuthBaseParameters.setOAuthSignature(signature)
oAuthBaseParametersHash = oAuthBaseParameters.getBaseParametersHash()
paramStr = oAuthBaseParametersHash.map { |k,v| "#{uriRfc3986Encode(k)}=\"#{uriRfc3986Encode(v.to_s)}\""}.join(",")
return "#{OAuthParameters::OAUTH_KEY} #{paramStr}"
end
|
#getPrivateKey ⇒ Object
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)
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
|