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;
12 use OpenILS::Application::AppUtils;
17 my $apputils = "OpenILS::Application::AppUtils";
20 # -------------------------------------------------------------
22 # -------------------------------------------------------------
23 # -------------------------------------------------------------
25 __PACKAGE__->register_method(
26 method => "init_authenticate",
27 api_name => "open-ils.auth.authenticate.init",
28 argc => 1, #(username)
30 Generates a random seed and returns it. The client
31 must then perform md5_hex( \$seed . \$password ) and use that
32 as the passwordhash to open-ils.auth.authenticate.complete
36 __PACKAGE__->register_method(
37 method => "complete_authenticate",
38 api_name => "open-ils.auth.authenticate.complete",
39 argc => 2, #( barcode, passwdhash )
41 Client provides the username and passwordhash (see
42 open-ils.auth.authenticate.init). If their password hash is
43 correct for the given username, a session id is returned,
44 if not, "0" is returned
48 __PACKAGE__->register_method(
49 method => "retrieve_session",
50 api_name => "open-ils.auth.session.retrieve",
51 argc => 1, #( sessionid )
53 Pass in a sessionid and this returns the username associated with it
57 __PACKAGE__->register_method(
58 method => "delete_session",
59 api_name => "open-ils.auth.session.delete",
60 argc => 1, #( sessionid )
62 Pass in a sessionid and this delete it from the cache
67 # -------------------------------------------------------------
69 # -------------------------------------------------------------
70 # -------------------------------------------------------------
73 # -------------------------------------------------------------
74 # connect to the memcache server
75 # -------------------------------------------------------------
77 $cache_handle = OpenSRF::Utils::Cache->new('global');
81 # -------------------------------------------------------------
82 # We build a random hash and put the hash along with the
83 # username into memcache (so that any backend may fulfill the
85 # -------------------------------------------------------------
86 sub init_authenticate {
87 my( $self, $client, $username ) = @_;
88 my $seed = md5_hex( time() . $$ . rand() . $username );
89 $cache_handle->put_cache( "_open-ils_seed_$username", $seed, 30 );
90 warn "init happened with seed $seed\n";
94 # -------------------------------------------------------------
95 # The temporary hash is removed from memcache.
96 # We retrieve the password from storage and verify
97 # their password hash against our re-hashed version of the
98 # password. If all goes well, we return the session id.
99 # Otherwise, we return "0"
100 # If type is set to 'opac', then this is an opac login,
101 # otherwise, it's a staff login
102 # -------------------------------------------------------------
103 sub complete_authenticate {
104 my( $self, $client, $username, $passwdhash, $type ) = @_;
106 my $name = "open-ils.storage.direct.actor.user.search.usrname.atomic";
108 warn "Completing Authentication\n";
110 warn "Retrieving user from storage..\n";
111 my $user_list = OpenILS::Application::AppUtils->simple_scalar_request(
112 "open-ils.storage", $name, $username );
114 unless(ref($user_list)) {
115 return OpenILS::EX->new("UNKNOWN_USER")->ex;
118 warn "We have the user from storage with usrname $username\n";
120 my $user = $user_list->[0];
123 if(!$user or !ref($user) ) {
124 return OpenILS::EX->new("UNKNOWN_USER")->ex();
127 my $password = $user->passwd();
128 warn "user passwd is $password\n";
131 throw OpenSRF::EX::ERROR ("No password exists for $username", ERROR);
134 warn "we have a password\n";
136 my $current_seed = $cache_handle->get_cache("_open-ils_seed_$username");
137 $cache_handle->delete_cache( "_open-ils_seed_$username" );
139 warn "Removed tmp data from cache\n";
141 unless($current_seed) {
142 throw OpenSRF::EX::User
143 ("User must call open-ils.auth.init_authenticate first (or respond faster)");
146 my $hash = md5_hex($current_seed . $password);
148 if( $hash eq $passwdhash ) {
149 # password is correct... do they have permission to login here?
151 my $timeout = 28800; #staff login timeout - different for opac?
152 my $opactimeout = 604800; # 14 days
154 if($type eq "opac") {
155 # 1 is the top level org unit (we should probably load the tree and get id from it)
156 warn "Checking user perms for OPAC login\n";
157 if($apputils->check_user_perms($user->id(), 1, "OPAC_LOGIN")) {
158 return OpenILS::Perm->new("OPAC_LOGIN");
162 # 1 is the top level org unit (we should probably load the tree and get id from it)
163 warn "Checking user perms for Staff login\n";
164 if($apputils->check_user_perms($user->id(), 1, "STAFF_LOGIN")) {
165 return OpenILS::Perm->new("STAFF_LOGIN");
169 my $session_id = md5_hex(time() . $$ . rand());
170 $cache_handle->put_cache( $session_id, $user, $timeout );
175 warn "User password is incorrect...\n"; # send exception
180 sub retrieve_session {
181 my( $self, $client, $sessionid ) = @_;
182 my $user = $cache_handle->get_cache($sessionid);
184 warn "No User returned from retrieve_session $sessionid\n";
186 if($user) {$user->clear_passwd();}
191 my( $self, $client, $sessionid ) = @_;
192 return $cache_handle->delete_cache($sessionid);