Skip to content

Commit

Permalink
Adding gateway implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashi1993 committed Sep 30, 2024
1 parent 52338b8 commit 43090a2
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 200 deletions.
4 changes: 2 additions & 2 deletions financial-services-accelerator/accelerators/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<url>http://wso2.org</url>

<modules>
<module>ob-apim</module>
<module>ob-is</module>
<module>fs-apim</module>
<module>fs-is</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@
org.json;version="${org.json.version.range}",
org.wso2.carbon.apimgt.common.gateway.*;version="${org.wso2.carbon.apimgt.version.range}",
org.wso2.carbon.apimgt.impl;version="${org.wso2.carbon.apimgt.version.range}",
org.wso2 .financial.services.accelerator.common.*;version="${project.version}",
*
org.wso2.financial.services.accelerator.common.*;version="${project.version}"
</Import-Package>
<Export-Package>
!org.wso2.financial.services.accelerator.gateway.internal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class GatewayCache extends FinancialServicesBaseCache<GatewayCacheKey, Object> {

private static final String cacheName = "OPEN_BANKING_GATEWAY_CACHE";
private static final String cacheName = "FINANCIAL_SERVICES_GATEWAY_CACHE";

private final Integer accessExpiryMinutes;
private final Integer modifiedExpiryMinutes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Objects;

/**
* Cache Key for Open Banking Gateway cache.
* Cache Key for Financial Services Gateway cache.
*/
public class GatewayCacheKey extends FinancialServicesBaseCacheKey implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public List<FinancialServicesGatewayExecutor> getExecutorsForRequest(FSAPIReques
//add support for consent management portal APIs
requestContext.addContextProperty(GatewayConstants.API_TYPE_CUSTOM_PROP,
GatewayConstants.API_TYPE_CONSENT);
return this.getExecutorMap().get("Consent");
} else if (requestContext.getMsgInfo().getResource().contains("/register")) {
return this.getExecutorMap().get("DCR");
return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_CONSENT);
} else if (requestContext.getMsgInfo().getResource().contains(GatewayConstants.DCR_PATH)) {
return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DCR);
} else {
return this.getExecutorMap().get("Default");
return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DEFAULT);
}
}

Expand All @@ -59,14 +59,14 @@ public List<FinancialServicesGatewayExecutor> getExecutorsForResponse(FSAPIRespo
return EMPTY_LIST;
} else if (GatewayConstants.API_TYPE_CONSENT
.equals(responseContext.getContextProps().get(GatewayConstants.API_TYPE_CUSTOM_PROP))) {
return this.getExecutorMap().get("Consent");
return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_CONSENT);
}
}

if (responseContext.getMsgInfo().getResource().contains("/register")) {
return this.getExecutorMap().get("DCR");
if (responseContext.getMsgInfo().getResource().contains(GatewayConstants.DCR_PATH)) {
return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DCR);
} else {
return this.getExecutorMap().get("Default");
return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DEFAULT);
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.HashMap;
import java.util.Map;

import javax.security.cert.X509Certificate;

