Skip to content

Commit 7cc8162

Browse files
committed
New changes for MLE signature for Request for HTTP Signature
1 parent ce2b8fa commit 7cc8162

File tree

7 files changed

+348
-167
lines changed

7 files changed

+348
-167
lines changed

lib/AuthenticationSDK/authentication/jwt/JwtToken.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def getToken(merchantconfig_obj,gmtDatetime)
2525
jwtBody=getJwtBody(request_type, gmtDatetime, merchantconfig_obj)
2626
claimSet = JSON.parse(jwtBody)
2727

28-
cache_value = Cache.new.fetchJwtCertsAndKeys(merchantconfig_obj)
28+
cache_value = Cache.new.fetchCachedP12Certificate(merchantconfig_obj)
2929
privateKey = cache_value.private_key
3030
jwt_cert_obj = cache_value.cert
3131
jwt_cert_in_der= Base64.strict_encode64(jwt_cert_obj.to_der)

lib/AuthenticationSDK/core/MerchantConfig.rb

Lines changed: 97 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,64 @@
22
require_relative '../util/ExceptionHandler.rb'
33
require_relative '../logging/log_factory.rb'
44
require_relative '../logging/log_configuration.rb'
5+
require_relative '../util/CertificateUtility.rb'
56

67
public
78
# This fuction has all the merchantConfig properties getters and setters methods
89
class Merchantconfig
910
def initialize(cybsPropertyObj)
10-
# Common Parameters
11-
@merchantId = cybsPropertyObj['merchantID']
12-
@runEnvironment = cybsPropertyObj['runEnvironment']
13-
@intermediateHost = cybsPropertyObj['intermediateHost']
14-
@defaultDeveloperId = cybsPropertyObj['defaultDeveloperId']
15-
@authenticationType = cybsPropertyObj['authenticationType']
16-
@proxyAddress = cybsPropertyObj['proxyAddress']
17-
@proxyPort = cybsPropertyObj['proxyPort']
18-
@getId = ''
19-
@requestHost = ''
20-
@requestTarget = ''
21-
@requestJsonData = ''
22-
# HTTP Parameters
23-
@merchantSecretKey = cybsPropertyObj['merchantsecretKey']
24-
@merchantKeyId = cybsPropertyObj['merchantKeyId']
25-
# JWT Parameters
26-
@keysDirectory = cybsPropertyObj['keysDirectory']
27-
@keyAlias = cybsPropertyObj['keyAlias']
28-
@keyPass = cybsPropertyObj['keyPass']
29-
@keyFilename = cybsPropertyObj['keyFilename']
30-
@useMetaKey = cybsPropertyObj['useMetaKey']
31-
@portfolioID = cybsPropertyObj['portfolioID']
32-
@solutionId = cybsPropertyObj['solutionId']
33-
# MutualAuth & OAuth Parameters
34-
@enableClientCert = cybsPropertyObj['enableClientCert']
35-
@clientCertDirectory = cybsPropertyObj['clientCertDirectory']
36-
@sslClientCert = cybsPropertyObj['sslClientCert']
37-
@privateKey = cybsPropertyObj['privateKey']
38-
@sslKeyPassword = cybsPropertyObj['sslKeyPassword']
39-
@clientId = cybsPropertyObj['clientId']
40-
@clientSecret = cybsPropertyObj['clientSecret']
41-
@accessToken = cybsPropertyObj['accessToken']
42-
@refreshToken = cybsPropertyObj['refreshToken']
43-
# LogConfiguration
44-
@log_config = LogConfiguration.new(cybsPropertyObj['logConfiguration'])
45-
# Custom Default Headers
46-
@defaultCustomHeaders = cybsPropertyObj['defaultCustomHeaders']
47-
# Path to client JWE pem file directory
48-
@pemFileDirectory = cybsPropertyObj['pemFileDirectory']
49-
@mleKeyAlias = cybsPropertyObj['mleKeyAlias']
50-
@useMLEGlobally = cybsPropertyObj['useMLEGlobally']
51-
52-
if !cybsPropertyObj['mleForRequestPublicCertPath'].nil? && !cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip.empty?
53-
@mleForRequestPublicCertPath = cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip
54-
end
11+
# Common Parameters
12+
@merchantId = cybsPropertyObj['merchantID']
13+
@runEnvironment = cybsPropertyObj['runEnvironment']
14+
@intermediateHost = cybsPropertyObj['intermediateHost']
15+
@defaultDeveloperId = cybsPropertyObj['defaultDeveloperId']
16+
@authenticationType = cybsPropertyObj['authenticationType']
17+
@proxyAddress = cybsPropertyObj['proxyAddress']
18+
@proxyPort = cybsPropertyObj['proxyPort']
19+
@getId = ''
20+
@requestHost = ''
21+
@requestTarget = ''
22+
@requestJsonData = ''
23+
# HTTP Parameters
24+
@merchantSecretKey = cybsPropertyObj['merchantsecretKey']
25+
@merchantKeyId = cybsPropertyObj['merchantKeyId']
26+
# JWT Parameters
27+
@keysDirectory = cybsPropertyObj['keysDirectory']
28+
@keyAlias = cybsPropertyObj['keyAlias']
29+
@keyPass = cybsPropertyObj['keyPass']
30+
@keyFilename = cybsPropertyObj['keyFilename']
31+
@useMetaKey = cybsPropertyObj['useMetaKey']
32+
@portfolioID = cybsPropertyObj['portfolioID']
33+
@solutionId = cybsPropertyObj['solutionId']
34+
@p12KeyFilePath = nil
35+
# MutualAuth & OAuth Parameters
36+
@enableClientCert = cybsPropertyObj['enableClientCert']
37+
@clientCertDirectory = cybsPropertyObj['clientCertDirectory']
38+
@sslClientCert = cybsPropertyObj['sslClientCert']
39+
@privateKey = cybsPropertyObj['privateKey']
40+
@sslKeyPassword = cybsPropertyObj['sslKeyPassword']
41+
@clientId = cybsPropertyObj['clientId']
42+
@clientSecret = cybsPropertyObj['clientSecret']
43+
@accessToken = cybsPropertyObj['accessToken']
44+
@refreshToken = cybsPropertyObj['refreshToken']
45+
# LogConfiguration
46+
@log_config = LogConfiguration.new(cybsPropertyObj['logConfiguration'])
47+
# Custom Default Headers
48+
@defaultCustomHeaders = cybsPropertyObj['defaultCustomHeaders']
49+
# Path to client JWE pem file directory
50+
@pemFileDirectory = cybsPropertyObj['pemFileDirectory']
51+
@mleKeyAlias = cybsPropertyObj['mleKeyAlias']
52+
@useMLEGlobally = cybsPropertyObj['useMLEGlobally']
53+
54+
if !cybsPropertyObj['mleForRequestPublicCertPath'].nil? && !cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip.empty?
55+
@mleForRequestPublicCertPath = cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip
56+
end
5557

