Skip to content

Commit

Permalink
Add request object retrieval to a common method
Browse files Browse the repository at this point in the history
  • Loading branch information
imesh94 committed Oct 2, 2023
1 parent 3079fa2 commit f3ae7bb
Showing 1 changed file with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -544,26 +544,8 @@ public String getRedirectUri(String requestUri) throws OpenBankingException {
private JSONObject getParRequestObject(JSONObject sessionData) throws OpenBankingException {

//get request ref Ex -> "IVL...." from "urn::IVL..."
String[] requestUri = sessionData.get(REQUEST_URI).toString().split(":");
String requestUriRef = requestUri[requestUri.length - 1];

SessionDataCacheKey cacheKey = new SessionDataCacheKey(requestUriRef);
SessionDataCacheEntry cacheEntry = SessionDataCache.getInstance().getValueFromCache(cacheKey);

if (cacheEntry != null) {
String essentialClaims = cacheEntry.getoAuth2Parameters().getEssentialClaims();
byte[] requestObject;
try {
requestObject = Base64.getDecoder().decode(essentialClaims.split("\\.")[1]);
} catch (IllegalArgumentException e) {
// Decode if the requestObject is base64-url encoded.
requestObject = Base64.getUrlDecoder().decode(essentialClaims.split("\\.")[1]);
}
return new JSONObject(new String(requestObject, StandardCharsets.UTF_8));
} else {
log.error("Could not able to fetch par request object from session data cache.");
throw new OpenBankingException("Could not able to fetch par request object from session data cache.");
}
String requestUri = sessionData.get(REQUEST_URI).toString();
return getParRequestObject(requestUri);
}

/**
Expand All @@ -578,7 +560,20 @@ private JSONObject getParRequestObject(String requestUri) throws OpenBankingExce

String[] requestUriArr = requestUri.split(":");
String requestUriRef = requestUriArr[requestUriArr.length - 1];
SessionDataCacheKey cacheKey = new SessionDataCacheKey(requestUriRef);
return getRequestObjectUsingUriReference(requestUriRef);
}

/**
* Retrieve PAR request object using request_uri reference.
*
* @param requestUriReference - request_uri reference (i.e:last part of request_uri split by :)
* @return Request object json.
* @throws OpenBankingException - OpenBankingException
*/
@Generated(message = "Excluding from code coverage since it requires a valid cache entry")
private JSONObject getRequestObjectUsingUriReference(String requestUriReference) throws OpenBankingException {

SessionDataCacheKey cacheKey = new SessionDataCacheKey(requestUriReference);
SessionDataCacheEntry cacheEntry = SessionDataCache.getInstance().getValueFromCache(cacheKey);

if (cacheEntry != null) {
Expand All @@ -592,8 +587,8 @@ private JSONObject getParRequestObject(String requestUri) throws OpenBankingExce
}
return new JSONObject(new String(requestObject, StandardCharsets.UTF_8));
} else {
log.error("Could not able to fetch par request object from session data cache.");
throw new OpenBankingException("Could not able to fetch par request object from session data cache.");
log.error("Unable to fetch par request object from session data cache.");
throw new OpenBankingException("Unable to fetch par request object from session data cache.");
}
}

Expand Down

0 comments on commit f3ae7bb

Please sign in to comment.