/**
* Open Banking executor request context.
Expand All @@ -49,44 +48,25 @@ public class FSAPIRequestContext extends RequestContextDTO {

private static final Log log = LogFactory.getLog(FSAPIRequestContext.class);
private final RequestContextDTO requestContextDTO;
private Map<String, String> contextProps;
private Map<String, Object> contextProps;
private String modifiedPayload;
private String requestPayload;
private Map<String, String> addedHeaders;
private boolean isError;
private ArrayList<FSExecutorError> errors;
private String consentId;
private Map<String, Object> analyticsData;
private OpenAPI openAPI;

public FSAPIRequestContext(RequestContextDTO requestContextDTO,
Map<String, String> contextProps, Map<String, Object> analyticsData) {
public FSAPIRequestContext(RequestContextDTO requestContextDTO, Map<String, Object> contextProps) {

this.requestContextDTO = requestContextDTO;
this.contextProps = contextProps;
this.addedHeaders = new HashMap<>();
this.errors = new ArrayList<>();
this.contextProps = new HashMap<>();
this.analyticsData = analyticsData;

Map<String, String> headers = requestContextDTO.getMsgInfo().getHeaders();
String authHeader = headers.get(GatewayConstants.AUTH_HEADER);
if (authHeader != null && !authHeader.isEmpty() &&
GatewayUtils.isValidJWTToken(authHeader.replace(GatewayConstants.BEARER_TAG, ""))) {
this.consentId = extractConsentID(authHeader);
}
this.consentId = extractConsentID(requestContextDTO);
this.openAPI = retrieveOpenAPI(requestContextDTO);

String apiId = requestContextDTO.getApiRequestInfo().getApiId();
Object cacheObject = GatewayDataHolder.getGatewayCache()
.getFromCache(GatewayCacheKey.of(apiId));
if (cacheObject == null) {
String swaggerDefinition = GatewayUtils.getSwaggerDefinition(apiId);
OpenAPIParser parser = new OpenAPIParser();
this.openAPI = parser.readContents(swaggerDefinition, null, null).getOpenAPI();
GatewayDataHolder.getGatewayCache().addToCache(GatewayCacheKey.of(apiId), this.openAPI);
} else {
this.openAPI = (OpenAPI) cacheObject;
}
if (requestContextDTO.getMsgInfo().getHeaders().get(GatewayConstants.CONTENT_TYPE_TAG) != null) {
String contentType = requestContextDTO.getMsgInfo().getHeaders().get(GatewayConstants.CONTENT_TYPE_TAG);
String httpMethod = requestContextDTO.getMsgInfo().getHttpMethod();
Expand All @@ -99,15 +79,15 @@ public FSAPIRequestContext(RequestContextDTO requestContextDTO,
} catch (Exception e) {
log.error(String.format("Failed to read the text payload from request. %s",
e.getMessage().replaceAll("\n\r", "")));
handleContentTypeErrors(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage);
handleContentTypeErrors(errorMessage);
}
} else if (GatewayUtils.isEligibleRequest(contentType, httpMethod)) {
try {
this.requestPayload = requestContextDTO.getMsgInfo().getPayloadHandler().consumeAsString();
} catch (Exception e) {
log.error(String.format("Failed to read the payload from request. %s",
e.getMessage().replaceAll("\n\r", "")));
handleContentTypeErrors(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage);
handleContentTypeErrors(errorMessage);
}
} else {
this.requestPayload = null;
Expand Down Expand Up @@ -135,16 +115,26 @@ public void setAddedHeaders(Map<String, String> addedHeaders) {
this.addedHeaders = addedHeaders;
}

public Map<String, String> getContextProps() {
public Map<String, Object> getContextProps() {

return contextProps;
}

public void setContextProps(Map<String, String> contextProps) {
public void setContextProps(Map<String, Object> contextProps) {

this.contextProps = contextProps;
}

public void addContextProperty(String key, String value) {

this.contextProps.put(key, value);
}

public Object getContextProperty(String key) {

return this.contextProps.get(key);
}

public boolean isError() {

return isError;
Expand Down Expand Up @@ -186,16 +176,6 @@ public void setOpenAPI(OpenAPI openAPI) {
this.openAPI = openAPI;
}

public Map<String, Object> getAnalyticsData() {

return analyticsData;
}

public void setAnalyticsData(Map<String, Object> analyticsData) {

this.analyticsData = analyticsData;
}

@Override
public MsgInfoDTO getMsgInfo() {

Expand All @@ -208,11 +188,6 @@ public APIRequestInfoDTO getApiRequestInfo() {
return requestContextDTO.getApiRequestInfo();
}

@Override
public X509Certificate[] getClientCerts() {

return requestContextDTO.getClientCerts();
}

@Override
public Certificate[] getClientCertsLatest() {
Expand All @@ -224,43 +199,65 @@ public String getRequestPayload() {
return requestPayload;
}

private String extractConsentID(String jwtToken) {

String consentIdClaim = null;
try {
if (!jwtToken.contains(GatewayConstants.BASIC_TAG)) {
jwtToken = jwtToken.replace(GatewayConstants.BEARER_TAG, "");
JSONObject jwtClaims = GatewayUtils.decodeBase64(GatewayUtils.getPayloadFromJWT(jwtToken));
String consentIdClaimName =
GatewayDataHolder.getInstance().getFinancialServicesConfigurationService().getConfigurations()
.get(FinancialServicesConstants.CONSENT_ID_CLAIM_NAME).toString();
if (!jwtClaims.isNull(consentIdClaimName) &&
!jwtClaims.getString(consentIdClaimName).isEmpty()) {
consentIdClaim = jwtClaims.getString(consentIdClaimName);
/**
* Extract consent ID from the Auth header in the request context.
*
* @param requestContextDTO Request context DTO
* @return consent ID
*/
private String extractConsentID(RequestContextDTO requestContextDTO) {

Map<String, String> headers = requestContextDTO.getMsgInfo().getHeaders();
String authHeader = headers.get(GatewayConstants.AUTH_HEADER);
if (authHeader != null && !authHeader.isEmpty() &&
GatewayUtils.isValidJWTToken(authHeader.replace(GatewayConstants.BEARER_TAG, ""))) {
String consentIdClaim = null;
try {
if (!authHeader.contains(GatewayConstants.BASIC_TAG)) {
authHeader = authHeader.replace(GatewayConstants.BEARER_TAG, "");
JSONObject jwtClaims = GatewayUtils.decodeBase64(GatewayUtils.getPayloadFromJWT(authHeader));
String consentIdClaimName = GatewayDataHolder.getInstance()
.getFinancialServicesConfigurationService().getConfigurations()
.get(FinancialServicesConstants.CONSENT_ID_CLAIM_NAME).toString();
if (!jwtClaims.isNull(consentIdClaimName) &&
!jwtClaims.getString(consentIdClaimName).isEmpty()) {
consentIdClaim = jwtClaims.getString(consentIdClaimName);
}
}
} catch (UnsupportedEncodingException | JSONException | IllegalArgumentException e) {
log.error("Failed to retrieve the consent ID from JWT claims. %s", e);
}
} catch (UnsupportedEncodingException | JSONException | IllegalArgumentException e) {
log.error("Failed to retrieve the consent ID from JWT claims. %s", e);
return consentIdClaim;
}
return consentIdClaim;
}

