]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Auth.pm
fixed some logic errors, change recived from user search
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Auth.pm
1 use strict; use warnings;
2 package OpenILS::Application::Auth;
3 use OpenSRF::Application;
4 use base qw/OpenSRF::Application/;
5 use OpenSRF::Utils::Cache;
6 use Digest::MD5 qw(md5_hex);
7 use OpenSRF::Utils::Logger qw(:level);
8 use OpenILS::Utils::Fieldmapper;
9 use OpenSRF::EX qw(:try);
10
11 # memcache handle
12 my $cache_handle;
13
14
15 # -------------------------------------------------------------
16 # Methods
17 # -------------------------------------------------------------
18 # -------------------------------------------------------------
19
20 __PACKAGE__->register_method(
21         method  => "init_authenticate",
22         api_name        => "open-ils.auth.authenticate.init",
23         argc            => 1, #(username) 
24         note            =>      <<TEXT,
25 Generates a random seed and returns it.  The client
26 must then perform md5_hex( \$seed . \$password ) and use that
27 as the passwordhash to open-ils.auth.authenticate.complete
28 TEXT
29 );
30
31 __PACKAGE__->register_method(
32         method  => "complete_authenticate",
33         api_name        => "open-ils.auth.authenticate.complete",
34         argc            => 2, #( barcode, passwdhash )
35         note            => <<TEXT,
36 Client provides the username and passwordhash (see 
37 open-ils.auth.authenticate.init).  If their password hash is 
38 correct for the given username, a session id is returned, 
39 if not, "0" is returned
40 TEXT
41 );
42
43 __PACKAGE__->register_method(
44         method  => "retrieve_session",
45         api_name        => "open-ils.auth.session.retrieve",
46         argc            => 1, #( sessionid )
47         note            => <<TEXT,
48 Pass in a sessionid and this returns the username associated with it
49 TEXT
50 );
51
52 __PACKAGE__->register_method(
53         method  => "delete_session",
54         api_name        => "open-ils.auth.session.delete",
55         argc            => 1, #( sessionid )
56         note            => <<TEXT,
57 Pass in a sessionid and this delete it from the cache 
58 TEXT
59 );
60
61
62 # -------------------------------------------------------------
63 # Implementation
64 # -------------------------------------------------------------
65 # -------------------------------------------------------------
66
67
68 # -------------------------------------------------------------
69 # connect to the memcache server
70 # -------------------------------------------------------------
71 sub initialize {
72
73         my $config_client = OpenSRF::Utils::SettingsClient->new();
74         my $memcache_servers = 
75                 $config_client->config_value( "apps","open-ils.auth", "app_settings","memcache" );
76
77         if( !$memcache_servers ) {
78                 throw OpenSRF::EX::Config ("No Memcache servers specified for open-ils.auth!");
79         }
80
81         if(!ref($memcache_servers)) {
82                 $memcache_servers = [$memcache_servers];
83         }
84         $cache_handle = OpenSRF::Utils::Cache->new( "open-ils.auth", 0, $memcache_servers );
85 }
86
87
88
89 # -------------------------------------------------------------
90 # We build a random hash and put the hash along with the 
91 # username into memcache (so that any backend may fulfill the
92 # auth request).
93 # -------------------------------------------------------------
94 sub init_authenticate {
95         my( $self, $client, $username ) = @_;
96         my $seed = md5_hex( time() . $$ . rand() . $username );
97         $cache_handle->put_cache( "_open-ils_seed_$username", $seed, 300 );
98         return $seed;
99 }
100
101 # -------------------------------------------------------------
102 # The temporary hash is removed from memcache.  
103 # We retrieve the password from storage and verify
104 # their password hash against our re-hashed version of the 
105 # password. If all goes well, we return the session id. 
106 # Otherwise, we return "0"
107 # -------------------------------------------------------------
108 sub complete_authenticate {
109         my( $self, $client, $username, $passwdhash ) = @_;
110
111         my $name = "open-ils.storage.actor.user.search.usrname";
112
113         use Data::Dumper;
114         warn "Completing Authentication\n";
115         my $session = OpenSRF::AppSession->create("open-ils.storage");
116         warn "session built\n";
117         my $request = $session->request( $name, $username );
118         warn "made request\n";
119         my $response = $request->recv();
120
121         warn "called receive\n";
122         warn Dumper $response;
123
124         if( $response and $response->isa("OpenSRF::EX") ) {
125                 warn "Throwing " . $response->stringify . "\n";
126                 throw $response ($response->stringify . "\n");
127         }
128
129         warn "getting user\n";
130
131         my $user_list = $response->content;
132
133         $session->disconnect();
134         $session->kill_me();
135
136         unless(ref($user_list)) {
137                 throw OpenSRF::EX::ERROR 
138                         ("No user info returned from storage for $username");
139         }
140
141         my $user = $user_list->[0];
142         
143         use Data::Dumper;
144         warn Dumper $user;
145
146         if(!$user or !ref($user) ) {
147                 throw OpenSRF::EX::ERROR ("No user for $username");
148         }
149
150         my $password = $user->passwd();
151         warn "Got password $password\n";
152         if(!$password) {
153                 throw OpenSRF::EX::ERROR ("No password exists for $username", ERROR);
154         }
155
156         my $current_seed = $cache_handle->get_cache("_open-ils_seed_$username");
157         $cache_handle->delete_cache( "_open-ils_seed_$username" );
158
159         unless($current_seed) {
160                 throw OpenSRF::EX::User 
161                         ("User must call open-ils.auth.init_authenticate first (or respond faster)");
162         }
163
164         my $hash = md5_hex($current_seed . $password);
165
166         if( $hash eq $passwdhash ) {
167
168                 my $session_id = md5_hex( time() . $$ . rand() ); 
169                 $cache_handle->put_cache( $session_id, $user, 28800 );
170                 return $session_id;
171
172         } else {
173
174                 return 0;
175         }
176 }
177
178 sub retrieve_session {
179         my( $self, $client, $sessionid ) = @_;
180         return $cache_handle->get_cache($sessionid);
181 }
182
183 sub delete_session {
184         my( $self, $client, $sessionid ) = @_;
185         return $cache_handle->delete_cache($sessionid);
186 }
187
188
189 1;