|
5 | 5 | require 'active_support' |
6 | 6 | require_relative '../../core/ITokenGeneration.rb' |
7 | 7 | require_relative '../../util/Constants.rb' |
8 | | -require_relative '../../util/ApiException.rb' |
| 8 | +require_relative '../../util/ExceptionHandler.rb' |
9 | 9 | require_relative '../../util/Cache.rb' |
10 | 10 | require_relative '../../authentication/payloadDigest/digest.rb' |
| 11 | +require_relative '../../logging/log_factory.rb' |
11 | 12 | public |
12 | | -#JWT Token-generated based on the Request type |
13 | 13 | class GenerateJwtToken |
14 | | - def getToken(merchantconfig_obj,gmtDatetime,log_obj) |
| 14 | + @log_obj |
| 15 | + |
| 16 | + #JWT Token-generated based on the Request type |
| 17 | + def getToken(merchantconfig_obj,gmtDatetime) |
| 18 | + @log_obj = Log.new merchantconfig_obj.log_config, "JwtToken" |
| 19 | + |
15 | 20 | jwtBody = '' |
16 | 21 | request_type = merchantconfig_obj.requestType.upcase |
17 | 22 | filePath = merchantconfig_obj.keysDirectory + '/' + merchantconfig_obj.keyFilename + '.p12' |
| 23 | + |
18 | 24 | if (!File.exist?(filePath)) |
19 | 25 | raise Constants::ERROR_PREFIX + Constants::FILE_NOT_FOUND + File.expand_path(filePath) |
20 | 26 | end |
| 27 | + |
21 | 28 | p12File = File.binread(filePath) |
22 | | - jwtBody=getJwtBody(request_type, gmtDatetime, merchantconfig_obj, log_obj) |
| 29 | + jwtBody=getJwtBody(request_type, gmtDatetime, merchantconfig_obj) |
23 | 30 | claimSet = JSON.parse(jwtBody) |
24 | 31 | p12FilePath = OpenSSL::PKCS12.new(p12File, merchantconfig_obj.keyPass) |
| 32 | + |
25 | 33 | # Generating certificate. |
26 | 34 | cacheObj = ActiveSupport::Cache::MemoryStore.new |
27 | 35 | x5Cert = Cache.new.fetchCachedCertificate(filePath, p12File, merchantconfig_obj.keyPass, cacheObj) |
| 36 | + |
28 | 37 | # Generating Public key. |
29 | 38 | publicKey = OpenSSL::PKey::RSA.new(p12FilePath.key.public_key) |
| 39 | + |
30 | 40 | #Generating Private Key |
31 | 41 | privateKey = OpenSSL::PKey::RSA.new(p12FilePath.key) |
| 42 | + |
32 | 43 | # JWT token-Generates using RS256 algorithm only |
33 | 44 | x5clist = [x5Cert] |
34 | 45 | customHeaders = {} |
35 | 46 | customHeaders['v-c-merchant-id'] = merchantconfig_obj.keyAlias |
36 | 47 | customHeaders['x5c'] = x5clist |
| 48 | + |
37 | 49 | # Generating JWT token |
38 | 50 | token = JWT.encode(claimSet, privateKey, 'RS256', customHeaders) |
39 | 51 | return token |
40 | 52 | rescue StandardError => err |
41 | 53 | if err.message.include? 'PKCS12_parse: mac verify failure' |
42 | | - ApiException.new.customerror(Constants::ERROR_PREFIX + Constants::INCORRECT_KEY_PASS,log_obj) |
43 | | - exit! |
| 54 | + @log_obj.logger.error(ExceptionHandler.new.new_custom_error Constants::ERROR_PREFIX + Constants::INCORRECT_KEY_PASS) |
| 55 | + # exit! |
44 | 56 | else |
45 | | - ApiException.new.apiexception(err,log_obj) |
46 | | - exit! |
| 57 | + @log_obj.logger.error(ExceptionHandler.new.new_api_exception err) |
| 58 | + # exit! |
47 | 59 | end |
| 60 | + raise err |
48 | 61 | end |
49 | | - def getJwtBody(request_type, gmtDatetime, merchantconfig_obj,log_obj) |
| 62 | + |
| 63 | + def getJwtBody(request_type, gmtDatetime, merchantconfig_obj) |
50 | 64 | if request_type == Constants::POST_REQUEST_TYPE || request_type == Constants::PUT_REQUEST_TYPE || request_type == Constants::PATCH_REQUEST_TYPE |
51 | 65 | payload = merchantconfig_obj.requestJsonData |
| 66 | + |
52 | 67 | # Note: Digest is not passed for GET calls |
53 | | - digest = DigestGeneration.new.generateDigest(payload, log_obj) |
| 68 | + digest = DigestGeneration.new.generateDigest(payload) |
54 | 69 | jwtBody = "{\n \"digest\":\"" + digest + "\", \"digestAlgorithm\":\"SHA-256\", \"iat\":\"" + gmtDatetime + "\"}" |
55 | 70 | elsif request_type == Constants::GET_REQUEST_TYPE || request_type == Constants::DELETE_REQUEST_TYPE |
56 | 71 | jwtBody = "{\n \"iat\":\"" + gmtDatetime + "\"\n} \n\n" |
57 | | - else |
| 72 | + else |
58 | 73 | raise StandardError.new(Constants::ERROR_PREFIX + Constants::INVALID_REQUEST_TYPE_METHOD) |
59 | 74 | end |
60 | 75 | end |
|
0 commit comments