From dbefba3fdd519da01497dc35a0508a7833aea025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lange?= Date: Sat, 17 Jul 2021 23:18:12 +0200 Subject: [PATCH] [mielecloud] Less strict e-mail validation (#10928) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [mielecloud] Less strict e-mail validation Signed-off-by: Björn Lange * Some more e-mail validation test cases Signed-off-by: Björn Lange Co-authored-by: Björn Lange --- .../config/servlet/ForwardToLoginServlet.java | 7 +++ .../internal/util/EmailValidator.java | 3 +- .../mielecloud/internal/config/pairing.html | 2 +- .../internal/util/EmailValidatorTest.java | 46 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 bundles/org.openhab.binding.mielecloud/src/test/java/org/openhab/binding/mielecloud/internal/util/EmailValidatorTest.java diff --git a/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/config/servlet/ForwardToLoginServlet.java b/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/config/servlet/ForwardToLoginServlet.java index e817463ad..aa419fb41 100644 --- a/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/config/servlet/ForwardToLoginServlet.java +++ b/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/config/servlet/ForwardToLoginServlet.java @@ -70,18 +70,25 @@ public final class ForwardToLoginServlet extends AbstractRedirectionServlet { logger.warn("Request is missing client ID."); return getErrorRedirectionUrl(PairAccountServlet.MISSING_CLIENT_ID_PARAMETER_NAME); } + clientId = clientId.strip(); + if (clientSecret == null || clientSecret.isEmpty()) { logger.warn("Request is missing client secret."); return getErrorRedirectionUrl(PairAccountServlet.MISSING_CLIENT_SECRET_PARAMETER_NAME); } + clientSecret = clientSecret.strip(); + if (bridgeId == null || bridgeId.isEmpty()) { logger.warn("Request is missing bridge ID."); return getErrorRedirectionUrl(PairAccountServlet.MISSING_BRIDGE_ID_PARAMETER_NAME); } + bridgeId = bridgeId.strip(); + if (email == null || email.isEmpty()) { logger.warn("Request is missing e-mail address."); return getErrorRedirectionUrl(PairAccountServlet.MISSING_EMAIL_PARAMETER_NAME); } + email = email.strip(); ThingUID bridgeUid = null; try { diff --git a/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/util/EmailValidator.java b/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/util/EmailValidator.java index 4e4083f85..5cd7143b1 100644 --- a/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/util/EmailValidator.java +++ b/bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/util/EmailValidator.java @@ -23,7 +23,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault; */ @NonNullByDefault public final class EmailValidator { - private static final Pattern EMAIL_PATTERN = Pattern.compile("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"); + private static final Pattern EMAIL_PATTERN = Pattern + .compile("^[a-zA-Z_0-9\\.+%-]{3,}\\@([a-zA-Z_0-9-]+\\.)+[a-z]+$"); private EmailValidator() { throw new UnsupportedOperationException(); diff --git a/bundles/org.openhab.binding.mielecloud/src/main/resources/org/openhab/binding/mielecloud/internal/config/pairing.html b/bundles/org.openhab.binding.mielecloud/src/main/resources/org/openhab/binding/mielecloud/internal/config/pairing.html index ea1017555..4565122e4 100644 --- a/bundles/org.openhab.binding.mielecloud/src/main/resources/org/openhab/binding/mielecloud/internal/config/pairing.html +++ b/bundles/org.openhab.binding.mielecloud/src/main/resources/org/openhab/binding/mielecloud/internal/config/pairing.html @@ -53,7 +53,7 @@
- +
diff --git a/bundles/org.openhab.binding.mielecloud/src/test/java/org/openhab/binding/mielecloud/internal/util/EmailValidatorTest.java b/bundles/org.openhab.binding.mielecloud/src/test/java/org/openhab/binding/mielecloud/internal/util/EmailValidatorTest.java new file mode 100644 index 000000000..51c037c9e --- /dev/null +++ b/bundles/org.openhab.binding.mielecloud/src/test/java/org/openhab/binding/mielecloud/internal/util/EmailValidatorTest.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.mielecloud.internal.util; + +import static org.junit.jupiter.api.Assertions.*; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * @author Björn Lange - Initial Contribution + */ +@NonNullByDefault +public class EmailValidatorTest { + @ParameterizedTest + @ValueSource(strings = { "example@openhab.org", "itsme@test24.com", "my-account@t-online.de", "Some@dDRESs.edu", + "min@Length.com" }) + void validEmailAddress(String emailAddress) { + // when: + var valid = EmailValidator.isValid(emailAddress); + + // then: + assertTrue(valid); + } + + @ParameterizedTest + @ValueSource(strings = { "examp!e@###.org", "to@o.short.com" }) + void invalidEmailAddress(String emailAddress) { + // when: + var valid = EmailValidator.isValid(emailAddress); + + // then: + assertFalse(valid); + } +}