Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix email validation #939

Merged
merged 3 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.regex.Pattern;

/**
* Created by SMAKINEN on 1.9.2016.
*/
public class RegistrationUtil {

// From: https://owasp.org/www-community/OWASP_Validation_Regex_Repository
private static final String EMAIL_REGEXP = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEXP);
public static final String getServerAddress(ActionParameters params) {
final String domain = PropertyUtil.get("oskari.domain", null);
if(domain != null) {
Expand All @@ -23,18 +27,17 @@ public static final String getServerAddress(ActionParameters params) {
}

public static boolean isValidEmail(String email) {
String regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.\\-)+[\\w]+[\\w]$";
return email != null && !email.isEmpty() && email.matches(regex);
return email != null && !email.isEmpty() && EMAIL_PATTERN.matcher(email).matches();
}

public static boolean isPasswordOk(String passwd) {
if(passwd == null) {
if (passwd == null) {
return false;
}
if(passwd.length() < PasswordRules.getMinLength()) {
if (passwd.length() < PasswordRules.getMinLength()) {
return false;
}
if(PasswordRules.getRequireCase() &&
if (PasswordRules.getRequireCase() &&
(passwd.toLowerCase().equals(passwd) || passwd.toUpperCase().equals(passwd))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fi.nls.oskari.control.users;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class RegistrationUtilTest {
@Test
public void isValidEmail() {
List<String> goodEmails = Arrays.asList(
"username@domain.com",
"user.name@domain.com",
"user-name@domain.com",
"user-name@domain-test.com",
"username@domain.co.in",
"user_name@domain.com");
goodEmails.forEach(email -> assertTrue(email, RegistrationUtil.isValidEmail(email)));
}
@Test
public void isNotValidEmail() {
List<String> badEmails = Arrays.asList(
"username.@domain.com",
".user.name@domain.com",
"user-name@domain.com.",
"username@.com");
badEmails.forEach(email -> assertFalse(email, RegistrationUtil.isValidEmail(email)));
}
}