22require_relative '../util/ExceptionHandler.rb'
33require_relative '../logging/log_factory.rb'
44require_relative '../logging/log_configuration.rb'
5+ require_relative '../util/CertificateUtility.rb'
56
67public
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- # Keep Alive Time for Connection Pooling
48- @keepAliveTime = cybsPropertyObj [ 'keepAliveTime' ] || 118 # Default to 118 seconds as same as default of libcurl
49- # Path to client JWE pem file directory
50- @pemFileDirectory = cybsPropertyObj [ 'pemFileDirectory' ]
51- @mleKeyAlias = cybsPropertyObj [ 'mleKeyAlias' ]
52- @useMLEGlobally = cybsPropertyObj [ 'useMLEGlobally' ]
53- @enableRequestMLEForOptionalApisGlobally = !!( cybsPropertyObj [ 'enableRequestMLEForOptionalApisGlobally' ] || cybsPropertyObj [ 'useMLEGlobally' ] )
54- @disableRequestMLEForMandatoryApisGlobally = cybsPropertyObj [ 'disableRequestMLEForMandatoryApisGlobally' ]
55- @mapToControlMLEonAPI = cybsPropertyObj [ 'mapToControlMLEonAPI' ]
56- validateMerchantDetails
57- logAllProperties ( cybsPropertyObj )
58- validateMLEConfiguration ( cybsPropertyObj )
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+ # Keep Alive Time for Connection Pooling
50+ @keepAliveTime = cybsPropertyObj [ 'keepAliveTime' ] || 118 # Default to 118 seconds as same as default of libcurl
51+ # Path to client JWE pem file directory
52+ @pemFileDirectory = cybsPropertyObj [ 'pemFileDirectory' ]
53+ @mleKeyAlias = cybsPropertyObj [ 'mleKeyAlias' ]
54+ @useMLEGlobally = cybsPropertyObj [ 'useMLEGlobally' ]
55+ @enableRequestMLEForOptionalApisGlobally = !!( cybsPropertyObj [ 'enableRequestMLEForOptionalApisGlobally' ] || cybsPropertyObj [ 'useMLEGlobally' ] )
56+ @disableRequestMLEForMandatoryApisGlobally = cybsPropertyObj [ 'disableRequestMLEForMandatoryApisGlobally' ]
57+
58+
59+ if !cybsPropertyObj [ 'mleForRequestPublicCertPath' ] . nil? && !cybsPropertyObj [ 'mleForRequestPublicCertPath' ] . to_s . strip . empty?
60+ @mleForRequestPublicCertPath = cybsPropertyObj [ 'mleForRequestPublicCertPath' ] . to_s . strip
61+ end
62+
63+ @mapToControlMLEonAPI = cybsPropertyObj [ 'mapToControlMLEonAPI' ]
64+ validateMerchantDetails
65+ validateMLEConfiguration ( cybsPropertyObj )
66+ @p12KeyFilePath = File . join ( @keysDirectory , @keyFilename + ".p12" )
67+ logAllProperties ( cybsPropertyObj )
5968 end
6069
6170 #fall back logic
@@ -166,6 +175,9 @@ def validateMerchantDetails()
166175 elsif !@keyFilename . instance_of? String
167176 @keyFilename = @keyFilename . to_s
168177 end
178+ if !check_key_file
179+ @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." )
180+ end
169181 end
170182 if @authenticationType . upcase == Constants ::AUTH_TYPE_MUTUAL_AUTH
171183 if @clientId . to_s . empty?
@@ -280,21 +292,39 @@ def validateMLEConfiguration(cybsPropertyObj)
280292 @mleKeyAlias = Constants ::DEFAULT_ALIAS_FOR_MLE_CERT
281293 end
282294
283- mle_configured = @enableRequestMLEForOptionalApisGlobally
284- if !@mapToControlMLEonAPI . nil? && !@mapToControlMLEonAPI . empty?
285- @mapToControlMLEonAPI . each do |_ , value |
286- unless [ true , false ] . include? ( value ) && value
287- mle_configured = true
288- break
289- end
295+ if @mleForRequestPublicCertPath && !@mleForRequestPublicCertPath . to_s . strip . empty?
296+ begin
297+ CertificateUtility . validatePathAndFile ( @mleForRequestPublicCertPath , "mleForRequestPublicCertPath" , @log_config )
298+ rescue => err
299+ @log_obj . logger . error ( ExceptionHandler . new . new_api_exception err )
300+ raise err
290301 end
291302 end
292303
293- if mle_configured && !Constants ::AUTH_TYPE_JWT . eql? ( @authenticationType . upcase )
294- err = StandardError . new ( Constants ::ERROR_PREFIX + "MLE can only be used with JWT authentication" )
295- @log_obj . logger . error ( ExceptionHandler . new . new_api_exception err )
296- raise err
297- end
304+ # # verify the input path for mle Cert should be correct else throw error in both case mle=true/false
305+ # if @mleForRequestPublicCertPath && [email protected] _s.strip.empty? 306+ # unless File.exist?(@mleForRequestPublicCertPath) && File.readable?(@mleForRequestPublicCertPath)
307+ # err = StandardError.new(Constants::ERROR_PREFIX + "Invalid mleForRequestPublicCertPath: file does not exist or is not readable")
308+ # @log_obj.logger.error(ExceptionHandler.new.new_api_exception err)
309+ # raise err
310+ # end
311+ # end
312+
313+ # mle_configured = @enableRequestMLEForOptionalApisGlobally
314+ 315+ # @mapToControlMLEonAPI.each do |_, value|
316+ # unless [true, false].include?(value) && value
317+ # mle_configured = true
318+ # break
319+ # end
320+ # end
321+ # end
322+
323+ # if mle_configured && !Constants::AUTH_TYPE_JWT.eql?(@authenticationType.upcase)
324+ # err = StandardError.new(Constants::ERROR_PREFIX + "MLE can only be used with JWT authentication")
325+ # @log_obj.logger.error(ExceptionHandler.new.new_api_exception err)
326+ # raise err
327+ # end
298328 end
299329
300330 def logAllProperties ( merchantPropertyObj )
@@ -315,6 +345,35 @@ def logAllProperties(merchantPropertyObj)
315345 @log_obj . logger . info ( 'Merchant Configuration :\n' + propertyObj . to_s )
316346 end
317347
348+ def check_key_file
349+ # Directory exists?
350+ unless Dir . exist? ( @keysDirectory )
351+ @log_obj . logger . error ( "Keys Directory not found. Entered directory : #{ @keysDirectory } " )
352+ return false
353+ end
354+
355+ key_file_pathname = File . join ( @keysDirectory , @keyFilename + ".p12" )
356+
357+ # File exists?
358+ unless File . exist? ( key_file_pathname )
359+ @log_obj . logger . error ( "Key File not found. Check path/filename entered. Entered path/filename : #{ key_file_pathname } " )
360+ return false
361+ end
362+
363+ @log_obj . logger . info ( "Entered value for Key File Path : #{ key_file_pathname } " )
364+
365+ # Can file be opened for reading?
366+ begin
367+ File . open ( key_file_pathname , 'rb' ) do |f |
368+ # Just open and close
369+ end
370+ return true
371+ rescue => e
372+ @log_obj . logger . info ( "File cannot be accessed. Permission denied : #{ key_file_pathname } " )
373+ return false
374+ end
375+ end
376+
318377 # getter and setter methods
319378 attr_accessor :merchantId
320379 attr_accessor :merchantSecretKey
@@ -355,6 +414,8 @@ def logAllProperties(merchantPropertyObj)
355414 attr_accessor :useMLEGlobally
356415 attr_accessor :enableRequestMLEForOptionalApisGlobally
357416 attr_accessor :disableRequestMLEForMandatoryApisGlobally
417+ attr_accessor :mleForRequestPublicCertPath
358418 attr_accessor :mapToControlMLEonAPI
359419 attr_accessor :mleKeyAlias
420+ attr_accessor :p12KeyFilePath
360421 end
0 commit comments