]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Auth.pm
cleaning up code from cat days.
[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 use OpenILS::Application::AppUtils;
11
12 # memcache handle
13 my $cache_handle;
14
15
16 # -------------------------------------------------------------
17 # Methods
18 # -------------------------------------------------------------
19 # -------------------------------------------------------------
20
21 __PACKAGE__->register_method(
22         method  => "init_authenticate",
23         api_name        => "open-ils.auth.authenticate.init",
24         argc            => 1, #(username) 
25         note            =>      <<TEXT,
26 Generates a random seed and returns it.  The client
27 must then perform md5_hex( \$seed . \$password ) and use that
28 as the passwordhash to open-ils.auth.authenticate.complete
29 TEXT
30 );
31
32 __PACKAGE__->register_method(
33         method  => "complete_authenticate",
34         api_name        => "open-ils.auth.authenticate.complete",
35         argc            => 2, #( barcode, passwdhash )
36         note            => <<TEXT,
37 Client provides the username and passwordhash (see 
38 open-ils.auth.authenticate.init).  If their password hash is 
39 correct for the given username, a session id is returned, 
40 if not, "0" is returned
41 TEXT
42 );
43
44 __PACKAGE__->register_method(
45         method  => "retrieve_session",
46         api_name        => "open-ils.auth.session.retrieve",
47         argc            => 1, #( sessionid )
48         note            => <<TEXT,
49 Pass in a sessionid and this returns the username associated with it
50 TEXT
51 );
52
53 __PACKAGE__->register_method(
54         method  => "delete_session",
55         api_name        => "open-ils.auth.session.delete",
56         argc            => 1, #( sessionid )
57         note            => <<TEXT,
58 Pass in a sessionid and this delete it from the cache 
59 TEXT
60 );
61
62
63 # -------------------------------------------------------------
64 # Implementation
65 # -------------------------------------------------------------
66 # -------------------------------------------------------------
67
68
69 # -------------------------------------------------------------
70 # connect to the memcache server
71 # -------------------------------------------------------------
72 sub child_init {
73         $cache_handle = OpenSRF::Utils::Cache->new('global');
74 }
75
76
77 # -------------------------------------------------------------
78 # We build a random hash and put the hash along with the 
79 # username into memcache (so that any backend may fulfill the
80 # auth request).
81 # -------------------------------------------------------------
82 sub init_authenticate {
83         my( $self, $client, $username ) = @_;
84         my $seed = md5_hex( time() . $$ . rand() . $username );
85         $cache_handle->put_cache( "_open-ils_seed_$username", $seed, 30 );
86         warn "init happened with seed $seed\n";
87         return $seed;
88 }
89
90 # -------------------------------------------------------------
91 # The temporary hash is removed from memcache.  
92 # We retrieve the password from storage and verify
93 # their password hash against our re-hashed version of the 
94 # password. If all goes well, we return the session id. 
95 # Otherwise, we return "0"
96 # -------------------------------------------------------------
97 sub complete_authenticate {
98         my( $self, $client, $username, $passwdhash ) = @_;
99
100         my $name = "open-ils.storage.direct.actor.user.search.usrname";
101
102         my $user_list = OpenILS::Application::AppUtils->simple_scalar_request(
103                         "open-ils.storage", $name, $username );
104
105         unless(ref($user_list)) {
106                 throw OpenSRF::EX::ERROR 
107                         ("No user info returned from storage for $username");
108         }
109
110         my $user = $user_list->[0];
111         
112
113         if(!$user or !ref($user) ) {
114                 throw OpenSRF::EX::ERROR ("No user for $username");
115         }
116
117         my $password = $user->passwd();
118
119         if(!$password) {
120                 throw OpenSRF::EX::ERROR ("No password exists for $username", ERROR);
121         }
122
123         my $current_seed = $cache_handle->get_cache("_open-ils_seed_$username");
124         $cache_handle->delete_cache( "_open-ils_seed_$username" );
125
126         unless($current_seed) {
127                 throw OpenSRF::EX::User 
128                         ("User must call open-ils.auth.init_authenticate first (or respond faster)");
129         }
130
131         my $hash = md5_hex($current_seed . $password);
132
133         if( $hash eq $passwdhash ) {
134
135                 my $session_id = md5_hex( time() . $$ . rand() ); 
136                 $cache_handle->put_cache( $session_id, $user, 3600 );
137                 return $session_id;
138
139         } else {
140
141                 return 0;
142         }
143 }
144
145 sub retrieve_session {
146         my( $self, $client, $sessionid ) = @_;
147         my $user =  $cache_handle->get_cache($sessionid);
148         if(!$user) {
149                 warn "No User returned from retrieve_session $sessionid\n";
150         }
151         if($user) {$user->clear_password();}
152         use Data::Dumper;
153         warn Dumper $user;
154         return $user;
155 }
156
157 sub delete_session {
158         my( $self, $client, $sessionid ) = @_;
159         return $cache_handle->delete_cache($sessionid);
160 }
161
162
163 1;