]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/python/oils/utils/utils.py
Merge branch 'master' of ssh://yeti.esilibrary.com/home/evergreen/evergreen-equinox...
[working/Evergreen.git] / Open-ILS / src / python / oils / utils / utils.py
1 """
2 Grab-bag of general utility functions
3 """
4
5 # -----------------------------------------------------------------------
6 # Copyright (C) 2007  Georgia Public Library Service
7 # Bill Erickson <billserickson@gmail.com>
8
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 # -----------------------------------------------------------------------
19
20 import hashlib
21 import osrf.log, osrf.ses
22
23 def md5sum(string):
24     """
25     Return an MD5 message digest for a given input string
26     """
27
28     md5 = hashlib.md5()
29     md5.update(string)
30     return md5.hexdigest()
31
32 def unique(arr):
33     """
34     Unique-ify a list.  only works if list items are hashable
35     """
36
37     o = {}
38     for x in arr:
39         o[x] = 1
40     return o.keys()
41
42 def is_db_true(data):
43     """
44     Returns PostgreSQL's definition of "truth" for the supplied data, roughly.
45     """
46
47     if not data or data == 'f' or str(data) == '0':
48         return False
49     return True
50
51 def login(username, password, login_type=None, workstation=None):
52     """
53     Login to the server and get back an authentication token
54
55     @param username: user name
56     @param password: password
57     @param login_type: one of 'opac', 'temp', or 'staff' (default: 'staff')
58     @param workstation: name of the workstation to associate with this login
59
60     @rtype: string
61     @return: a string containing an authentication token to pass as
62         a required parameter of many OpenSRF service calls
63     """
64
65     osrf.log.log_info("attempting login with user " + username)
66
67     seed = osrf.ses.ClientSession.atomic_request(
68         'open-ils.auth', 
69         'open-ils.auth.authenticate.init', username)
70
71     # generate the hashed password
72     password = md5sum(seed + md5sum(password))
73
74     return osrf.ses.ClientSession.atomic_request(
75         'open-ils.auth',
76         'open-ils.auth.authenticate.complete',
77         {   'workstation' : workstation,
78             'username' : username,
79             'password' : password,
80             'type' : login_type
81         }
82     )
83