public void addContextProperty(String key, String value) {

this.contextProps.put(key, value);
return null;
}

public String getContextProperty(String key) {
/**
* Retrieve OpenAPI definition from the cache or from the publisher API.
*
* @param requestContextDTO Request context DTO
* @return OpenAPI definition
*/
private OpenAPI retrieveOpenAPI(RequestContextDTO requestContextDTO) {

return this.contextProps.get(key);
String apiId = requestContextDTO.getApiRequestInfo().getApiId();
Object cacheObject = GatewayDataHolder.getGatewayCache()
.getFromCache(GatewayCacheKey.of(apiId));
if (cacheObject == null) {
String swaggerDefinition = GatewayUtils.getSwaggerDefinition(apiId);
OpenAPIParser parser = new OpenAPIParser();
OpenAPI openAPIDefinition = parser.readContents(swaggerDefinition, null, null).getOpenAPI();
GatewayDataHolder.getGatewayCache().addToCache(GatewayCacheKey.of(apiId), openAPIDefinition);
return openAPIDefinition;
}
return (OpenAPI) cacheObject;
}

private void handleContentTypeErrors(String errorCode, String errorMessage) {
FSExecutorError error = new FSExecutorError(errorCode, errorMessage, errorMessage,
FinancialServicesErrorCodes.UNSUPPORTED_MEDIA_TYPE_CODE);
private void handleContentTypeErrors(String errorMessage) {
FSExecutorError error = new FSExecutorError(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage,
errorMessage, FinancialServicesErrorCodes.UNSUPPORTED_MEDIA_TYPE_CODE);

this.isError = true;
this.errors.add(error);
}

}
Loading

0 comments on commit 43090a2

Please sign in to comment.