MToken/Server.pm

filter: Statementstmt 45.8% 33 / 72 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 76.9% 10 / 13 Podpod 100% 2 / 2 totaltotal 34.8% 45 / 129 CC 16 SCAR 39.1
CC
16
Cov
31%
startup
90.0
__ANON__
72.0
reload
2.0
CRAP 49.9
« MToken/Config.pm ↑ Summary MToken/Store.pm »
1 package MToken::Server; # $Id: Server.pm 112 2021-10-11 11:53:20Z minus $
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 MToken::Server - MToken web-server class
11  
12 =head1 VERSION
13  
14 Version 1.03
15  
16 =head1 SYNOPSIS
17  
18 use MToken::Server;
19  
20 =head1 DESCRIPTION
21  
22 This module provides MToken web-server functionality
23  
24 =head2 reload
25  
26 The reload hook
27  
28 =head2 startup
29  
30 Mojo application startup method
31  
32 =head1 HISTORY
33  
34 See C<Changes> file
35  
36 =head1 TO DO
37  
38 See C<TODO> file
39  
40 =head1 SEE ALSO
41  
42 L<Mojolicious>, L<MToken>
43  
44 =head1 AUTHOR
45  
46 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
47  
48 =head1 COPYRIGHT
49  
50 Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved
51  
52 =head1 LICENSE
53  
54 This program is free software; you can redistribute it and/or
55 modify it under the same terms as Perl itself.
56  
57 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
58  
59 =cut
60  
61 1 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 $VERSION = "1.03";
63  
64 1 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 ▶use Mojo::File qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Mojo::Util qw/sha1_sum secure_compare/;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 ▶use CTK::Util qw/preparedir sharedir sharedstatedir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use CTK::ConfGenUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶use MToken::Const;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 1 has 'documentroot';
75  
76 # Mojo Routes (paths)
77 0 ▶sub startup {
Subroutine startup: not called CC=9 SCAR=45.0
Pod documented
78 0 my $self = shift; # app
79 0 ▶ my $ctk = $self->can('ctk') ? $self->ctk() : $self->{ctk};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('ctk') ? : 0 0 0
80  
81 # Set password
82 0 ▶ my $username = value($ctk->conf("username") // "");
Condition0%0/2
$ctk->conf("username") // ""
l!l
00
MC/DC0%0/1
$ctk->conf("username") // ""
$ctk->conf("username")
Truth table0%0/2
A
A= $ctk->conf("username")
Aresult
11
00
83 0 ▶ my $secret = value($ctk->conf("password") // "");
Condition0%0/2
$ctk->conf("password") // ""
l!l
00
MC/DC0%0/1
$ctk->conf("password") // ""
$ctk->conf("password")
Truth table0%0/2
A
A= $ctk->conf("password")
Aresult
11
00
84 0 ▶ $self->secrets([$secret]) if length($secret);
Branch: 1 branch
BranchTrueFalseTotal
if length $secret 0 0 0
85  
86 # Logging
87 0 ▶ if ($ctk->debugmode) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ctk->debugmode) { } 0 0 0
elsif ($ctk->verbosemode) { } 0 0 0
88 0 $self->log->level("debug")->path($ctk->logfile())
89 } elsif ($ctk->verbosemode()) {
90 0 $self->log->level("info")->path($ctk->logfile())
91 } else {
92 0 $self->log->level("warn")->path($ctk->logfile())
93 }
94 #$self->log->debug("Startup!! =$$"); # $self->ctk->logdir()
95  
96 # Switch to installable home directory
97 0 $self->home(Mojo::Home->new($ctk->datadir()));
98  
99 # Get DocumentRoot and replace as public-path
100 0 ▶ my $documentroot = value($ctk->conf("documentroot")) || path(sharedir(), $ctk->prefix)->to_string();
Condition0%0/3
value($ctk->conf('documentroot')) || path(sharedir(), $ctk->prefix)->to_string
l!l&&r!l&&!r
000
MC/DC0%0/2
value($ctk->conf('documentroot')) || path(sharedir(), $ctk->prefix)->to_string
value($ctk->conf('documentroot')) path(sharedir(), $ctk->prefix)->to_string
Truth table0%0/3
A || B
A= value($ctk->conf('documentroot'))B= path(sharedir(), $ctk->prefix)->to_string
ABresult
1X1
011
000
101 0 $self->documentroot($documentroot);
102 0 $self->static->paths()->[0] = $documentroot; #unshift @{$static->paths}, '/home/sri/themes/blue/public';
103 0 $self->static->paths()->[1] = $ctk->datadir();
104  
105 # Hooks
106 0 ▶ $self->hook(before_dispatch => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
107 0 my $c = shift;
108  
109 # Set Server header
110 0 $c->res->headers->server(sprintf("%s/%s", PROJECTNAME, $self->VERSION));
111 0 $c->app->log->debug("Start request dispatch");
112  
113 # Authentication
114 0 ▶ my $need_auth = length($username) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $username ? : 0 0 0
115 0 ▶ if ($need_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_auth) 0 0 0
116 0 my $req_uri = $c->req->url->to_abs();
117 0 ▶ my $ui_username = $req_uri->username() // "";
Condition0%0/2
$req_uri->username // ""
l!l
00
MC/DC0%0/1
$req_uri->username // ""
$req_uri->username
Truth table0%0/2
A
A= $req_uri->username
Aresult
11
00
118 0 ▶ my $ui_secret = sha1_sum($req_uri->password() // time());
Condition0%0/2
$req_uri->password // time
l!l
00
MC/DC0%0/1
$req_uri->password // time
$req_uri->password
Truth table0%0/2
A
A= $req_uri->password
Aresult
11
00
119  
120 # Check username and password
121 0 ▶ return 1 if length($secret)
Branch: 1 branch
BranchTrueFalseTotal
if length $secret and secure_compare($username, $ui_username) and secure_compare($secret, $ui_secret) 0 0 0
Condition0%0/6
length $secret and secure_compare($username, $ui_username) and secure_compare($secret, $ui_secret)
!ll&&!rl&&r
000
length $secret and secure_compare($username, $ui_username)
!ll&&!rl&&r
000
MC/DC0%0/3
length $secret and secure_compare($username, $ui_username) and secure_compare($secret, $ui_secret)
length $secret secure_compare($username, $ui_username) secure_compare($secret, $ui_secret)
Truth table0%0/4
A and B and C
A= length $secretB= secure_compare($username, $ui_username)C= secure_compare($secret, $ui_secret)
ABCresult
0XX0
10X0
1100
1111
122 and secure_compare($username, $ui_username)
123 and secure_compare($secret, $ui_secret);
124  
125 # Require authentication
126 0 $c->res->headers->www_authenticate('Basic realm="MToken Strict Zone"');
127 0 return $c->render(json => {
128 message => "Authentication required!",
129 }, status => 401);
130 }
131  
132 0 return;
133 0 });
134  
135 # Routes
136 0 $self->routes->get('/')->to('alpha#root');
137 0 ▶ $self->routes->get('/env')->to('alpha#env') if $ctk->debugmode();
Branch: 1 branch
BranchTrueFalseTotal
if $ctk->debugmode 0 0 0
138 0 $self->routes->get('/mtoken')->to('alpha#info');
139 0 $self->routes->get('/mtoken/:token' => [token => qr/[a-z][a-z0-9]+/])->to('alpha#list');
140 0 $self->routes->get('/mtoken/:token/:tarball' =>
141 [
142 token => qr/[a-z][a-z0-9]+/,
143 tarball => qr/C[0-9]{8}T[0-9]{6}\.tkn/,
144 ]
145 )->to('alpha#download_tarball');
146 0 $self->routes->put('/mtoken/:token/:tarball' =>
147 [
148 token => qr/[a-z][a-z0-9]+/,
149 tarball => qr/C[0-9]{8}T[0-9]{6}\.tkn/,
150 ]
151 )->to('alpha#upload_tarball');
152 0 $self->routes->delete('/mtoken/:token/:tarball' =>
153 [
154 token => qr/[a-z][a-z0-9]+/,
155 tarball => qr/C[0-9]{8}T[0-9]{6}\.tkn/,
156 ]
157 )->to('alpha#delete_tarball');
158  
159 # Delete std favicon file from static
160 0 delete $self->static->extra->{'favicon.ico'};
161  
162 0 return 1;
163 }
164  
165 # Reload hook
166 0 ▶sub reload {
Subroutine reload: not called CC=1 SCAR=6.9
Pod documented
167 0 my $self = shift;
168 0 $self->log->warn("Request for reload $$"); # $self->ctk->logdir()
169 0 return 1; # 1 - ok; 0 - error :(
170 }
171  
172 1 1;
173  
174 __END__
« MToken/Config.pm ↑ Summary MToken/Store.pm »