diff --git a/element-templates/template-connector.json b/element-templates/template-connector.json index 0a5ef8306..09f71c2e4 100644 --- a/element-templates/template-connector.json +++ b/element-templates/template-connector.json @@ -41,13 +41,28 @@ } }, { - "label": "OAuth Token", + "label": "Username", + "description": "The username for authentication.", "group": "authentication", "type": "String", "feel": "optional", "binding": { "type": "zeebe:input", - "name": "token" + "name": "authentication.user" + }, + "constraints": { + "notEmpty": true + } + }, + { + "label": "Token", + "description": "The token for authentication.", + "group": "authentication", + "type": "String", + "feel": "optional", + "binding": { + "type": "zeebe:input", + "name": "authentication.token" }, "constraints": { "notEmpty": true diff --git a/src/main/java/io/camunda/connector/Authentication.java b/src/main/java/io/camunda/connector/Authentication.java new file mode 100644 index 000000000..2b65c5b36 --- /dev/null +++ b/src/main/java/io/camunda/connector/Authentication.java @@ -0,0 +1,61 @@ +package io.camunda.connector; + +import io.camunda.connector.api.ConnectorInput; +import io.camunda.connector.api.SecretStore; +import io.camunda.connector.api.Validator; +import java.util.Objects; + +public class Authentication implements ConnectorInput { + + private String user; + private String token; + + @Override + public void validateWith(final Validator validator) { + validator.require(user, "user"); + validator.require(token, "token"); + if (token != null && !token.startsWith("xobx")) { + validator.addErrorMessage("Token must start with \"xobx\""); + } + } + + @Override + public void replaceSecrets(final SecretStore secretStore) { + token = secretStore.replaceSecret(token); + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + @Override + public int hashCode() { + return Objects.hash(token, user); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + Authentication other = (Authentication) obj; + return Objects.equals(token, other.token) && Objects.equals(user, other.user); + } + + @Override + public String toString() { + return "Authentication [user=" + user + ", token=" + token + "]"; + } +} diff --git a/src/main/java/io/camunda/connector/MyConnectorRequest.java b/src/main/java/io/camunda/connector/MyConnectorRequest.java index c0f0337a3..712b08be0 100644 --- a/src/main/java/io/camunda/connector/MyConnectorRequest.java +++ b/src/main/java/io/camunda/connector/MyConnectorRequest.java @@ -8,20 +8,18 @@ public class MyConnectorRequest implements ConnectorInput { private String message; - private String token; + private Authentication authentication; @Override public void validateWith(final Validator validator) { validator.require(message, "message"); - validator.require(token, "token"); - if (token != null && !token.startsWith("xobx")) { - validator.addErrorMessage("Token must start with \"xobx\""); - } + validator.require(authentication, "authentication"); + validateIfNotNull(authentication, validator); } @Override public void replaceSecrets(final SecretStore secretStore) { - token = secretStore.replaceSecret(token); + replaceSecretsIfNotNull(authentication, secretStore); } public String getMessage() { @@ -32,17 +30,17 @@ public void setMessage(String message) { this.message = message; } - public String getToken() { - return token; + public Authentication getAuthentication() { + return authentication; } - public void setToken(String token) { - this.token = token; + public void setAuthentication(Authentication authentication) { + this.authentication = authentication; } @Override public int hashCode() { - return Objects.hash(message, token); + return Objects.hash(authentication, message); } @Override @@ -51,12 +49,12 @@ public boolean equals(Object obj) { if (obj == null) return false; if (getClass() != obj.getClass()) return false; MyConnectorRequest other = (MyConnectorRequest) obj; - return Objects.equals(message, other.message) && Objects.equals(token, other.token); + return Objects.equals(authentication, other.authentication) + && Objects.equals(message, other.message); } @Override public String toString() { - return "MyConnectorRequest [message=" + message + ", token=" + token + "]"; + return "MyConnectorRequest [message=" + message + ", authentication=" + authentication + "]"; } - } diff --git a/src/test/java/io/camunda/connector/MyFunctionTest.java b/src/test/java/io/camunda/connector/MyFunctionTest.java index 2581bb602..24066cb1e 100644 --- a/src/test/java/io/camunda/connector/MyFunctionTest.java +++ b/src/test/java/io/camunda/connector/MyFunctionTest.java @@ -11,8 +11,11 @@ public class MyFunctionTest { void shouldReturnReceivedMessageWhenExecute() throws Exception { // given var input = new MyConnectorRequest(); + var auth = new Authentication(); input.setMessage("Hello World!"); - input.setToken("xobx-test"); + input.setAuthentication(auth); + auth.setToken("xobx-test"); + auth.setUser("testuser"); var function = new MyConnectorFunction(); var context = ConnectorContextBuilder.create() .variables(input) diff --git a/src/test/java/io/camunda/connector/MyRequestTest.java b/src/test/java/io/camunda/connector/MyRequestTest.java index 3afe25e6d..14ab7ebc5 100644 --- a/src/test/java/io/camunda/connector/MyRequestTest.java +++ b/src/test/java/io/camunda/connector/MyRequestTest.java @@ -2,8 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import io.camunda.connector.api.Validator; import io.camunda.connector.test.ConnectorContextBuilder; import org.junit.jupiter.api.Test; @@ -13,8 +11,11 @@ public class MyRequestTest { void shouldReplaceTokenSecretWhenReplaceSecrets() { // given var input = new MyConnectorRequest(); + var auth = new Authentication(); input.setMessage("Hello World!"); - input.setToken("secrets.MY_TOKEN"); + input.setAuthentication(auth); + auth.setToken("secrets.MY_TOKEN"); + auth.setUser("testuser"); var context = ConnectorContextBuilder.create() .secret("MY_TOKEN", "token value") .build(); @@ -22,19 +23,35 @@ void shouldReplaceTokenSecretWhenReplaceSecrets() { context.replaceSecrets(input); // then assertThat(input) + .extracting("authentication") .extracting("token") .isEqualTo("token value"); } + @Test + void shouldFailWhenValidate_NoAuthentication() { + // given + var input = new MyConnectorRequest(); + input.setMessage("Hello World!"); + var context = ConnectorContextBuilder.create().build(); + // when + assertThatThrownBy(() -> context.validate(input)) + // then + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("authentication"); + } + @Test void shouldFailWhenValidate_NoToken() { // given var input = new MyConnectorRequest(); + var auth = new Authentication(); input.setMessage("Hello World!"); - var validator = new Validator(); - input.validateWith(validator); + input.setAuthentication(auth); + auth.setUser("testuser"); + var context = ConnectorContextBuilder.create().build(); // when - assertThatThrownBy(() -> validator.evaluate()) + assertThatThrownBy(() -> context.validate(input)) // then .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("token"); @@ -44,11 +61,13 @@ void shouldFailWhenValidate_NoToken() { void shouldFailWhenValidate_NoMesage() { // given var input = new MyConnectorRequest(); - input.setToken("xobx-test"); - var validator = new Validator(); - input.validateWith(validator); + var auth = new Authentication(); + input.setAuthentication(auth); + auth.setUser("testuser"); + auth.setToken("xobx-test"); + var context = ConnectorContextBuilder.create().build(); // when - assertThatThrownBy(() -> validator.evaluate()) + assertThatThrownBy(() -> context.validate(input)) // then .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("message"); @@ -58,12 +77,14 @@ void shouldFailWhenValidate_NoMesage() { void shouldFailWhenValidate_TokenWrongPattern() { // given var input = new MyConnectorRequest(); - input.setToken("test"); + var auth = new Authentication(); input.setMessage("foo"); - var validator = new Validator(); - input.validateWith(validator); + input.setAuthentication(auth); + auth.setUser("testuser"); + auth.setToken("test"); + var context = ConnectorContextBuilder.create().build(); // when - assertThatThrownBy(() -> validator.evaluate()) + assertThatThrownBy(() -> context.validate(input)) // then .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Token must start with \"xobx\"");