]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Reporter/Proxy.pm
updating the proxy
[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(REDIRECT FORBIDDEN 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 Apache2::Const::OK;
47                         }
48
49                         print $cgi->header(-type=>'text/html', -expires=>'-1d');
50                         print <<"                       HTML";
51 <html>
52         <head>
53                 <title>Report Output Login</title>
54         </head>
55         <body>
56                 <form method='POST'>
57                         <table style='border-collapse: collapse;'>
58                                 <tr>
59                                         <th colspan='2' align='center'><u>Please log in to view reports</u></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 Apache2::Const::OK;
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 Apache2::Const::REDIRECT;
89                 }
90         }
91
92         my $user = verify_login($auth_ses);
93         return Apache2::Const::FORBIDDEN 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::FORBIDDEN 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 undef 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;