]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Reporter/Proxy.pm
allow login when no cookie is present
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Reporter / Proxy.pm
1 package OpenILS::Reporter::Proxy;
2 use strict; use warnings;
3
4 use Apache2 ();
5 use Apache2::Log;
6 use Apache2::Const -compile => qw(OK NOT_FOUND DECLINED :log);
7 use APR::Const    -compile => qw(:error SUCCESS);
8 use CGI;
9 use Data::Dumper;
10 use Digest::MD5 qw/md5_hex/;
11
12 use OpenSRF::EX qw(:try);
13 use OpenSRF::System;
14
15
16 # set the bootstrap config and template include directory when 
17 # this module is loaded
18 my $bootstrap;
19
20 sub import {
21         my $self = shift;
22         $bootstrap = shift;
23 }
24
25
26 sub child_init {
27         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
28 }
29
30 sub handler {
31         my $apache = shift;
32         my $cgi = new CGI;
33         my $auth_ses = $cgi->cookie('ses');
34         my $ws_ou = $cgi->cookie('ws_ou') || 1;
35
36         if (!$auth_ses) {
37                 my $u = $cgi->param('user');
38                 my $p = $cgi->param('passwd');
39
40                 my $url = $cgi->url;
41
42                 if (!$u) {
43                         if ($url =~ /^http:/o) {
44                                 $url =~ s/^http:/https:/o;
45                                 print "Location: $url\n\n";
46                                 return 200;
47                         }
48
49                         print <<"                       HTML";
50 Content-type: text/html
51
52 <html>
53         <head>
54                 <title>Report Output Login</title>
55         </head>
56         <body>
57                 <form method='POST'>
58                         <table>
59                                 <tr>
60                                         <th colspan='2' align='center'>Please log in to view reports</th>
61                                 </tr>
62                                 <tr>
63                                         <th>Username or barcode:</th>
64                                         <td><input type="text" name="user"/></td>
65                                 </tr>
66                                 <tr>
67                                         <th>Password:</th>
68                                         <td><input type="password" name="passwd"/></td>
69                                 </tr>
70                         </table>
71                         <input type="submit" value="Log in"/>
72                 </form>
73         </body>
74 </html>
75                         HTML
76                         return 200;
77                 }
78
79                 $auth_ses = oils_login($u, $p);
80                 if ($auth_ses) {
81                         print $cgi->redirect(
82                                 -uri=>$url,
83                                 -cookie=>$cgi->cookie(
84                                         -name=>'ses',
85                                         -value=>$auth_ses,
86                                         -path=>'/',-expires=>'+1h'
87                                 )
88                         );
89                         return 302;
90                 }
91         }
92
93         my $user = verify_login($auth_ses);
94         return Apache2::Const::NOT_FOUND unless ($user);
95
96         my $failures = OpenSRF::AppSession
97                 ->create('open-ils.actor')
98                 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, ['RUN_REPORTS'])
99                 ->gather(1);
100
101         return Apache2::Const::NOT_FOUND if (@$failures > 0);
102
103         # they're good, let 'em through
104         return Apache2::Const::DECLINED if (-e $apache->filename);
105
106         # oops, file not found
107         return Apache2::Const::NOT_FOUND;
108 }
109
110 # returns the user object if the session is valid, 0 otherwise
111 sub verify_login {
112         my $auth_token = shift;
113         return undef unless $auth_token;
114
115         my $user = OpenSRF::AppSession
116                 ->create("open-ils.auth")
117                 ->request( "open-ils.auth.session.retrieve", $auth_token )
118                 ->gather(1);
119
120         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
121                 return undef;
122         }
123
124         return $user if ref($user);
125         return undef;
126 }
127
128 sub oils_login {
129         my( $username, $password, $type ) = @_;
130
131         $type |= "staff";
132         my $nametype = 'username';
133         $nametype = 'barcode' if ($username =~ /^\d+$/o);
134
135         my $seed = OpenSRF::AppSession
136                 ->create("open-ils.auth")
137                 ->request( 'open-ils.auth.authenticate.init', $username )
138                 ->gather(1);
139
140         return undef unless $seed;
141
142         my $response = OpenSRF::AppSession
143                 ->create("open-ils.auth")
144                 ->request( 'open-ils.auth.authenticate.complete',
145                         { $nametype => $username,
146                           password => md5_hex($seed . md5_hex($password)),
147                           type => $type })
148                 ->gather(1);
149
150         return undef unless $response;
151
152         return $response->{payload}->{authtoken};
153 }
154
155
156
157 1;