App/MFILE/WWW/Dispatch.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 113 CC 8 SCAR 26.7
CC
8
Cov
0%
process_post
30.0
validate_user_credentials
20.0
_logout
2.0
CRAP 14.5
1 # *************************************************************************
2 # Copyright (c) 2014-2017, 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 # ------------------------
34 # This package defines how our web server handles the request-response
35 # cycle. All the "heavy lifting" is done by Web::Machine and Plack.
36 # ------------------------
37  
38 package App::MFILE::WWW::Dispatch;
39  
40 use strict;
41 use warnings;
42  
43 use App::CELL qw( $CELL $log $meta $site );
44 use Data::Dumper;
45 use JSON;
46 use Params::Validate qw(:all);
47  
48 # methods/attributes not defined in this module will be inherited from:
49 use parent 'App::MFILE::WWW::Resource';
50  
51  
52  
53 =head1 NAME
54  
55 App::MFILE::WWW::Dispatch - app dispatch stub
56  
57  
58  
59 =head1 SYNOPSIS
60  
61 TBD
62  
63  
64  
65 =head1 DESCRIPTION
66  
67 This is where we override the default version of the is_authorized method
68 defined by L<Web::Machine::Resource>.
69  
70 This module is only used in standalone mode. In derived distribution mode, the
71 application's dispatch module will be used, instead.
72  
73 =cut
74  
75  
76  
77 =head1 METHODS
78  
79  
80 =head2 process_post
81  
82 In the standalone demo-application mode, POST requests are used to handle
83 login/logout requests generated by login-dialog.js.
84  
85 Login requests look like this:
86  
87 { method: "LOGIN", path: "login", body: { nam: "foo", pwd: "bar" } }
88  
89 and logout requests like this:
90  
91 { method: "LOGIN", path: "logout" }
92  
93 In derived-distro mode, POST requests are also used to implement AJAX calls. See
94 the C<process_post> function in C<App::Dochazka::WWW::Dispatch> for a real
95 implementation.
96  
97 =cut
98  
99 sub process_post {
100 my $self = shift;
101 $log->debug( "Entering " . __PACKAGE__ . "::process_post()" );
102  
103 my $r = $self->request;
104 my $session = $r->{'env'}->{'psgix.session'};
105 my $ajax = $self->context->{'request_body'}; # request body (Perl string)
106  
107 if ( ! $ajax ) {
108 $log->crit( 'POST request received, but without a body' );
109 return 0;
110 }
111  
112 my $method = $ajax->{'method'};
113 my $path = $ajax->{'path'};
114 my $body = $ajax->{'body'} || {};
115  
116 $log->debug( "process_post: method $method, path $path, body " . Dumper $body );
117  
118 if ( ! $method or ! $path or ! $body ) {
119 $log->crit( 'POST request received, but missing mandatory attribute(s) - ' .
120 'here is the entire request body: ' . Dumper( $ajax ) );
121 return 0;
122 }
123  
124 # POST is used only for login/logout ATM
125 if ( $method =~ m/^LOGIN/i ) {
126 $log->debug( "Incoming login/logout attempt" );
127 if ( $path =~ m/^login/i ) {
128 return $self->validate_user_credentials( $body );
129 } else {
130 return $self->_logout( $body );
131 }
132 }
133  
134 $log->crit( "Asked to perform an AJAX call, but feature is not implemented!" );
135 return 0;
136 }
137  
138  
139 =head2 validate_user_credentials
140  
141 Called from C<process_post> to process login requests (special AJAX requests)
142 originating from the JavaScript side (i.e. the login screen in
143 login-dialog.js, via login.js).
144  
145 Returns a status object - OK means the login was successful; all other statuses
146 mean unsuccessful.
147  
148 =cut
149  
150 sub validate_user_credentials {
151 my ( $self, $body ) = @_;
152 $log->debug( "Entering " . __PACKAGE__ . "::validate_user_credentials()" );
153  
154 my $r = $self->request;
155 my $session = $r->{'env'}->{'psgix.session'};
156 my $nick = $body->{'nam'};
157 my $password = $body->{'pwd'};
158 my $standalone = $meta->META_WWW_STANDALONE_MODE;
159  
160 $log->debug( "Employee $nick login attempt" );
161  
162 my ( $code, $message, $body_json );
163 if ( $standalone ) {
164 # check nam and pwd against MFILE_WWW_STANDALONE_CREDENTIALS_DATABASE
165 my $db = $site->MFILE_WWW_STANDALONE_CREDENTIALS_DATABASE;
166 $code = 401;
167 $message = 'Unauthorized';
168 for my $entry (@$db) {
169 if ( $nick eq $entry->{'nam'} ) {
170 if ( $password eq $entry->{'pwd'} ) {
171 $code = 200;
172 $message = 'OK';
173 $body_json = { payload =>
174 {
175 emp => { nick => $nick, eid => $entry->{'eid'} },
176 priv => $entry->{'priv'}
177 }
178 };
179 }
180 last;
181 }
182 }
183 } else {
184 $log->crit( "Not running in standalone mode" );
185 return $CELL->status_not_ok();
186 }
187  
188 my $status = $self->login_status( $code, $message, $body_json );
189 $log->debug( "login_status() returned" . Dumper( $status ) );
190 return $status;
191 }
192  
193  
194 =head2 _logout
195  
196 Called from C<process_post> to process logout requests (special AJAX requests)
197 originating from the JavaScript side.
198  
199 =cut
200  
201 sub _logout {
202 my ( $self, $body ) = @_;
203 $log->debug( "Entering " . __PACKAGE__ . "::_logout()" );
204 $self->request->{'env'}->{'psgix.session'} = {};
205 $self->response->header( 'Content-Type' => 'application/json' );
206 $self->response->body( to_json( $CELL->status_ok( 'MFILE_WWW_LOGOUT_OK' )->expurgate ) );
207 return 1;
208 }
209  
210 1;