Changes to introduce peader prefix in API key authentication type (#7742)

This commit is contained in:
Nidhi 2021-09-23 13:20:44 +05:30 committed by GitHub
parent 8f84fc0f69
commit 688b75af22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 3 deletions

View File

@ -71,6 +71,7 @@ export interface Basic {
export interface ApiKey {
authenticationType: AuthType.apiKey;
label: string;
headerPrefix: string;
value: string;
addTo: string;
}

View File

@ -6,6 +6,7 @@ export interface DatasourceAuthentication {
username?: string;
password?: string;
label?: string;
headerPrefix?: string;
value?: string;
addTo?: string;
bearerToken?: string;

View File

@ -207,6 +207,9 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
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<Props> {
};
renderApiKey = () => {
const { authentication } = this.props.formData;
return (
<>
<FormInputContainer>
@ -481,6 +485,7 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
<InputTextControl
{...COMMON_INPUT_PROPS}
configProperty="authentication.value"
encrypted
label="Value"
placeholderText="value"
/>
@ -504,6 +509,16 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
propertyValue=""
/>
</FormInputContainer>
{_.get(authentication, "addTo") == "header" && (
<FormInputContainer>
<InputTextControl
{...COMMON_INPUT_PROPS}
configProperty="authentication.headerPrefix"
label="Header Prefix"
placeholderText="eg: Bearer "
/>
</FormInputContainer>
)}
</>
);
};
@ -514,6 +529,7 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
<InputTextControl
{...COMMON_INPUT_PROPS}
configProperty="authentication.bearerToken"
encrypted
label="Bearer Token"
placeholderText="Bearer Token"
/>

View File

@ -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;

View File

@ -30,6 +30,8 @@ public class ApiKeyAuth extends AuthenticationDTO {
Type addTo;
String label;
String headerPrefix;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Encrypted
String value;

View File

@ -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

View File

@ -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<ApiKeyAuthentication> connectionMono = ApiKeyAuthentication.create(apiKeyAuthDTO);
StepVerifier.create(connectionMono)
.assertNext(connection -> {

View File

@ -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();