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 { export interface ApiKey {
authenticationType: AuthType.apiKey; authenticationType: AuthType.apiKey;
label: string; label: string;
headerPrefix: string;
value: string; value: string;
addTo: string; addTo: string;
} }

View File

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

View File

@ -207,6 +207,9 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
if (!authentication || !_.get(authentication, "addTo")) { if (!authentication || !_.get(authentication, "addTo")) {
this.props.change("authentication.addTo", ApiKeyAuthType.Header); this.props.change("authentication.addTo", ApiKeyAuthType.Header);
} }
if (!authentication || !_.get(authentication, "headerPrefix")) {
this.props.change("authentication.headerPefix", "ApiKeyAuthType.Header");
}
}; };
ensureOAuthDefaultsAreCorrect = () => { ensureOAuthDefaultsAreCorrect = () => {
@ -467,6 +470,7 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
}; };
renderApiKey = () => { renderApiKey = () => {
const { authentication } = this.props.formData;
return ( return (
<> <>
<FormInputContainer> <FormInputContainer>
@ -481,6 +485,7 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
<InputTextControl <InputTextControl
{...COMMON_INPUT_PROPS} {...COMMON_INPUT_PROPS}
configProperty="authentication.value" configProperty="authentication.value"
encrypted
label="Value" label="Value"
placeholderText="value" placeholderText="value"
/> />
@ -504,6 +509,16 @@ class DatasourceRestAPIEditor extends React.Component<Props> {
propertyValue="" propertyValue=""
/> />
</FormInputContainer> </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 <InputTextControl
{...COMMON_INPUT_PROPS} {...COMMON_INPUT_PROPS}
configProperty="authentication.bearerToken" configProperty="authentication.bearerToken"
encrypted
label="Bearer Token" label="Bearer Token"
placeholderText="Bearer Token" placeholderText="Bearer Token"
/> />

View File

@ -124,6 +124,7 @@ const formToDatasourceAuthentication = (
authenticationType: AuthType.apiKey, authenticationType: AuthType.apiKey,
label: authentication.label, label: authentication.label,
value: authentication.value, value: authentication.value,
headerPrefix: authentication.headerPrefix,
addTo: authentication.addTo, addTo: authentication.addTo,
}; };
return apiKey; return apiKey;
@ -204,6 +205,7 @@ const datasourceToFormAuthentication = (
authenticationType: AuthType.apiKey, authenticationType: AuthType.apiKey,
label: authentication.label || "", label: authentication.label || "",
value: authentication.value || "", value: authentication.value || "",
headerPrefix: authentication.headerPrefix || "",
addTo: authentication.addTo || "", addTo: authentication.addTo || "",
}; };
return apiKey; return apiKey;

View File

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

View File

@ -25,6 +25,7 @@ import java.net.URI;
@AllArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiKeyAuthentication extends APIConnection { public class ApiKeyAuthentication extends APIConnection {
private String label; private String label;
private String headerPrefix;
private String value; private String value;
Type addTo; Type addTo;
@ -32,6 +33,7 @@ public class ApiKeyAuthentication extends APIConnection {
return Mono.just( return Mono.just(
ApiKeyAuthentication.builder() ApiKeyAuthentication.builder()
.label(apiKeyAuth.getLabel()) .label(apiKeyAuth.getLabel())
.headerPrefix(apiKeyAuth.getHeaderPrefix())
.value(apiKeyAuth.getValue()) .value(apiKeyAuth.getValue())
.addTo(apiKeyAuth.getAddTo()) .addTo(apiKeyAuth.getAddTo())
.build() .build()
@ -46,7 +48,7 @@ public class ApiKeyAuthentication extends APIConnection {
requestBuilder.url(appendApiKeyParamToUrl(request.url())); requestBuilder.url(appendApiKeyParamToUrl(request.url()));
break; break;
case HEADER: case HEADER:
requestBuilder.headers(header -> header.set(label, value)); requestBuilder.headers(header -> header.set(label, this.getHeaderValue()));
break; break;
default: default:
return Mono.error( return Mono.error(
@ -65,6 +67,16 @@ public class ApiKeyAuthentication extends APIConnection {
.switchIfEmpty(next.exchange(request)); .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) { private URI appendApiKeyParamToUrl(URI oldUrl) {
return UriComponentsBuilder return UriComponentsBuilder

View File

@ -15,7 +15,7 @@ public class ApiKeyAuthenticationTest {
String label = "label"; String label = "label";
String value = "value"; String value = "value";
ApiKeyAuth.Type type = ApiKeyAuth.Type.QUERY_PARAMS; 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); Mono<ApiKeyAuthentication> connectionMono = ApiKeyAuthentication.create(apiKeyAuthDTO);
StepVerifier.create(connectionMono) StepVerifier.create(connectionMono)
.assertNext(connection -> { .assertNext(connection -> {

View File

@ -495,7 +495,7 @@ public class RestApiPluginTest {
public void testRequestWithApiKeyHeader() { public void testRequestWithApiKeyHeader() {
DatasourceConfiguration dsConfig = new DatasourceConfiguration(); DatasourceConfiguration dsConfig = new DatasourceConfiguration();
dsConfig.setUrl("https://postman-echo.com/post"); 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); dsConfig.setAuthentication(authenticationDTO);
ActionConfiguration actionConfig = new ActionConfiguration(); ActionConfiguration actionConfig = new ActionConfiguration();