package com.widgetworkbench.cheerpj; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Arrays; /** * Optional one-shot bridge from an authenticated HTTPS HMI browser session to * the *independent* iTAC Workplace PersonService login. Java 11. * * No vendor classes are patched, and IMSAPI session tokens are NOT reused. * The browser offers credentials before main() starts; one Java-owned daemon * waits for PersonService to become available and invokes the public * MesContext.doLogin(String, Password, String) method exactly once. * * 0 = NONE; 1 = SUCCESS; 2 = REJECTED; 3 = ALREADY_SIGNED_IN; * 4 = EXPIRED; 5 = BRIDGE_ERROR. No passwords, usernames, or stack traces * are printed. Logging must never contain credential-bearing exceptions. */ public final class WorkplaceCredentialBridge { private static final long TTL_MILLIS = 120_000L; private static final int RETRY_DELAY = 400; private static char[] pending; private static String username; private static String client; private static long expires; private static boolean armed; private static volatile int result; private WorkplaceCredentialBridge() { } // Direct CheerpJ native handler, registered with cheerpjInit({natives}). // No System.loadLibrary() is called by this helper. private static native void nativeReport(int code); private static void report(int code) { result = code; System.out.println("[WidgetWorkbench/WorkplaceAuth] RESULT_CODE=" + code); try { nativeReport(code); } catch (Throwable ignored) { /* stdout still reports */ } } /** Called ONCE via CheerpJ Library Mode, before cheerpjRunMain(). */ public static synchronized int offer(String user, String password, String targetClient) { if (armed || pending != null || !valid(user, 120) || !valid(password, 1024) || !valid(targetClient, 120)) return 0; username = user; pending = password.toCharArray(); client = targetClient; expires = System.currentTimeMillis() + TTL_MILLIS; armed = true; Thread worker = new Thread(WorkplaceCredentialBridge::runOnce, "hmi-workplace-auth-handoff"); worker.setDaemon(true); worker.start(); return 1; } public static synchronized int getResultCode() { return result; } private static boolean valid(String input, int max) { return input != null && !input.isEmpty() && input.length() <= max && input.indexOf('\0') < 0; } private static void clearPending() { if (pending != null) Arrays.fill(pending, '\0'); pending = null; username = null; client = null; expires = 0L; } private static void runOnce() { // Wait until the Workplace has begun its own Java initialization. The // service locator may be unavailable during the first few seconds. try { Thread.sleep(1200); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); synchronized (WorkplaceCredentialBridge.class) { clearPending(); } report(5);return; } try { final ClassLoader cl = WorkplaceCredentialBridge.class.getClassLoader(); final Class locator = Class.forName("com.itac.artes.ihas.ServiceLocator", true, cl); final Class personService = Class.forName("com.itac.mes.admin.domain.PersonService", false, cl); final Method getService = locator.getMethod("getService", Class.class); // Do not construct/use MesContext before the MES services are ready. Object service = null; while (System.currentTimeMillis() < expires) { try { service = getService.invoke(null, personService); } catch (Exception ignored) { } if (service != null) break; Thread.sleep(RETRY_DELAY); } if (service == null) { report(4);return; } final Class contextType = Class.forName("com.itac.mes.gui.MesContext", true, cl); final Object context = contextType.getMethod("instance").invoke(null); // Avoid replacing an active login (including a different MES user). if (contextType.getMethod("getUser").invoke(context) != null) { report(3);return; } final Class passwordType = Class.forName("com.itac.util.text.Password", true, cl); @SuppressWarnings({"unchecked", "rawtypes"}) final Class encryptionType = (Class) Class.forName("com.itac.util.text.Password$Encryption", true, cl); @SuppressWarnings({"unchecked", "rawtypes"}) final Object none = Enum.valueOf((Class) encryptionType, "NONE"); final Constructor passwordConstructor = passwordType.getConstructor(encryptionType, String.class); String user; String target; char[] chars; synchronized (WorkplaceCredentialBridge.class) { if (pending == null || System.currentTimeMillis() >= expires) { report(4);return; } user = username; target = client; chars = pending; pending = null; username = null; client = null; } try { // Same Password constructor used by iTAC GuiPasswordField.getValue(). final Object pwd = passwordConstructor.newInstance(none, new String(chars)); // Exactly one real MES authentication attempt; boolean false is // a rejected login. No exception contents leave the JVM. final Object success = contextType.getMethod("doLogin", String.class, passwordType, String.class) .invoke(context, user, pwd, target); report(Boolean.TRUE.equals(success) ? 1 : 2); } finally { Arrays.fill(chars, '\0'); } } catch (Throwable error) { // Do not log Throwable.toString(): vendor exceptions may contain // credentials or personally identifying information. report(5); } finally { synchronized (WorkplaceCredentialBridge.class) { clearPending(); } } } }