diff --git a/app/client/src/entities/Datasource/RestAPIForm.ts b/app/client/src/entities/Datasource/RestAPIForm.ts index c12e1ae2d9..108701f769 100644 --- a/app/client/src/entities/Datasource/RestAPIForm.ts +++ b/app/client/src/entities/Datasource/RestAPIForm.ts @@ -71,6 +71,7 @@ export interface Basic { export interface ApiKey { authenticationType: AuthType.apiKey; label: string; + headerPrefix: string; value: string; addTo: string; } diff --git a/app/client/src/entities/Datasource/index.ts b/app/client/src/entities/Datasource/index.ts index 2c444455c7..0bddb63619 100644 --- a/app/client/src/entities/Datasource/index.ts +++ b/app/client/src/entities/Datasource/index.ts @@ -6,6 +6,7 @@ export interface DatasourceAuthentication { username?: string; password?: string; label?: string; + headerPrefix?: string; value?: string; addTo?: string; bearerToken?: string; diff --git a/app/client/src/pages/Editor/DataSourceEditor/RestAPIDatasourceForm.tsx b/app/client/src/pages/Editor/DataSourceEditor/RestAPIDatasourceForm.tsx index c31c4ed70e..2714ab0eaf 100644 --- a/app/client/src/pages/Editor/DataSourceEditor/RestAPIDatasourceForm.tsx +++ b/app/client/src/pages/Editor/DataSourceEditor/RestAPIDatasourceForm.tsx @@ -207,6 +207,9 @@ class DatasourceRestAPIEditor extends React.Component { if (!authentication || !_.get(authentication, "addTo")) { this.props.change("authentication.addTo", ApiKeyAuthType.Header); } + if (!authentication || !_.get(authentication, "headerPrefix")) { + this.props.change("authentication.headerPefix", "ApiKeyAuthType.Header"); + } }; ensureOAuthDefaultsAreCorrect = () => { @@ -467,6 +470,7 @@ class DatasourceRestAPIEditor extends React.Component { }; renderApiKey = () => { + const { authentication } = this.props.formData; return ( <> @@ -481,6 +485,7 @@ class DatasourceRestAPIEditor extends React.Component { @@ -504,6 +509,16 @@ class DatasourceRestAPIEditor extends React.Component { propertyValue="" /> + {_.get(authentication, "addTo") == "header" && ( + + + + )} ); }; @@ -514,6 +529,7 @@ class DatasourceRestAPIEditor extends React.Component { diff --git a/app/client/src/transformers/RestAPIDatasourceFormTransformer.ts b/app/client/src/transformers/RestAPIDatasourceFormTransformer.ts index 8a10dac832..5e5765f3a5 100644 --- a/app/client/src/transformers/RestAPIDatasourceFormTransformer.ts +++ b/app/client/src/transformers/RestAPIDatasourceFormTransformer.ts @@ -124,6 +124,7 @@ const formToDatasourceAuthentication = ( authenticationType: AuthType.apiKey, label: authentication.label, value: authentication.value, + headerPrefix: authentication.headerPrefix, addTo: authentication.addTo, }; return apiKey; @@ -204,6 +205,7 @@ const datasourceToFormAuthentication = ( authenticationType: AuthType.apiKey, label: authentication.label || "", value: authentication.value || "", + headerPrefix: authentication.headerPrefix || "", addTo: authentication.addTo || "", }; return apiKey; diff --git a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/models/ApiKeyAuth.java b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/models/ApiKeyAuth.java index 0d01f05077..69a78bd7bb 100644 --- a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/models/ApiKeyAuth.java +++ b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/models/ApiKeyAuth.java @@ -30,6 +30,8 @@ public class ApiKeyAuth extends AuthenticationDTO { Type addTo; String label; + String headerPrefix; + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) @Encrypted String value; diff --git a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/connections/ApiKeyAuthentication.java b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/connections/ApiKeyAuthentication.java index 86c9755515..359d63d424 100644 --- a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/connections/ApiKeyAuthentication.java +++ b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/connections/ApiKeyAuthentication.java @@ -25,6 +25,7 @@ import java.net.URI; @AllArgsConstructor(access = AccessLevel.PRIVATE) public class ApiKeyAuthentication extends APIConnection { private String label; + private String headerPrefix; private String value; Type addTo; @@ -32,6 +33,7 @@ public class ApiKeyAuthentication extends APIConnection { return Mono.just( ApiKeyAuthentication.builder() .label(apiKeyAuth.getLabel()) + .headerPrefix(apiKeyAuth.getHeaderPrefix()) .value(apiKeyAuth.getValue()) .addTo(apiKeyAuth.getAddTo()) .build() @@ -46,7 +48,7 @@ public class ApiKeyAuthentication extends APIConnection { requestBuilder.url(appendApiKeyParamToUrl(request.url())); break; case HEADER: - requestBuilder.headers(header -> header.set(label, value)); + requestBuilder.headers(header -> header.set(label, this.getHeaderValue())); break; default: return Mono.error( @@ -65,6 +67,16 @@ public class ApiKeyAuthentication extends APIConnection { .switchIfEmpty(next.exchange(request)); } + private String getHeaderValue() { + String headerValue = ""; + if (this.headerPrefix != null) { + headerValue = this.headerPrefix.trim() + " "; + } + headerValue += this.value; + + return headerValue.trim(); + } + private URI appendApiKeyParamToUrl(URI oldUrl) { return UriComponentsBuilder diff --git a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/connections/ApiKeyAuthenticationTest.java b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/connections/ApiKeyAuthenticationTest.java index 19cbf34628..edfa39c509 100644 --- a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/connections/ApiKeyAuthenticationTest.java +++ b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/connections/ApiKeyAuthenticationTest.java @@ -15,7 +15,7 @@ public class ApiKeyAuthenticationTest { String label = "label"; String value = "value"; ApiKeyAuth.Type type = ApiKeyAuth.Type.QUERY_PARAMS; - ApiKeyAuth apiKeyAuthDTO = new ApiKeyAuth(type, label, value); + ApiKeyAuth apiKeyAuthDTO = new ApiKeyAuth(type, label, null, value); Mono connectionMono = ApiKeyAuthentication.create(apiKeyAuthDTO); StepVerifier.create(connectionMono) .assertNext(connection -> { diff --git a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java index e39eeaa174..6bd7b735d2 100644 --- a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java +++ b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java @@ -495,7 +495,7 @@ public class RestApiPluginTest { public void testRequestWithApiKeyHeader() { DatasourceConfiguration dsConfig = new DatasourceConfiguration(); dsConfig.setUrl("https://postman-echo.com/post"); - AuthenticationDTO authenticationDTO = new ApiKeyAuth(ApiKeyAuth.Type.HEADER, "api_key", "test"); + AuthenticationDTO authenticationDTO = new ApiKeyAuth(ApiKeyAuth.Type.HEADER, "api_key", "Token", "test"); dsConfig.setAuthentication(authenticationDTO); ActionConfiguration actionConfig = new ActionConfiguration();