| 1 | package CallBackery; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | CallBackery - Appliance Frontend Builder | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | require Mojolicious::Commands; | ||||||||||||||||||||||||||||
| 11 | Mojolicious::Commands->start_app('CallBackery'); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Configure the mojo engine to run our application logic as webrequests arrive. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # load the two modules to have perl check them | ||||||||||||||||||||||||||||
| 22 | # having a non-C locale for number will wreck all sorts of havoc | ||||||||||||||||||||||||||||
| 23 | # when things get converted to string and back | ||||||||||||||||||||||||||||
| 24 | 12 | ▶ | use POSIX qw(locale_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 12 | setlocale(LC_NUMERIC, "C"); | |||||||||||||||||||||||||||
| 26 | 12 | setlocale(LC_TIME, "C"); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 12 | ▶ | use Mojo::Base 'Mojolicious'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 12 | ▶ | use Mojolicious::Plugin::Qooxdoo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 12 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 12 | ▶ | use Mojo::JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 12 | ▶ | use Mojo::Util qw(hmac_sha1_sum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 12 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 12 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 12 | ▶ | use CallBackery::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 12 | ▶ | use CallBackery::Plugin::Doc; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 12 | ▶ | use CallBackery::Database; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 12 | ▶ | use CallBackery::User; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 12 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 12 | our $VERSION = '0.62.1'; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 config | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | A hash pointer to the configuration object. See L<CallBackery::Config> for details. | ||||||||||||||||||||||||||||
| 46 | The default configuration file is located in etc/callbackery.cfg. You can override the | ||||||||||||||||||||||||||||
| 47 | path by setting the C<CALLBACKERY_CONF> environment variable. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | The config property is set automatically on startup. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 13 | ▶ | has 'config' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 54 | 13 | my $app = shift; | |||||||||||||||||||||||||||
| 55 | my $conf = CallBackery::Config->new( | ||||||||||||||||||||||||||||
| 56 | app => $app, | ||||||||||||||||||||||||||||
| 57 | 13 | ▶ | file => $ENV{CALLBACKERY_CONF} || $app->home->child('etc','callbackery.cfg') | ||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'CALLBACKERY_CONF'} || $app->home->child('etc', 'callbackery.cfg')
MC/DC0%0/2
$ENV{'CALLBACKERY_CONF'} || $app->home->child('etc', 'callbackery.cfg')
$ENV{'CALLBACKERY_CONF'}
$app->home->child('etc', 'callbackery.cfg')
Truth table33%1/3
A || B
A= $ENV{'CALLBACKERY_CONF'}B= $app->home->child('etc', 'callbackery.cfg')
| |||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | 12 | }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 database | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | An instance of L<CallBackery::Database> or a module with the same API. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | has database => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 7 | CallBackery::Database->new(app=>shift); | |||||||||||||||||||||||||||
| 69 | 12 | }; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | has userObject => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 6 | my $app = shift; | |||||||||||||||||||||||||||
| 73 | 6 | my $ obj = CallBackery::User->new(app=>$app,log=>$app->log); | |||||||||||||||||||||||||||
| 74 | 6 | $obj->{prototype} = 1; | |||||||||||||||||||||||||||
| 75 | 6 | return $obj; | |||||||||||||||||||||||||||
| 76 | 12 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 securityHeaders | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | A hash of headers to set on every response to ask the webbrowser to | ||||||||||||||||||||||||||||
| 81 | help us fight the bad guys. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 7 | ▶ | has securityHeaders => sub { { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | # prevent click jacking | ||||||||||||||||||||||||||||
| 87 | 7 | 'X-Frame-Options' => 'SAMEORIGIN', | |||||||||||||||||||||||||||
| 88 | # some magic browser based anti xss action | ||||||||||||||||||||||||||||
| 89 | 'X-XSS-Protection' => '1; mode=block', | ||||||||||||||||||||||||||||
| 90 | # the browser should obej the servers settings regarding content-type | ||||||||||||||||||||||||||||
| 91 | 'X-Content-Type-Options' => 'nosniff', | ||||||||||||||||||||||||||||
| 92 | # do not store our data ever | ||||||||||||||||||||||||||||
| 93 | 'Pragma' => 'private', | ||||||||||||||||||||||||||||
| 94 | 12 | }}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 rpcServiceNamespace | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | our rpc service namespace | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 12 | has rpcServiceNamespace => 'CallBackery'; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 rpcServiceController | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | our rpc service controller | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 12 | has rpcServiceController => 'Controller::RpcService'; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 docIndex | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | initial document to be presented on the doc link | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 12 | has docIndex => __PACKAGE__ . '::Index'; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 METHODS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | All the methods of L<Mojolicious> as well as: | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 startup | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Mojolicious calls the startup method at initialization time. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 13 | ▶ | sub startup { | ||||||||||||||||||||||||||
|
Subroutine
startup: called CC=6 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 13 | my $app = shift; | |||||||||||||||||||||||||||
| 134 | # having a non-C locale for number will wreck all sorts of havoc | ||||||||||||||||||||||||||||
| 135 | # when things get converted to string and back | ||||||||||||||||||||||||||||
| 136 | 13 | setlocale(LC_NUMERIC, "C"); | |||||||||||||||||||||||||||
| 137 | 13 | setlocale(LC_TIME, "C"); | |||||||||||||||||||||||||||
| 138 | 13 | weaken($app); | |||||||||||||||||||||||||||
| 139 | 13 | $app->config->postProcessCfg(); | |||||||||||||||||||||||||||
| 140 | 13 | my $gcfg = $app->config->cfgHash->{BACKEND}; | |||||||||||||||||||||||||||
| 141 | 13 | ▶ | if ($gcfg->{log_file}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if (open my $file, '>>', $gcfg->{log_file}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $app->log->handle($file); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||
| 146 | 0 | $app->log->debug("Opening $gcfg->{log_file}: $!"); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ## commands | ||||||||||||||||||||||||||||
| 151 | 13 | unshift @{$app->commands->namespaces}, __PACKAGE__.'::Command'; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 13 | ▶ | unshift @{$app->static->paths}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | $app->home->rel_file('frontend').'/compiled/source/' | ||||||||||||||||||||||||||||
| 155 | if $app->mode eq 'development'; # Router | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # properly figure your own path when running under fastcgi | ||||||||||||||||||||||||||||
| 158 | 23 | ▶ | $app->hook( before_dispatch => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 159 | 23 | my $c = shift; | |||||||||||||||||||||||||||
| 160 | 23 | my $reqEnv = $c->req->env; | |||||||||||||||||||||||||||
| 161 | 23 | ▶ | my $uri = $reqEnv->{SCRIPT_URI} || $reqEnv->{REQUEST_URI}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$reqEnv->{'SCRIPT_URI'} || $reqEnv->{'REQUEST_URI'}
MC/DC50%1/2
$reqEnv->{'SCRIPT_URI'} || $reqEnv->{'REQUEST_URI'}
$reqEnv->{'SCRIPT_URI'}
$reqEnv->{'REQUEST_URI'}
Truth table66%2/3
A || B
A= $reqEnv->{'SCRIPT_URI'}B= $reqEnv->{'REQUEST_URI'}
| |||||||||||||||||||||||||||||
| 162 | 23 | my $path_info = $reqEnv->{PATH_INFO}; | |||||||||||||||||||||||||||
| 163 | 23 | ▶ | $uri =~ s|/?\Q${path_info}\E$|/| if $path_info and $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path_info and $uri
MC/DC50%1/2
$path_info and $uri
$path_info
$uri
Truth table66%2/3
A and B
A= $path_infoB= $uri
| |||||||||||||||||||||||||||||
| 164 | 23 | ▶ | $c->req->url->base(Mojo::URL->new($uri)) if $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 13 | }); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 23 | ▶ | $app->hook( after_dispatch => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 168 | 23 | my $c = shift; | |||||||||||||||||||||||||||
| 169 | # not telling anyone that we are mojo | ||||||||||||||||||||||||||||
| 170 | 23 | $c->res->headers->remove('Server'); | |||||||||||||||||||||||||||
| 171 | 23 | ▶ | my $securityHeaders = $c->can('securityHeaders') ? $c->securityHeaders : $app->securityHeaders; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 23 | for my $header ( keys %$securityHeaders){ | |||||||||||||||||||||||||||
| 173 | 92 | $c->res->headers->header($header,$securityHeaders->{$header}); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 23 | ▶ | $c->res->headers->cache_control('no-cache, no-store, must-revalidate') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | unless $c->req->url->path =~ m{/resource/.+}; | ||||||||||||||||||||||||||||
| 177 | 13 | }); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 13 | ▶ | if (my $secrets = $app->config->secretFile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 13 | ▶ | if (-r $secrets) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 13 | $app->secrets([ path($app->config->secretFile)->slurp ]); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||
| 184 | 0 | $app->log->error("Cannot read secrets file $secrets. Please check permissions."); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 13 | my $routes = $app->routes; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | $app->plugin('CallBackery::Plugin::Doc', { | ||||||||||||||||||||||||||||
| 191 | root => '/doc', | ||||||||||||||||||||||||||||
| 192 | index => $app->docIndex, | ||||||||||||||||||||||||||||
| 193 | template => Mojo::Asset::File->new( | ||||||||||||||||||||||||||||
| 194 | 13 | path=>dirname($INC{'CallBackery/Config.pm'}).'/templates/doc.html.ep', | |||||||||||||||||||||||||||
| 195 | )->slurp, | ||||||||||||||||||||||||||||
| 196 | }); | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 13 | $routes->any('/upload')->to(namespace => $app->rpcServiceNamespace, controller=>$app->rpcServiceController, action => 'handleUpload'); | |||||||||||||||||||||||||||
| 199 | 13 | $routes->any('/download')->to(namespace => $app->rpcServiceNamespace, controller=>$app->rpcServiceController, action => 'handleDownload'); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # this is a dummy login screen, we use inside an iframe to trick the browser | ||||||||||||||||||||||||||||
| 202 | # into storing our password for auto-fill. Since there is no standard for triggering the | ||||||||||||||||||||||||||||
| 203 | # behavior, this is all a bit voodoo, sorry. -- tobi | ||||||||||||||||||||||||||||
| 204 | 0 | ▶ | $routes->get('/login')->to(cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 205 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 206 | 0 | $c->render(data=><<HTML, format=>'html'); | |||||||||||||||||||||||||||
| 207 | <!DOCTYPE html><html><head></head><body><form id="cbLoginForm" name="cbLoginForm" autocomplete="on" method="POST" > | ||||||||||||||||||||||||||||
| 208 | <input type="text" id="cbUsername" name="cbUsername" autocomplete="on" /> | ||||||||||||||||||||||||||||
| 209 | <input type="password" id="cbPassword" name="cbPassword" autocomplete="on" /> | ||||||||||||||||||||||||||||
| 210 | </form></body></html> | ||||||||||||||||||||||||||||
| 211 | HTML | ||||||||||||||||||||||||||||
| 212 | 13 | }); | |||||||||||||||||||||||||||
| 213 | # second stage of the deception. the answer page for login must not be the same as the original page | ||||||||||||||||||||||||||||
| 214 | # otherwise the browser assumes the login failed and does not offer to save the password. | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | $routes->post('/login')->to(cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 216 | 0 | shift->render(data=><<HTML, format=>'html'); | |||||||||||||||||||||||||||
| 217 | <!DOCTYPE html><html><head></head><body></body></html> | ||||||||||||||||||||||||||||
| 218 | HTML | ||||||||||||||||||||||||||||
| 219 | 13 | }); | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 13 | $app->plugin('qooxdoo',{ | |||||||||||||||||||||||||||
| 223 | path => '/QX-JSON-RPC', | ||||||||||||||||||||||||||||
| 224 | namespace => $app->rpcServiceNamespace, | ||||||||||||||||||||||||||||
| 225 | controller => $app->rpcServiceController, | ||||||||||||||||||||||||||||
| 226 | }); | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 13 | return 0; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 12 | 1; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||