56-
@mapToControlMLEonAPI = cybsPropertyObj['mapToControlMLEonAPI']
57-
validateMerchantDetails
58-
validateMLEConfiguration
59-
logAllProperties(cybsPropertyObj)
58+
@mapToControlMLEonAPI = cybsPropertyObj['mapToControlMLEonAPI']
59+
validateMerchantDetails
60+
validateMLEConfiguration
61+
@p12KeyFilePath = File.join(@keysDirectory, @keyFilename + ".p12")
62+
logAllProperties(cybsPropertyObj)
6063
end
6164

6265
#fall back logic
@@ -162,6 +165,9 @@ def validateMerchantDetails()
162165
elsif !@keyFilename.instance_of? String
163166
@keyFilename=@keyFilename.to_s
164167
end
168+
if !check_key_file
169+
@log_obj.logger.error(ExceptionHandler.new.new_custom_error "Error finding or accessing the Key Directory or Key File. Please review the values in the merchant configuration.")
170+
end
165171
end
166172
if @authenticationType.upcase == Constants::AUTH_TYPE_MUTUAL_AUTH
167173
if @clientId.to_s.empty?
@@ -262,15 +268,24 @@ def validateMLEConfiguration
262268
@mleKeyAlias = Constants::DEFAULT_ALIAS_FOR_MLE_CERT
263269
end
264270

265-
# verify the input path for mle Cert should be correct else throw error in both case mle=true/false
266271
if @mleForRequestPublicCertPath && !@mleForRequestPublicCertPath.to_s.strip.empty?
267-
unless File.exist?(@mleForRequestPublicCertPath) && File.readable?(@mleForRequestPublicCertPath)
268-
err = StandardError.new(Constants::ERROR_PREFIX + "Invalid mleForRequestPublicCertPath: file does not exist or is not readable")
272+
begin
273+
CertificateUtility.validatePathAndFile(@mleForRequestPublicCertPath, "mleForRequestPublicCertPath", @log_config)
274+
rescue => err
269275
@log_obj.logger.error(ExceptionHandler.new.new_api_exception err)
270276
raise err
271277
end
272278
end
273279

280+
# # verify the input path for mle Cert should be correct else throw error in both case mle=true/false
281+
# if @mleForRequestPublicCertPath && [email protected]_s.strip.empty?
282+
# unless File.exist?(@mleForRequestPublicCertPath) && File.readable?(@mleForRequestPublicCertPath)
283+
# err = StandardError.new(Constants::ERROR_PREFIX + "Invalid mleForRequestPublicCertPath: file does not exist or is not readable")
284+
# @log_obj.logger.error(ExceptionHandler.new.new_api_exception err)
285+
# raise err
286+
# end
287+
# end
288+
274289
# mle_configured = @useMLEGlobally
275290
276291
# @mapToControlMLEonAPI.each do |_, value|
@@ -306,6 +321,35 @@ def logAllProperties(merchantPropertyObj)
306321
@log_obj.logger.info('Merchant Configuration :\n' + propertyObj.to_s)
307322
end
308323

324+
def check_key_file
325+
# Directory exists?
326+
unless Dir.exist?(@keysDirectory)
327+
@log_obj.logger.error("Keys Directory not found. Entered directory : #{@keysDirectory}")
328+
return false
329+
end
330+
331+
key_file_pathname = File.join(@keysDirectory, @keyFilename + ".p12")
332+
333+
# File exists?
334+
unless File.exist?(key_file_pathname)
335+
@log_obj.logger.error("Key File not found. Check path/filename entered. Entered path/filename : #{key_file_pathname}")
336+
return false
337+
end
338+
339+
@log_obj.logger.info("Entered value for Key File Path : #{key_file_pathname}")
340+
341+
# Can file be opened for reading?
342+
begin
343+
File.open(key_file_pathname, 'rb') do |f|
344+
# Just open and close
345+
end
346+
return true
347+
rescue => e
348+
@log_obj.logger.info("File cannot be accessed. Permission denied : #{key_file_pathname}")
349+
return false
350+
end
351+
end
352+
309353
# getter and setter methods
310354
attr_accessor :merchantId
311355
attr_accessor :merchantSecretKey
@@ -346,4 +390,5 @@ def logAllProperties(merchantPropertyObj)
346390
attr_accessor :mleForRequestPublicCertPath
347391
attr_accessor :mapToControlMLEonAPI
348392
attr_accessor :mleKeyAlias
393+
attr_accessor :p12KeyFilePath
349394
end

0 commit comments

Comments
 (0)