From bd9ba3bcb8c16d28a69d06f24e83c12c9f2774db Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Fri, 22 Mar 2019 09:10:19 +0000 Subject: [PATCH] broken refactor but need to leave now... --- lib/App/Netdisco/Util/Web.pm | 13 ------------- lib/App/Netdisco/Web.pm | 15 ++++++++++++--- lib/App/Netdisco/Web/AuthN.pm | 9 ++++----- lib/App/Netdisco/Web/OpenAPI.pm | 9 --------- lib/App/Netdisco/Web/Plugin.pm | 3 +-- lib/App/Netdisco/Web/Plugin/Device/Details.pm | 3 +-- 6 files changed, 18 insertions(+), 34 deletions(-) diff --git a/lib/App/Netdisco/Util/Web.pm b/lib/App/Netdisco/Util/Web.pm index 35d9c1f8..fae7ab22 100644 --- a/lib/App/Netdisco/Util/Web.pm +++ b/lib/App/Netdisco/Util/Web.pm @@ -8,7 +8,6 @@ use Time::Seconds; use base 'Exporter'; our @EXPORT = (); our @EXPORT_OK = qw/ - request_is_api sort_port sort_modules interval_to_daterange sql_match @@ -28,18 +27,6 @@ subroutines. =head1 EXPORT_OK -=head2 request_is_api - -Whether the request should be interpreted as an API call. - -=cut - -sub request_is_api { - return (setting('api_token_lifetime') - and request->accept =~ m/(?:json|javascript)/ - and index(var('orig_path'), uri_for('/api')->path) == 0); -} - =head2 sql_match( $value, $exact? ) Convert wildcard characters "C<*>" and "C" to "C<%>" and "C<_>" diff --git a/lib/App/Netdisco/Web.pm b/lib/App/Netdisco/Web.pm index 00f3c83c..53715939 100644 --- a/lib/App/Netdisco/Web.pm +++ b/lib/App/Netdisco/Web.pm @@ -12,8 +12,7 @@ use HTML::Entities (); # to ensure dependency is met use URI::QueryParam (); # part of URI, to add helper methods use Path::Class 'dir'; use Module::Load (); -use App::Netdisco::Util::Web - qw/request_is_api interval_to_daterange/; +use App::Netdisco::Util::Web 'interval_to_daterange'; use App::Netdisco::Web::AuthN; use App::Netdisco::Web::OpenAPI; @@ -202,7 +201,7 @@ hook 'after' => sub { }; any qr{.*} => sub { - if (request_is_api()) { + if (request->is_api) { status(404); return to_json { error => 'not found' }; } @@ -222,6 +221,16 @@ any qr{.*} => sub { $response->status($status || 302); $response->headers('Location' => $destination); }; + + # helper for handlers of more than one method type + *Dancer::Request::is_api = sub { + my $self = shift; + vars->{'orig_path'} = request->path unless request->is_forward; + my $path = ($self->is_forward ? vars->{'orig_path'} : $self->path); + return (setting('api_token_lifetime') + and $self->accept =~ m/(?:json|javascript)/ + and index($path, uri_for('/api')->path) == 0); + }; } true; diff --git a/lib/App/Netdisco/Web/AuthN.pm b/lib/App/Netdisco/Web/AuthN.pm index eee631ff..70ab4601 100644 --- a/lib/App/Netdisco/Web/AuthN.pm +++ b/lib/App/Netdisco/Web/AuthN.pm @@ -5,7 +5,6 @@ use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Swagger; -use App::Netdisco::Util::Web 'request_is_api'; use MIME::Base64; hook 'before' => sub { @@ -49,7 +48,7 @@ hook 'before' => sub { session(logged_in_user => 'guest'); session(logged_in_user_realm => 'users'); } - elsif (request_is_api() + elsif (request->is_api and request->header('Authorization')) { my $token = request->header('Authorization'); @@ -68,7 +67,7 @@ hook 'before' => sub { # user redirected here (POST -> GET) when login fails get qr{^/(?:login(?:/denied)?)?} => sub { - if (request_is_api()) { + if (request->is_api) { status('unauthorized'); return to_json { error => 'not authorized', @@ -96,7 +95,7 @@ swagger_path { }, }, post qr{^/(?:api/)?login$} => sub { - my $mode = (request_is_api() ? 'API' : 'WebUI'); + my $mode = (request->is_api ? 'API' : 'WebUI'); my $x = params; use DDP; p $x; @@ -173,7 +172,7 @@ swagger_path { responses => { default => { examples => { 'application/json' => {} } } }, }, get qr{^/(?:api/)?logout$} => sub { - my $mode = (request_is_api() ? 'API' : 'WebUI'); + my $mode = (request->is_api ? 'API' : 'WebUI'); # clear out API token my $user = schema('netdisco')->resultset('User') diff --git a/lib/App/Netdisco/Web/OpenAPI.pm b/lib/App/Netdisco/Web/OpenAPI.pm index 490fca6f..f2b233c1 100644 --- a/lib/App/Netdisco/Web/OpenAPI.pm +++ b/lib/App/Netdisco/Web/OpenAPI.pm @@ -5,9 +5,6 @@ use Dancer::Plugin::Ajax; use Dancer::Plugin::Swagger; -use App::Netdisco::Util::Web - qw/request_is_api interval_to_daterange/; - # setup for swagger API my $swagger = Dancer::Plugin::Swagger->instance->doc; $swagger->{schemes} = ['http','https']; @@ -30,11 +27,6 @@ $swagger->{securityDefinitions} = { }; $swagger->{security} = [ { APIKeyHeader => [] } ]; -# support for checking if this is an api request even after forward -hook 'before' => sub { - vars->{'orig_path'} = request->path unless request->is_forward; -}; - # workaround for Swagger plugin weird response body hook 'after' => sub { my $r = shift; # a Dancer::Response @@ -50,7 +42,6 @@ any '/api/:type/:identifier/:method' => require_login sub { pass unless setting('api_enabled') ->{ params->{'type'} }->{ params->{'method'} }; - vars->{'is_api'} = 1; my $target = sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'}; forward $target, { tab => params->{'method'}, q => params->{'identifier'} }; diff --git a/lib/App/Netdisco/Web/Plugin.pm b/lib/App/Netdisco/Web/Plugin.pm index 889df8e1..885c0f1d 100644 --- a/lib/App/Netdisco/Web/Plugin.pm +++ b/lib/App/Netdisco/Web/Plugin.pm @@ -3,7 +3,6 @@ package App::Netdisco::Web::Plugin; use Dancer ':syntax'; use Dancer::Plugin; -use App::Netdisco::Util::Web 'request_is_api'; use Path::Class 'dir'; set( @@ -25,7 +24,7 @@ set( config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} ||= [ setting('views') ]; register 'bang' => sub { - if (request_is_api()) { + if (request->is_api) { content_type('application/json'); set serializer => 'JSON'; status $_[1]; diff --git a/lib/App/Netdisco/Web/Plugin/Device/Details.pm b/lib/App/Netdisco/Web/Plugin/Device/Details.pm index 8f36c831..a50e6803 100644 --- a/lib/App/Netdisco/Web/Plugin/Device/Details.pm +++ b/lib/App/Netdisco/Web/Plugin/Device/Details.pm @@ -22,7 +22,6 @@ swagger_path { } } }, }, get '/api/device/:identifier' => require_login sub { - vars->{'is_api'} = 1; forward '/ajax/content/device/details', { tab => 'details', q => params->{'identifier'} }; }; @@ -45,7 +44,7 @@ get '/ajax/content/device/details' => require_login sub { delete $results[0]->{'snmp_comm'}; $results[0]->{'power'} = \@power; - if (vars->{'is_api'}) { + if (request->is_api) { content_type('application/json'); to_json { device => $results[0] }; }