line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MToken::Server; # $Id: Server.pm 112 2021-10-11 11:53:20Z minus $ |
2
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
86
|
|
4
|
1
|
|
|
1
|
|
9
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
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 file |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 TO DO |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
See C file |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 SEE ALSO |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
L, L |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 AUTHOR |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
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 file and L |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
|
47
|
use vars qw/ $VERSION /; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
66
|
|
62
|
|
|
|
|
|
|
$VERSION = "1.03"; |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
1
|
|
19
|
use Mojo::Base 'Mojolicious'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
19
|
|
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
1
|
|
93484
|
use Mojo::File qw/path/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
87
|
|
67
|
1
|
|
|
1
|
|
7
|
use Mojo::Util qw/sha1_sum secure_compare/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
1
|
|
6
|
use CTK::Util qw/preparedir sharedir sharedstatedir/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
65
|
|
70
|
1
|
|
|
1
|
|
7
|
use CTK::ConfGenUtil; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
94
|
|
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
1
|
|
17
|
use MToken::Const; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1303
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
has 'documentroot'; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Mojo Routes (paths) |
77
|
|
|
|
|
|
|
sub startup { |
78
|
0
|
|
|
0
|
1
|
|
my $self = shift; # app |
79
|
0
|
0
|
|
|
|
|
my $ctk = $self->can('ctk') ? $self->ctk() : $self->{ctk}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Set password |
82
|
0
|
|
0
|
|
|
|
my $username = value($ctk->conf("username") // ""); |
83
|
0
|
|
0
|
|
|
|
my $secret = value($ctk->conf("password") // ""); |
84
|
0
|
0
|
|
|
|
|
$self->secrets([$secret]) if length($secret); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Logging |
87
|
0
|
0
|
|
|
|
|
if ($ctk->debugmode) { |
|
|
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
|
|
0
|
|
|
|
my $documentroot = value($ctk->conf("documentroot")) || path(sharedir(), $ctk->prefix)->to_string(); |
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
|
|
|
|
|
|
|
$self->hook(before_dispatch => sub { |
107
|
0
|
|
|
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
|
0
|
|
|
|
|
my $need_auth = length($username) ? 1 : 0; |
115
|
0
|
0
|
|
|
|
|
if ($need_auth) { |
116
|
0
|
|
|
|
|
|
my $req_uri = $c->req->url->to_abs(); |
117
|
0
|
|
0
|
|
|
|
my $ui_username = $req_uri->username() // ""; |
118
|
0
|
|
0
|
|
|
|
my $ui_secret = sha1_sum($req_uri->password() // time()); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Check username and password |
121
|
0
|
0
|
0
|
|
|
|
return 1 if length($secret) |
|
|
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
$self->routes->get('/env')->to('alpha#env') if $ctk->debugmode(); |
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
|
|
|
|
|
|
|
sub reload { |
167
|
0
|
|
|
0
|
1
|
|
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; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
__END__ |