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