diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyAuthenticator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyAuthenticator.java index 3105f47de3..6e68ca5deb 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyAuthenticator.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/APIKeyAuthenticator.java @@ -122,17 +122,18 @@ private static String getAPIKeyFromRequest(RequestContext requestContext) { } } - // If an API Key is not found, check for the API Key in the WebSocket protocol header + // If an API Key is not found, check for the API Key in the WebSocket protocol + // header if (requestContext.getMatchedAPI().getApiType().equalsIgnoreCase(APIConstants.ApiType.WEB_SOCKET) && requestContext.getHeaders().containsKey(HttpConstants.WEBSOCKET_PROTOCOL_HEADER)) { - String apiKey = extractAPIKeyInWSProtocolHeader(requestContext); - if (apiKey != null && !apiKey.isEmpty()) { - String protocols = getProtocolsToSetInRequestHeaders(requestContext); - if (protocols != null) { - requestContext.addOrModifyHeaders(HttpConstants.WEBSOCKET_PROTOCOL_HEADER, protocols); - } - return apiKey.trim(); + String apiKey = extractAPIKeyInWSProtocolHeader(requestContext); + if (apiKey != null && !apiKey.isEmpty()) { + String protocols = getProtocolsToSetInRequestHeaders(requestContext); + if (protocols != null) { + requestContext.addOrModifyHeaders(HttpConstants.WEBSOCKET_PROTOCOL_HEADER, protocols); } + return apiKey.trim(); + } } return ""; @@ -484,7 +485,8 @@ public static String extractAPIKeyInWSProtocolHeader(RequestContext requestConte String[] secProtocolHeaderValues = protocolHeader.split(","); if (secProtocolHeaderValues.length > 1 && secProtocolHeaderValues[0].equals( Constants.WS_API_KEY_IDENTIFIER)) { - AuthenticatorUtils.addWSProtocolResponseHeaderIfRequired(requestContext, Constants.WS_API_KEY_IDENTIFIER); + AuthenticatorUtils.addWSProtocolResponseHeaderIfRequired(requestContext, + Constants.WS_API_KEY_IDENTIFIER); return secProtocolHeaderValues[1].trim(); } } @@ -493,7 +495,7 @@ public static String extractAPIKeyInWSProtocolHeader(RequestContext requestConte public static String getProtocolsToSetInRequestHeaders(RequestContext requestContext) { String[] secProtocolHeaderValues = requestContext.getHeaders().get( - HttpConstants.WEBSOCKET_PROTOCOL_HEADER).split(","); + HttpConstants.WEBSOCKET_PROTOCOL_HEADER).split(","); if (secProtocolHeaderValues.length > 2) { return Arrays.stream(secProtocolHeaderValues, 2, secProtocolHeaderValues.length) .collect(Collectors.joining(",")).trim();