Skip to content

Commit

Permalink
CLDR-17248 Enable creation of ST accounts for webdriver simulated use…
Browse files Browse the repository at this point in the history
…rs (#3424)

-Simulated users have pseudo email addresses like [email protected]

-New class AuthSurveyDriver

-In Auth, if login fails, call AuthSurveyDriver to enable creation of account if appropriate

-Only create simulated users if SurveyMain.isUnofficial, never on production server

-Only create simulated users if CLDR_WEBDRIVER_PASSWORD is defined in cldr.properties
  • Loading branch information
btangmu authored Dec 25, 2023
1 parent 4154cf2 commit fcb968b
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.unicode.cldr.web;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.unicode.cldr.util.CLDRConfig;
import org.unicode.cldr.util.Organization;
import org.unicode.cldr.util.StandardCodes;
import org.unicode.cldr.web.UserRegistry.User;

public class AuthSurveyDriver {

/**
* Fictitious domain identifying fictitious email addresses for simulated users like
* "[email protected]"
*/
public static final String EMAIL_AT_DOMAIN = "@cldr-apps-webdriver.org";

private static final String EMAIL_PREFIX = "driver-";

private static final int INVALID_USER_INDEX = -1;
private static final int FIRST_VALID_USER_INDEX = 0;

private static boolean isInitialized = false;
private static boolean isEnabled = false;
private static String realPassword = null;

/**
* If webdriver users are enabled, and the given password and email are correct for such users,
* create a new user.
*
* @param password the password
* @param email the (pseudo) email address
* @return the new user, or null
*/
public static User createTestUser(String password, String email) {
if (!isInitialized) {
initialize();
}
if (isEnabled && realPassword.equals(password) && email.contains(EMAIL_AT_DOMAIN)) {
int userIndex = getUserIndexFromEmail(email);
if (userIndex >= FIRST_VALID_USER_INDEX) {
return addTestUser(password, email, userIndex);
}
}
return null;
}

private static void initialize() {
if (SurveyMain.isUnofficial()) {
realPassword = CLDRConfig.getInstance().getProperty("CLDR_WEBDRIVER_PASSWORD", "");
if (realPassword != null && !realPassword.isBlank()) {
isEnabled = true;
}
}
isInitialized = true;
}

/**
* Get a number like 123 given an email address like "[email protected]"
*
* @param email the address
* @return the nonnegative number if the address matches the pattern, else INVALID_USER_INDEX
*/
private static int getUserIndexFromEmail(String email) {
Matcher m = Pattern.compile("\\d+").matcher(email);
if (m.find()) {
int userIndex = Integer.parseInt(m.group());
String properEmail = EMAIL_PREFIX + userIndex + EMAIL_AT_DOMAIN;
if (properEmail.equals(email)) {
return userIndex;
}
}
return INVALID_USER_INDEX;
}

private static User addTestUser(String password, String email, int userIndex) {
UserRegistry reg = CookieSession.sm.reg;
User u = reg.new User(UserRegistry.NO_USER);
u.email = email;
// Make user level TC, since even with ALL_LOCALES (*), a VETTER might
// not have permission to vote in all locales, depending on the organization.
u.userlevel = UserRegistry.TC;
u.locales = StandardCodes.ALL_LOCALES;
u.name = EMAIL_PREFIX + userIndex;
Organization[] orgArray = Organization.values();
u.org = orgArray[userIndex % orgArray.length].name();
u.setPassword(password);
User registeredUser = reg.newUser(null, u);
if (registeredUser == null || registeredUser.id <= 0) {
return null;
}
return registeredUser;
}
}
14 changes: 11 additions & 3 deletions tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.unicode.cldr.web.AuthSurveyDriver;
import org.unicode.cldr.web.CookieSession;
import org.unicode.cldr.web.SurveyLog;
import org.unicode.cldr.web.SurveyMain;
Expand Down Expand Up @@ -82,10 +83,17 @@ public Response login(
String userIP = WebContext.userIP(hreq);
CookieSession session = null;
if (!request.isEmpty()) {
UserRegistry.User user =
CookieSession.sm.reg.get(request.password, request.email, userIP);
UserRegistry.User user;
try {
user = CookieSession.sm.reg.get(request.password, request.email, userIP);
} catch (LogoutException e) {
user = null;
}
if (user == null) {
user = AuthSurveyDriver.createTestUser(request.password, request.email);
}
if (user == null) {
return Response.status(403, "Login failed").build();
throw new LogoutException();
}
session = CookieSession.retrieveUser(user);
if (session == null) {
Expand Down

0 comments on commit fcb968b

Please sign in to comment.