|
1 | 1 | require 'openssl'
|
2 | 2 | require 'base64'
|
| 3 | +require 'active_support' |
| 4 | +require 'thread' |
| 5 | +require_relative 'CacheValue' |
| 6 | + |
3 | 7 | public
|
4 | 8 | # P12 file certificate Cache
|
5 | 9 | class Cache
|
6 |
| - def fetchCachedCertificate(filePath, p12File, keyPass, keyAlias, cacheObj) |
7 |
| - certCache = cacheObj.read(keyAlias.to_s.upcase) |
8 |
| - cachedLastModifiedTimeStamp = cacheObj.read(keyAlias.to_s.upcase + '_LastModifiedTime') |
9 |
| - if File.exist?(filePath) |
10 |
| - currentFileLastModifiedTime = File.mtime(filePath) |
11 |
| - if certCache.to_s.empty? || cachedLastModifiedTimeStamp.to_s.empty? |
12 |
| - certificateFromP12File = getCertificate(p12File, keyPass, keyAlias, cacheObj, currentFileLastModifiedTime) |
13 |
| - return certificateFromP12File |
14 |
| - elsif currentFileLastModifiedTime > cachedLastModifiedTimeStamp |
15 |
| - # Function call to read the file and put values to new cache |
16 |
| - certificateFromP12File = getCertificate(p12File, keyPass, keyAlias, cacheObj, currentFileLastModifiedTime) |
17 |
| - return certificateFromP12File |
18 |
| - else |
19 |
| - return certCache |
| 10 | + @@cache_obj = ActiveSupport::Cache::MemoryStore.new |
| 11 | + @@mutex = Mutex.new |
| 12 | + |
| 13 | + def fetchJwtCertsAndKeys(merchantConfig) |
| 14 | + merchantId = merchantConfig.merchantId |
| 15 | + filePath = merchantConfig.keysDirectory + '/' + merchantConfig.keyFilename + '.p12' |
| 16 | + if (!File.exist?(filePath)) |
| 17 | + raise Constants::ERROR_PREFIX + Constants::FILE_NOT_FOUND + File.expand_path(filePath) |
| 18 | + end |
| 19 | + |
| 20 | + cacheKey = merchantId.to_s + '_JWT' |
| 21 | + |
| 22 | + # Thread-safe cache access with race condition protection |
| 23 | + @@mutex.synchronize do |
| 24 | + certCache = @@cache_obj.read(cacheKey) |
| 25 | + fileModTime = File.mtime(filePath) |
| 26 | + |
| 27 | + if !certCache || certCache.empty? || certCache.file_modified_time != fileModTime |
| 28 | + setupCache(cacheKey, filePath, merchantConfig) |
| 29 | + certCache = @@cache_obj.read(cacheKey) |
20 | 30 | end
|
21 |
| - else |
22 |
| - raise Constants::ERROR_PREFIX + Constants::FILE_NOT_FOUND + filePath |
| 31 | + |
| 32 | + return certCache |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def fetchMLECert(merchantConfig) |
| 37 | + merchantId = merchantConfig.merchantId |
| 38 | + filePath = merchantConfig.keysDirectory + '/' + merchantConfig.keyFilename + '.p12' |
| 39 | + if (!File.exist?(filePath)) |
| 40 | + raise Constants::ERROR_PREFIX + Constants::FILE_NOT_FOUND + File.expand_path(filePath) |
| 41 | + end |
| 42 | + |
| 43 | + cacheKey = merchantId.to_s + '_MLE' |
| 44 | + |
| 45 | + # Thread-safe cache access with race condition protection |
| 46 | + @@mutex.synchronize do |
| 47 | + certCache = @@cache_obj.read(cacheKey) |
| 48 | + fileModTime = File.mtime(filePath) |
| 49 | + |
| 50 | + if !certCache || certCache.empty? || certCache.file_modified_time != fileModTime |
| 51 | + setupCache(cacheKey, filePath, merchantConfig) |
| 52 | + certCache = @@cache_obj.read(cacheKey) |
| 53 | + end |
| 54 | + |
| 55 | + return certCache |
23 | 56 | end
|
24 | 57 | end
|
25 | 58 |
|
26 |
| - def getCertificate(p12File, keyPass, keyAlias, cacheObj, currentFileLastModifiedTime) |
27 |
| - x5CertDer = Utility.new.fetchCert(keyPass, p12File, keyAlias) |
28 |
| - cacheObj.write(keyAlias.to_s.upcase, x5CertDer) |
29 |
| - cacheObj.write(keyAlias.to_s.upcase + '_LastModifiedTime', currentFileLastModifiedTime) |
30 |
| - x5CertDer |
| 59 | + def setupCache(cacheKey, filePath, merchantConfig) |
| 60 | + if(cacheKey.end_with?("_JWT")) |
| 61 | + private_key, certs = getCertsAndKeysFromP12(filePath, merchantConfig) |
| 62 | + jwt_cert = Utility.getCertBasedOnKeyAlias(certs, merchantConfig.keyAlias) |
| 63 | + currentFileLastModifiedTime = File.mtime(filePath) |
| 64 | + |
| 65 | + # Create CacheValue object with all 3 parameters |
| 66 | + cache_value = CacheValue.new(private_key, jwt_cert, currentFileLastModifiedTime) |
| 67 | + |
| 68 | + # Store the cache value object in cache |
| 69 | + @@cache_obj.write(cacheKey, cache_value) |
| 70 | + end |
| 71 | + if(cacheKey.end_with?("_MLE")) |
| 72 | + private_key, certs = getCertsAndKeysFromP12(filePath, merchantConfig) |
| 73 | + mle_cert = Utility.getCertBasedOnKeyAlias(certs, merchantConfig.mleKeyAlias) |
| 74 | + currentFileLastModifiedTime = File.mtime(filePath) |
| 75 | + |
| 76 | + # Create CacheValue object with all 3 parameters |
| 77 | + cache_value = CacheValue.new(nil, mle_cert, currentFileLastModifiedTime) |
| 78 | + |
| 79 | + # Store the cache value object in cache |
| 80 | + @@cache_obj.write(cacheKey, cache_value) |
| 81 | + end |
| 82 | + |
| 83 | + end |
| 84 | + |
| 85 | + def getCertsAndKeysFromP12(filePath, merchantConfig) |
| 86 | + p12File = File.binread(filePath) |
| 87 | + p12Object = OpenSSL::PKCS12.new(p12File, merchantConfig.keyPass) |
| 88 | + #Generating Private Key |
| 89 | + private_key = OpenSSL::PKey::RSA.new(p12Object.key) |
| 90 | + # Generating Public key. |
| 91 | + # publicKey = OpenSSL::PKey::RSA.new(p12Object.key.public_key) |
| 92 | + |
| 93 | + # Get all certs from p12 |
| 94 | + x5_cert_primary = p12Object.certificate |
| 95 | + x5_certs_others = p12Object.ca_certs |
| 96 | + certs = [x5_cert_primary] |
| 97 | + certs.concat(x5_certs_others) if x5_certs_others |
| 98 | + return [private_key, certs] |
31 | 99 | end
|
32 | 100 |
|
33 | 101 | # <b>DEPRECATED:</b> This method has been marked as Deprecated and will be removed in coming releases.
|
34 |
| - def fetchPEMFileForNetworkTokenization(filePath, cacheObj) |
| 102 | + def fetchPEMFileForNetworkTokenization(filePath) |
35 | 103 | warn("[DEPRECATED] 'fetchPEMFileForNetworkTokenization' method is deprecated and will be removed in coming releases.")
|
36 |
| - pem_file_cache = cacheObj.read('privateKeyFromPEMFile') |
37 |
| - cached_pem_file_last_updated_time = cacheObj.read('cachedLastModifiedTimeOfPEMFile') |
38 |
| - if File.exist?(filePath) |
39 |
| - current_last_modified_time_of_PEM_file = File.mtime(filePath) |
40 |
| - if pem_file_cache.nil? || pem_file_cache.to_s.empty? || current_last_modified_time_of_PEM_file > cached_pem_file_last_updated_time |
41 |
| - private_key = JOSE::JWK.from_pem_file filePath |
42 |
| - cacheObj.write('privateKeyFromPEMFile', private_key) |
43 |
| - cacheObj.write('cachedLastModifiedTimeOfPEMFile', current_last_modified_time_of_PEM_file) |
| 104 | + |
| 105 | + # Thread-safe cache access for deprecated method |
| 106 | + @@mutex.synchronize do |
| 107 | + pem_file_cache = @@cache_obj.read('privateKeyFromPEMFile') |
| 108 | + cached_pem_file_last_updated_time = @@cache_obj.read('cachedLastModifiedTimeOfPEMFile') |
| 109 | + |
| 110 | + if File.exist?(filePath) |
| 111 | + current_last_modified_time_of_PEM_file = File.mtime(filePath) |
| 112 | + if pem_file_cache.nil? || pem_file_cache.to_s.empty? || current_last_modified_time_of_PEM_file > cached_pem_file_last_updated_time |
| 113 | + private_key = JOSE::JWK.from_pem_file filePath |
| 114 | + @@cache_obj.write('privateKeyFromPEMFile', private_key) |
| 115 | + @@cache_obj.write('cachedLastModifiedTimeOfPEMFile', current_last_modified_time_of_PEM_file) |
| 116 | + end |
44 | 117 | end
|
| 118 | + |
| 119 | + return @@cache_obj.read('privateKeyFromPEMFile') |
45 | 120 | end
|
46 |
| - return cacheObj.read('privateKeyFromPEMFile') |
47 | 121 | end
|
| 122 | + |
48 | 123 | end
|
0 commit comments