From 7d6ecf8b95fa7ef6c7eba461e3afcfc989b0c892 Mon Sep 17 00:00:00 2001 From: sgayangi Date: Mon, 8 Apr 2024 18:10:15 +0530 Subject: [PATCH] Update API Key implementation --- .../ballerina/APIClient.bal | 14 +++++++++++--- .../config-deployer-service/ballerina/types.bal | 7 +++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 72096f208c..174b57dca5 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -361,9 +361,17 @@ public class APIClient { authTypes.jwt = {header: jwtAuthentication.headerName, sendTokenToUpstream: jwtAuthentication.sendTokenToUpstream, disabled: !jwtAuthentication.enabled, audience: jwtAuthentication.audience}; } else if authentication.authType == "APIKey" && authentication is APIKeyAuthentication { APIKeyAuthentication apiKeyAuthentication = check authentication.cloneWithType(APIKeyAuthentication); - authTypes.apiKey = []; - authTypes.apiKey.push({'in: "Header", name: apiKeyAuthentication.headerName, sendTokenToUpstream: apiKeyAuthentication.sendTokenToUpstream}); - authTypes.apiKey.push({'in: "Query", name: apiKeyAuthentication.queryParamName, sendTokenToUpstream: apiKeyAuthentication.sendTokenToUpstream}); + model:APIKey[] apiKeys = []; + boolean|() headerEnabled = apiKeyAuthentication.headerEnable; + boolean|() queryEnabled = apiKeyAuthentication.queryParamEnable; + + if headerEnabled is boolean && headerEnabled { + apiKeys.push({'in: "Header", name: apiKeyAuthentication.headerName, sendTokenToUpstream: apiKeyAuthentication.sendTokenToUpstream}); + } + if queryEnabled is boolean && queryEnabled { + apiKeys.push({'in: "Query", name: apiKeyAuthentication.queryParamName, sendTokenToUpstream: apiKeyAuthentication.sendTokenToUpstream}); + } + authTypes.apiKey = apiKeys; } else if authentication.authType == "mTLS" { MTLSAuthentication mtlsAuthentication = check authentication.cloneWithType(MTLSAuthentication); isMTLSMandatory = mtlsAuthentication.required == "mandatory"; diff --git a/runtime/config-deployer-service/ballerina/types.bal b/runtime/config-deployer-service/ballerina/types.bal index 66d04272f4..acf5bfc5bf 100644 --- a/runtime/config-deployer-service/ballerina/types.bal +++ b/runtime/config-deployer-service/ballerina/types.bal @@ -1,8 +1,7 @@ +import ballerina/constraint; // AUTO-GENERATED FILE. // This file is auto-generated by the Ballerina OpenAPI tool. - import ballerina/http; -import ballerina/constraint; public type OkAnydata record {| *http:Ok; @@ -275,8 +274,8 @@ public type APIKeyAuthentication record {| boolean sendTokenToUpstream = false; string headerName = "apiKey"; string queryParamName = "apiKey"; - boolean headerEnable = true; - boolean queryParamEnable = true; + boolean headerEnable?; + boolean queryParamEnable?; |}; # Mutual SSL configuration of this API