]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/python/oils/utils/utils.py
b1fd037481f657d5b55007356743f8c567443984
[working/Evergreen.git] / Open-ILS / src / python / oils / utils / utils.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 import re, md5
17 import osrf.ses
18 from osrf.log import *
19
20
21
22 # -----------------------------------------------------------------------
23 # Grab-bag of general utility functions
24 # -----------------------------------------------------------------------
25
26 def md5sum(str):
27     m = md5.new()
28     m.update(str)
29     return m.hexdigest()
30
31 def unique(arr):
32     ''' Unique-ify a list.  only works if list items are hashable '''
33     o = {}
34     for x in arr:
35         o[x] = 1
36     return o.keys()
37
38 def is_db_true(data):
39     ''' Returns true if the data provided matches what the database considers a true value '''
40     if not data or data == 'f' or str(data) == '0':
41         return False
42     return True
43
44
45 def login(username, password, type=None, workstation=None):
46     ''' Login to the server and get back an authtoken'''
47
48     log_info("attempting login with user " + username)
49
50     seed = osrf.ses.ClientSession.atomic_request(
51         'open-ils.auth', 
52         'open-ils.auth.authenticate.init', username)
53
54     # generate the hashed password
55     password = md5sum(seed + md5sum(password))
56
57     return osrf.ses.ClientSession.atomic_request(
58         'open-ils.auth',
59         'open-ils.auth.authenticate.complete',
60         {   'workstation' : workstation,
61             'username' : username,
62             'password' : password,
63             'type' : type
64         }
65     )
66