MFILE/HTTP.pm

filter: Statementstmt 43.9% 40 / 91 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 72.2% 13 / 18 Podpod 100% 1 / 1 totaltotal 33.1% 54 / 163 CC 19 SCAR 42.6
CC
19
Cov
30%
_is_authorized
182.0
rest_req
24.1
_is_fresh
12.0
CRAP 70.5
↑ Summary MFILE.pm »
1 # *************************************************************************
2 # Copyright (c) 2014, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32 #
33 # HTTP module
34 #
35 package App::MFILE::HTTP;
36  
37 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 ▶use App::CELL qw( $CELL $log $site $meta );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use HTTP::Request::Common qw( GET PUT POST DELETE );
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51  
52  
53 =head1 NAME
54  
55 App::MFILE::HTTP - general REST request forwarder for MFILE-based clients
56  
57  
58  
59 =head1 SYNOPSIS
60  
61 use App::MFILE::HTTP qw( rest_req );
62  
63  
64  
65 =head1 DESCRIPTION
66  
67 Module where C<rest_req> and other shared code resides.
68  
69  
70  
71 =head1 EXPORTS
72  
73 =cut
74  
75 1 our @EXPORT_OK = qw(
76 rest_req
77 _is_authorized
78 );
79  
80  
81  
82 =head1 FUNCTIONS
83  
84  
85 =head2 rest_req
86  
87 Algorithm: send request to REST server, get JSON response, decode it, return it.
88  
89 Takes a single _mandatory_ parameter: a LWP::UserAgent object
90  
91 Optionally takes PARAMHASH:
92  
93 server => [URI OF REST SERVER] default is 'http://0:5000'
94 method => [HTTP METHOD TO USE] default is 'GET'
95 nick => [NICK FOR BASIC AUTH] optional
96 password => [PASSWORD FOR BASIC AUTH] optional
97 path => [PATH OF REST RESOURCE] default is '/'
98 req_body => [HASHREF] optional
99  
100 Returns HASHREF containing:
101  
102 hr => HTTP::Response object (stripped of the body)
103 body => [BODY OF HTTP RESPONSE, IF ANY]
104  
105 =cut
106  
107 0 ▶sub rest_req {
Subroutine rest_req: not called CC=5 SCAR=31.8
Pod documented
108  
109 # process arguments
110 0 my $ua = shift;
111 0 ▶ die "Bad user agent object" unless ref( $ua ) eq 'LWP::UserAgent';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ua eq "LWP::UserAgent" 0 0 0
112 0 my %ARGS = validate( @_, {
113 server => { type => SCALAR, default => 'http://localhost:5000' },
114 method => { type => SCALAR, default => 'GET', regex => qr/^(GET|POST|PUT|DELETE)$/ },
115 nick => { type => SCALAR, optional => 1 },
116 password => { type => SCALAR, default => '' },
117 path => { type => SCALAR, default => '/' },
118 req_body => { type => HASHREF, optional => 1 },
119 } );
120 0 $ARGS{'path'} =~ s/^\/*/\//;
121  
122 0 my $r;
123 {
124 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
125 0 $r = &{ $ARGS{'method'} }( $ARGS{'server'} . encode_utf8( $ARGS{'path'} ),
126 Accept => 'application/json' );
127 }
128  
129 0 ▶ if ( $ARGS{'nick'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{"nick"}) 0 0 0
130 0 $r->authorization_basic( $ARGS{'nick'}, $ARGS{'password'} );
131 }
132  
133 0 ▶ if ( $ARGS{'method'} =~ m/^(POST|PUT)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS{"method"} =~ /^(POST|PUT)$/u) 0 0 0
134 0 $r->header( 'Content-Type' => 'application/json' );
135 0 ▶ if ( my $body = $ARGS{'req_body'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $ARGS{"req_body"}) 0 0 0
136 0 my $tmpvar = JSON->new->utf8(0)->encode( $body );
137 0 $r->content( encode_utf8( $tmpvar ) );
138 }
139 }
140  
141 # request is ready - send it and get response
142 0 my $response = $ua->request( $r );
143  
144 # process response
145 0 my $body_json = $response->decoded_content;
146 0 $log->debug( "rest_req: decoded JSON body " . Dumper $body_json );
147 0 $response->content('');
148 0 my $body;
149 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 $body = JSON->new->decode( $body_json );
151 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 $body = { 'code' => $body, 'text' => $body };
153 0 };
154  
155 return {
156 0 hr => $response,
157 body => $body
158 };
159 }
160  
161  
162 =head2 _is_authorized
163  
164 This function does the actual work for C<is_authorized> in the Dispatch.pm
165 module of an C<App::MFILE::WWW>-based application.
166  
167 This function belongs in Dispatch.pm - it is here only to prevent code
168 duplication.
169  
170 =cut
171  
172 0 ▶sub _is_authorized {
Subroutine _is_authorized: not called CC=13 SCAR=52.0
173 0 my ( $self ) = @_;
174  
175 0 $log->debug( "Entering " . __PACKAGE__ . "::_is_authorized()" );
176  
177 0 my $r = $self->request;
178 #my $session = $r->{'env'}->{'psgix.session'};
179 0 my $session = $self->session;
180 #my $remote_addr = $r->{'env'}->{'REMOTE_ADDR'};
181 0 my $remote_addr = $self->remote_addr;
182 0 my $ce;
183  
184 #$log->debug( "Environment is " . Dumper( $r->{'env'} ) );
185 0 $log->debug( "Session is " . Dumper( $session ) );
186  
187 # authorized session
188 0 ▶ if ( $ce = $session->{'currentUser'} and
Branch: 1 branch
BranchTrueFalseTotal
if ($ce = $session->{"currentUser"} and $session->{"ip_addr"} and $session->{"ip_addr"} eq $remote_addr and _is_fresh($session)) 0 0 0
Condition0%0/9
$ce = $session->{"currentUser"} and $session->{"ip_addr"} and $session->{"ip_addr"} eq $remote_addr and _is_fresh($session)
!ll&&!rl&&r
000
$ce = $session->{"currentUser"} and $session->{"ip_addr"} and $session->{"ip_addr"} eq $remote_addr
!ll&&!rl&&r
000
$ce = $session->{"currentUser"} and $session->{"ip_addr"}
!ll&&!rl&&r
000
MC/DC0%0/4
$ce = $session->{"currentUser"} and $session->{"ip_addr"} and $session->{"ip_addr"} eq $remote_addr and _is_fresh($session)
$ce = $session->{"currentUser"} $session->{"ip_addr"} $session->{"ip_addr"} eq $remote_addr _is_fresh($session)
Truth table0%0/5
A and B and C and D
A= $ce = $session->{"currentUser"}B= $session->{"ip_addr"}C= $session->{"ip_addr"} eq $remote_addrD= _is_fresh($session)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
189 $session->{'ip_addr'} and
190 $session->{'ip_addr'} eq $remote_addr and
191 _is_fresh( $session ) )
192 {
193 0 $log->debug( "is_authorized: Authorized session, employee " . $ce->{'nick'} );
194 0 $session->{'last_seen'} = time;
195 0 return 1;
196 }
197  
198 # login attempt
199 0 ▶ if ( $r->method eq 'POST' and
Branch: 1 branch
BranchTrueFalseTotal
if ($r->method eq "POST" and $self->context->{"request_body"} and $self->context->{"request_body"}{"method"} and $self->context->{"request_body"}{"method"} =~ /^LOGIN/iu) 0 0 0
Condition0%0/9
$r->method eq "POST" and $self->context->{"request_body"} and $self->context->{"request_body"}{"method"} and $self->context->{"request_body"}{"method"} =~ /^LOGIN/iu
!ll&&!rl&&r
000
$r->method eq "POST" and $self->context->{"request_body"} and $self->context->{"request_body"}{"method"}
!ll&&!rl&&r
000
$r->method eq "POST" and $self->context->{"request_body"}
!ll&&!rl&&r
000
MC/DC0%0/4
$r->method eq "POST" and $self->context->{"request_body"} and $self->context->{"request_body"}{"method"} and $self->context->{"request_body"}{"method"} =~ /^LOGIN/iu
$r->method eq "POST" $self->context->{"request_body"} $self->context->{"request_body"}{"method"} $self->context->{"request_body"}{"method"} =~ /^LOGIN/iu
Truth table0%0/5
A and B and C and D
A= $r->method eq "POST"B= $self->context->{"request_body"}C= $self->context->{"request_body"}{"method"}D= $self->context->{"request_body"}{"method"} =~ /^LOGIN/iu
ABCDresult
0XXX0
10XX0
110X0
11100
11111
200 $self->context->{'request_body'} and
201 $self->context->{'request_body'}->{'method'} and
202 $self->context->{'request_body'}->{'method'} =~ m/^LOGIN/i ) {
203 0 $log->debug( "is_authorized: Login attempt - pass it on" );
204 0 return 1;
205 }
206  
207 # login bypass
208 0 ▶ $meta->set('META_LOGIN_BYPASS_STATE', 0) if not defined $meta->META_LOGIN_BYPASS_STATE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $App::MFILE::HTTP::meta->META_LOGIN_BYPASS_STATE 0 0 0
209 0 ▶ if ( $site->MFILE_WWW_BYPASS_LOGIN_DIALOG and not $meta->META_LOGIN_BYPASS_STATE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::MFILE::HTTP::site->MFILE_WWW_BYPASS_LOGIN_DIALOG and not $App::MFILE::HTTP::meta->META_LOGIN_BYPASS_STATE) 0 0 0
Condition0%0/3
$App::MFILE::HTTP::site->MFILE_WWW_BYPASS_LOGIN_DIALOG and not $App::MFILE::HTTP::meta->META_LOGIN_BYPASS_STATE
!ll&&!rl&&r
000
MC/DC0%0/2
$App::MFILE::HTTP::site->MFILE_WWW_BYPASS_LOGIN_DIALOG and not $App::MFILE::HTTP::meta->META_LOGIN_BYPASS_STATE
$App::MFILE::HTTP::site->MFILE_WWW_BYPASS_LOGIN_DIALOG not $App::MFILE::HTTP::meta->META_LOGIN_BYPASS_STATE
Truth table0%0/3
A and not(B)
A= $App::MFILE::HTTP::site->MFILE_WWW_BYPASS_LOGIN_DIALOGB= not $App::MFILE::HTTP::meta->META_LOGIN_BYPASS_STATE
ABresult
0X0
100
111
210 0 $log->notice("Bypassing login dialog! Using default credentials");
211 0 $session->{'ip_addr'} = $remote_addr;
212 0 $session->{'last_seen'} = time;
213 my $bypass_result = $self->_login_dialog( {
214 'nam' => $site->MFILE_WWW_DEFAULT_LOGIN_CREDENTIALS->{'nam'},
215 0 'pwd' => $site->MFILE_WWW_DEFAULT_LOGIN_CREDENTIALS->{'pwd'},
216 } );
217 0 $meta->set('META_LOGIN_BYPASS_STATE', 1);
218 0 return $bypass_result;
219 }
220  
221 # unauthorized session
222 0 $log->debug( "is_authorized fall-through: " . $r->method . " " . $self->request->path_info );
223 0 ▶ return ( $r->method eq 'GET' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r->method eq 'GET' ? : 0 0 0
224 }
225  
226  
227 =head2 _is_fresh
228  
229 Takes a single argument, the PSGI session, which is assumed to contain a
230 C<last_seen> attribute containing the number of seconds since epoch when the
231 session was last seen.
232  
233 =cut
234  
235 0 ▶sub _is_fresh {
Subroutine _is_fresh: not called CC=3 SCAR=24.8
236 0 my ( $session ) = validate_pos( @_, { type => HASHREF } );
237  
238 0 ▶ return 0 unless my $last_seen = $session->{'last_seen'};
Branch: 1 branch
BranchTrueFalseTotal
unless my $last_seen = $session->{"last_seen"} 0 0 0
239  
240 0 ▶ return ( time - $last_seen > $site->MFILE_WWW_SESSION_EXPIRATION_TIME )
Branch: 1 branch
BranchTrueFalseTotal
time - $last_seen > $App::MFILE::HTTP::site->MFILE_WWW_SESSION_EXPIRATION_TIME ? : 0 0 0
241 ? 0
242 : 1;
243 }
244  
245  
246 1 1;
↑ Summary MFILE.pm »