[open-ils-commits] r7933 - in trunk/Open-ILS/src/java/org/open_ils:
. test util
svn at svn.open-ils.org
svn at svn.open-ils.org
Thu Oct 25 17:35:58 EDT 2007
Author: erickson
Date: 2007-10-25 17:22:03 -0400 (Thu, 25 Oct 2007)
New Revision: 7933
Added:
trunk/Open-ILS/src/java/org/open_ils/test/TestLogin.java
trunk/Open-ILS/src/java/org/open_ils/util/
trunk/Open-ILS/src/java/org/open_ils/util/Utils.java
Log:
wrapped up the login logic into a static utility method -- with test code. added generic md5 hex method
Added: trunk/Open-ILS/src/java/org/open_ils/test/TestLogin.java
===================================================================
--- trunk/Open-ILS/src/java/org/open_ils/test/TestLogin.java (rev 0)
+++ trunk/Open-ILS/src/java/org/open_ils/test/TestLogin.java 2007-10-25 21:22:03 UTC (rev 7933)
@@ -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 <opensrf_config> <username> <password>");
+ return;
+ }
+
+ Sys.bootstrapClient(args[0], "/config/opensrf");
+ Map<String,String> params = new HashMap<String,String>();
+ 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);
+ }
+ }
+}
+
Added: trunk/Open-ILS/src/java/org/open_ils/util/Utils.java
===================================================================
--- trunk/Open-ILS/src/java/org/open_ils/util/Utils.java (rev 0)
+++ trunk/Open-ILS/src/java/org/open_ils/util/Utils.java 2007-10-25 21:22:03 UTC (rev 7933)
@@ -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<br/>
+ * username<br/>
+ * barcode - if username is provided, barcode will be ignored<br/>
+ * password<br/>
+ * workstation - name of the workstation where the login is occuring<br/>
+ * type - type of login, currently "opac", "staff", and "temp"<br/>
+ * 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<String, String> initMap = new HashMap<String, String>();
+ 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();
+ }
+}
+
+
+
More information about the open-ils-commits
mailing list