From 85aedc38930f645a46e87146fa33a73146721b77 Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 25 Oct 2007 21:22:03 +0000 Subject: [PATCH] wrapped up the login logic into a static utility method -- with test code. added generic md5 hex method git-svn-id: svn://svn.open-ils.org/ILS/trunk@7933 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/java/org/open_ils/test/TestLogin.java | 29 +++++++++ Open-ILS/src/java/org/open_ils/util/Utils.java | 75 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Open-ILS/src/java/org/open_ils/test/TestLogin.java create mode 100644 Open-ILS/src/java/org/open_ils/util/Utils.java diff --git a/Open-ILS/src/java/org/open_ils/test/TestLogin.java b/Open-ILS/src/java/org/open_ils/test/TestLogin.java new file mode 100644 index 0000000000..265fa37e02 --- /dev/null +++ b/Open-ILS/src/java/org/open_ils/test/TestLogin.java @@ -0,0 +1,29 @@ +package org.open_ils.test; +import org.open_ils.util.Utils; +import org.open_ils.Event; +import org.opensrf.*; +import java.util.Map; +import java.util.HashMap; + + +public class TestLogin { + public static void main(String args[]) { + try { + + if(args.length < 3) { + System.err.println("usage: java org.open_ils.test.TestLogin "); + return; + } + + Sys.bootstrapClient(args[0], "/config/opensrf"); + Map params = new HashMap(); + params.put("username", args[1]); + params.put("password", args[2]); + Event evt = Utils.login(params); + System.out.println(evt); + } catch(Exception e) { + System.err.println(e); + } + } +} + diff --git a/Open-ILS/src/java/org/open_ils/util/Utils.java b/Open-ILS/src/java/org/open_ils/util/Utils.java new file mode 100644 index 0000000000..24edeef0b4 --- /dev/null +++ b/Open-ILS/src/java/org/open_ils/util/Utils.java @@ -0,0 +1,75 @@ +package org.open_ils.util; +import org.open_ils.*; +import org.opensrf.*; +import org.opensrf.util.*; +import java.util.Map; +import java.util.HashMap; +import java.security.MessageDigest; + +public class Utils { + + /** + * Logs in. + * @param params Login arguments, which may consist of
+ * username
+ * barcode - if username is provided, barcode will be ignored
+ * password
+ * workstation - name of the workstation where the login is occuring
+ * type - type of login, currently "opac", "staff", and "temp"
+ * org - optional org ID to provide login context when no workstation is used. + * @return An Event object. On success, the event 'payload' will contain + * 'authtoken' and 'authtime' fields, which represent the session key and + * session inactivity timeout, respectively. + */ + public static Event login(Map params) throws MethodException { + + Map initMap = new HashMap(); + String init = (params.get("username") != null) ? + params.get("username").toString() : params.get("barcode").toString(); + + Object resp = ClientSession.atomicRequest( + "open-ils.auth", + "open-ils.auth.authenticate.init", new Object [] {init}); + + /** see if the server responded with some type of unexpected event */ + Event evt = Event.parseEvent(resp); + if(evt != null) return evt; + + params.put("password", md5Hex(resp + md5Hex(params.get("password").toString()))); + + resp = ClientSession.atomicRequest( + "open-ils.auth", + "open-ils.auth.authenticate.complete", new Object[]{params}); + + return Event.parseEvent(resp); + } + + + /** + * Generates the hex md5sum of a string. + * @param The string to md5sum + * @return The 32-character hex md5sum + */ + public static String md5Hex(String s) { + StringBuffer sb = new StringBuffer(); + MessageDigest md; + try { + md = MessageDigest.getInstance("MD5"); + } catch(Exception e) { + return null; + } + + md.update(s.getBytes()); + byte[] digest = md.digest(); + for (int i = 0; i < digest.length; i++) { + int b = digest[i] & 0xff; + String hex = Integer.toHexString(b); + if (hex.length() == 1) sb.append("0"); + sb.append(hex); + } + return sb.toString(); + } +} + + + -- 2.11.0