]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/java/org/open_ils/util/Utils.java
LP1910409 MARC Batch Edit Allows CSV Column 0
[Evergreen.git] / Open-ILS / src / java / org / open_ils / util / Utils.java
1 package org.open_ils.util;
2 import org.open_ils.*;
3 import org.opensrf.*;
4 import org.opensrf.util.*;
5 import java.util.Map;
6 import java.util.HashMap;
7 import java.security.MessageDigest;
8
9 public class Utils {
10     
11     /**
12      * Logs in.
13      * @param params Login arguments, which may consist of<br/>
14      * username<br/>
15      * barcode - if username is provided, barcode will be ignored<br/>
16      * password<br/>
17      * workstation - name of the workstation where the login is occuring<br/>
18      * type - type of login, currently "opac", "staff", and "temp"<br/>
19      * org - optional org ID to provide login context when no workstation is used.
20      * @return An Event object.  On success, the event 'payload' will contain
21      * 'authtoken' and 'authtime' fields, which represent the session key and 
22      * session inactivity timeout, respectively.
23      */
24     public static Event login(Map params) throws MethodException {
25
26         Map<String, String> initMap = new HashMap<String, String>();
27         String init = (params.get("username") != null) ? 
28             params.get("username").toString() : params.get("barcode").toString();
29
30         Object resp = ClientSession.atomicRequest(
31             "open-ils.auth",
32             "open-ils.auth.authenticate.init", new Object [] {init});
33
34         /** see if the server responded with some type of unexpected event */
35         Event evt = Event.parseEvent(resp);
36         if(evt != null) return evt;
37
38         params.put("password", md5Hex(resp + md5Hex(params.get("password").toString())));
39
40         resp = ClientSession.atomicRequest(
41             "open-ils.auth",
42             "open-ils.auth.authenticate.complete", new Object[]{params});
43
44         return Event.parseEvent(resp);
45     }
46
47
48     /**
49      * Generates the hex md5sum of a string.
50      * @param s The string to md5sum
51      * @return The 32-character hex md5sum
52      */
53     public static String md5Hex(String s) {
54         StringBuffer sb = new StringBuffer();
55         MessageDigest md;
56         try {
57             md = MessageDigest.getInstance("MD5");
58         } catch(Exception e) {
59             return null;
60         }
61
62         md.update(s.getBytes());
63         byte[] digest = md.digest();
64         for (int i = 0; i < digest.length; i++) {
65             int b = digest[i] & 0xff;
66             String hex = Integer.toHexString(b);
67             if (hex.length() == 1) sb.append("0");
68             sb.append(hex);
69         }
70         return sb.toString();
71     }
72 }
73
74
75