]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mielecloud] Less strict e-mail validation (#10928)
authorBjörn Lange <bjoern.lange@udo.edu>
Sat, 17 Jul 2021 21:18:12 +0000 (23:18 +0200)
committerGitHub <noreply@github.com>
Sat, 17 Jul 2021 21:18:12 +0000 (23:18 +0200)
* [mielecloud] Less strict e-mail validation

Signed-off-by: Björn Lange <bjoern.lange@tu-dortmund.de>
* Some more e-mail validation test cases

Signed-off-by: Björn Lange <bjoern.lange@tu-dortmund.de>
Co-authored-by: Björn Lange <bjoern.lange@tu-dortmund.de>
bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/config/servlet/ForwardToLoginServlet.java
bundles/org.openhab.binding.mielecloud/src/main/java/org/openhab/binding/mielecloud/internal/util/EmailValidator.java
bundles/org.openhab.binding.mielecloud/src/main/resources/org/openhab/binding/mielecloud/internal/config/pairing.html
bundles/org.openhab.binding.mielecloud/src/test/java/org/openhab/binding/mielecloud/internal/util/EmailValidatorTest.java [new file with mode: 0644]

index e817463adf87db2ad3786773d91f3571d4de46aa..aa419fb41e963cd241286897801d0cd105dce078 100644 (file)
@@ -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 {
index 4e4083f85968546c2d70c2133317817b1a626afd..5cd7143b1f27cc2c17452cafc3292c26a64b7985 100644 (file)
@@ -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();
index ea101755544d3db5d8873d263b0ec783ab4d8d11..4565122e4ab856fa32c18ec6bf9eaadc5dfcda37 100644 (file)
@@ -53,7 +53,7 @@
                         </div>
                         <div class="form-group">
                             <label for="email">E-mail address:</label>
-                            <input type="text" class="form-control" id="email" name="email" placeholder="Enter the e-mail address associated with your Miele Cloud Account" required pattern="[a-z0-9._%+-]{3,}@[a-z]{3,}([.]{1}[a-z]{2,}|[.]{1}[a-z]{2,}[.]{1}[a-z]{2,})" />
+                            <input type="text" class="form-control" id="email" name="email" placeholder="Enter the e-mail address associated with your Miele Cloud Account" required pattern="[a-zA-Z_0-9.+%-]{3,}@([a-zA-Z_0-9-]+[.])+[a-z]+)" />
                         </div>
                         <button type="submit" class="btn btn-danger btn-lg">Pair Account</button>
                     </form>
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 (file)
index 0000000..51c037c
--- /dev/null
@@ -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);
+    }
+}