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