|
| 1 | +package utilities.capturecontext.utility; |
| 2 | + |
| 3 | +import com.cybersource.authsdk.cache.CacheForPublicKeys; |
| 4 | +import com.cybersource.authsdk.core.ConfigException; |
| 5 | +import com.cybersource.authsdk.core.MerchantConfig; |
| 6 | +import com.cybersource.authsdk.util.jwt.JWTUtility; |
| 7 | +import com.cybersource.authsdk.util.jwt.exceptions.InvalidJwkException; |
| 8 | +import com.cybersource.authsdk.util.jwt.exceptions.InvalidJwtException; |
| 9 | +import com.cybersource.authsdk.util.jwt.exceptions.JwtSignatureValidationException; |
| 10 | +import com.google.gson.Gson; |
| 11 | +import com.google.gson.JsonObject; |
| 12 | +import com.google.gson.JsonParser; |
| 13 | +import com.google.gson.reflect.TypeToken; |
| 14 | +import com.nimbusds.jose.JOSEException; |
| 15 | +import com.nimbusds.jose.JWSHeader; |
| 16 | +import com.nimbusds.jose.Payload; |
| 17 | +import com.nimbusds.jwt.SignedJWT; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.reflect.Type; |
| 21 | +import java.net.MalformedURLException; |
| 22 | +import java.security.interfaces.RSAPublicKey; |
| 23 | +import java.text.ParseException; |
| 24 | +import java.util.HashMap; |
| 25 | + |
| 26 | +public class CaptureContextParsingUtility { |
| 27 | + private static CacheForPublicKeys cache = new CacheForPublicKeys(); |
| 28 | + |
| 29 | + public static JsonObject parseCaptureContextResponse( |
| 30 | + String jwtValue, MerchantConfig merchantConfig, boolean verifyJwt) |
| 31 | + throws InvalidJwtException, ConfigException, IOException, InvalidJwkException, JwtSignatureValidationException { |
| 32 | + SignedJWT signedJWT = JWTUtility.parse(jwtValue); |
| 33 | + |
| 34 | + if (verifyJwt) { |
| 35 | + RSAPublicKey publicKey; |
| 36 | + boolean isJwtValid = false; |
| 37 | + |
| 38 | + JWSHeader jwsHeader = signedJWT.getHeader(); |
| 39 | + |
| 40 | + String kid = jwsHeader.getKeyID(); |
| 41 | + |
| 42 | + boolean isPublicKeyFromCache = false; |
| 43 | + |
| 44 | + try { |
| 45 | + publicKey = cache.getPublicKeyFromCache(merchantConfig.getRunEnvironment(), kid); |
| 46 | + isPublicKeyFromCache = true; |
| 47 | + } catch (NullPointerException e) { |
| 48 | + fetchPublicKeyFromApi(kid, merchantConfig.getRunEnvironment()); |
| 49 | + publicKey = cache.getPublicKeyFromCache(merchantConfig.getRunEnvironment(), kid); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + assert publicKey != null; |
| 54 | + isJwtValid = JWTUtility.verifyJwt(signedJWT, publicKey); |
| 55 | + } catch (JwtSignatureValidationException e) { |
| 56 | + if (isPublicKeyFromCache) { |
| 57 | + fetchPublicKeyFromApi(kid, merchantConfig.getRunEnvironment()); |
| 58 | + publicKey = cache.getPublicKeyFromCache(merchantConfig.getRunEnvironment(), kid); |
| 59 | + isJwtValid = JWTUtility.verifyJwt(signedJWT, publicKey); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (!isJwtValid) { |
| 64 | + throw new JwtSignatureValidationException("JWT validation failed"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return convertPayloadMapToJsonObject(signedJWT.getPayload()); |
| 69 | + } |
| 70 | + |
| 71 | + private static JsonObject convertPayloadMapToJsonObject(Payload payload) { |
| 72 | + Gson gson = new Gson(); |
| 73 | + Type typeObject = new TypeToken<HashMap<String, Object>>() {}.getType(); |
| 74 | + String jsonRepresentation = gson.toJson(payload.toJSONObject(), typeObject); |
| 75 | + return JsonParser.parseString(jsonRepresentation).getAsJsonObject(); |
| 76 | + } |
| 77 | + |
| 78 | + private static void fetchPublicKeyFromApi(String kid, String runEnvironment) throws ConfigException, IOException, InvalidJwkException { |
| 79 | + RSAPublicKey publicKey; |
| 80 | + |
| 81 | + try { |
| 82 | + publicKey = PublicKeyApiController.fetchPublicKey(kid, runEnvironment); |
| 83 | + cache.addPublicKeyToCache(runEnvironment, kid, publicKey); |
| 84 | + } catch (MalformedURLException err) { |
| 85 | + throw new ConfigException("Invalid Runtime URL in Merchant Config"); |
| 86 | + } catch (IOException err) { |
| 87 | + throw new IOException("Error while trying to retrieve public key from server"); |
| 88 | + } catch (ParseException err) { |
| 89 | + throw new InvalidJwkException("JWK received from server cannot be parsed correctly", err); |
| 90 | + } catch (JOSEException err) { |
| 91 | + throw new InvalidJwkException("Cannot convert JWK to RSA Public Key", err); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments