$delay = 0 if $delay < 0;
}
- my $csrfgen_func = $self->can('generate_csrf_prevention_token');
- my ($raw, $ct, $nocomp) = &$formatter($res, $res->{data}, $params, $path,
- $auth, $csrfgen_func, $self->{title});
+ my ($raw, $ct, $nocomp) = $formatter->($res, $res->{data}, $params, $path,
+ $auth, $self->{formatter_config});
my $resp;
if (ref($raw) && (ref($raw) eq 'HTTP::Response')) {
# always delay unauthorized calls by 3 seconds
my $delay = 3;
if (my $formatter = PVE::APIServer::Formatter::get_login_formatter($format)) {
- my ($raw, $ct, $nocomp) = &$formatter($path, $auth);
+ my ($raw, $ct, $nocomp) =
+ $formatter->($path, $auth, $self->{formatter_config});
my $resp;
if (ref($raw) && (ref($raw) eq 'HTTP::Response')) {
$resp = $raw;
$self->{dirs} //= {};
$self->{title} //= 'API Inspector';
+ # formatter_config: we pass some configuration values to the Formatter
+ $self->{formatter_config} = {};
+ foreach my $p (qw(cookie_name base_uri title)) {
+ $self->{formatter_config}->{$p} = $self->{$p};
+ }
+ $self->{formatter_config}->{csrfgen_func} =
+ $self->can('generate_csrf_prevention_token');
+
+ # add default dirs which includes jquery and bootstrap
my $base = '/usr/share/libpve-http-server-perl';
add_dirs($self->{dirs}, '/css/' => "$base/css/");
add_dirs($self->{dirs}, '/js/' => "$base/js/");
# userid => $username,
# age => $age,
# isUpload => $isUpload,
- # cookie_name => $self->{cookie_name},
#};
}
my $portal_format = 'html';
my $portal_ct = 'text/html;charset=UTF-8';
-my $baseurl = "/api2/$portal_format";
-my $login_url = "$baseurl/access/ticket";
+my $get_portal_base_url = sub {
+ my ($config) = @_;
+ return "$config->{base_uri}/$portal_format";
+};
+
+my $get_portal_login_url = sub {
+ my ($config) = @_;
+ return "$config->{base_uri}/$portal_format/access/ticket";
+};
sub render_page {
- my ($doc, $html, $title) = @_;
+ my ($doc, $html, $config) = @_;
my $items = [];
tag => 'li',
cn => {
tag => 'a',
- href => $login_url,
+ href => $get_portal_login_url->($config),
onClick => "PVE.delete_auth_cookie();",
text => "Logout",
}};
+ my $base_url = $get_portal_base_url->($config);
my $nav = $doc->el(
class => "navbar navbar-inverse navbar-fixed-top",
{
tag => 'a',
class => "navbar-brand",
- href => $baseurl,
- text => $title,
+ href => $base_url,
+ text => $config->{title},
},
],
},
shift @pcomp; # api2
shift @pcomp; # $format
- my $href = $baseurl;
+ my $href = $base_url;
push @$items, { tag => 'li', cn => {
tag => 'a',
href => $href,
}
my $login_form = sub {
- my ($doc, $param, $errmsg) = @_;
+ my ($config, $doc, $param, $errmsg) = @_;
$param = {} if !$param;
tag => 'form',
role => 'form',
method => 'POST',
- action => $login_url,
+ action => $get_portal_login_url->($config),
cn => [
{
class => 'form-group',
};
PVE::APIServer::Formatter::register_login_formatter($portal_format, sub {
- my ($path, $auth) = @_;
+ my ($path, $auth, $config) = @_;
- my $headers = HTTP::Headers->new(Location => $login_url);
+ my $headers = HTTP::Headers->new(Location => $get_portal_login_url->($config));
return HTTP::Response->new(301, "Moved", $headers);
});
PVE::APIServer::Formatter::register_formatter($portal_format, sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func, $title) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
# fixme: clumsy!
PVE::APIServer::Formatter::Standard::prepare_response_data($portal_format, $res);
$data = $res->{data};
my $html = '';
- my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path, $auth, $csrfgen_func, $title);
+ my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path, $auth, $config);
if (!HTTP::Status::is_success($res->{status})) {
$html .= $doc->alert(text => "Error $res->{status}: $res->{message}");
$html = $doc->el(class => 'container', html => $html);
- my $raw = render_page($doc, $html, $title);
+ my $raw = render_page($doc, $html, $config);
return ($raw, $portal_ct);
});
method => 'GET',
path => "/access/ticket",
code => sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func, $title) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
- my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path, $auth, $csrfgen_func, $title);
+ my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path, $auth, $config);
- my $html = &$login_form($doc);
+ my $html = $login_form->($config, $doc);
- my $raw = render_page($doc, $html, $title);
+ my $raw = render_page($doc, $html, $config);
return ($raw, $portal_ct);
});
method => 'POST',
path => "/access/ticket",
code => sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func, $title) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
if (HTTP::Status::is_success($res->{status})) {
my $cookie = PVE::APIServer::Formatter::create_auth_cookie(
- $data->{ticket}, $auth->{cookie_name});
+ $data->{ticket}, $config->{cookie_name});
- my $headers = HTTP::Headers->new(Location => $baseurl,
+ my $headers = HTTP::Headers->new(Location => $get_portal_base_url->($config),
'Set-Cookie' => $cookie);
return HTTP::Response->new(301, "Moved", $headers);
}
# Note: HTTP server redirects to 'GET /access/ticket', so below
# output is not really visible.
- my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path, $auth, $csrfgen_func, $title);
+ my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path, $auth, $config);
- my $html = &$login_form($doc);
+ my $html = $login_form->($config, $doc);
- my $raw = render_page($doc, $html, $title);
+ my $raw = render_page($doc, $html, $config);
return ($raw, $portal_ct);
});
}
PVE::APIServer::Formatter::register_formatter('json', sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
my $nocomp = 0;
PVE::APIServer::Formatter::register_formatter('extjs', sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
my $nocomp = 0;
});
PVE::APIServer::Formatter::register_formatter('htmljs', sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
my $nocomp = 0;
PVE::APIServer::Formatter::register_formatter('spiceconfig', sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
my $nocomp = 0;
});
PVE::APIServer::Formatter::register_formatter('png', sub {
- my ($res, $data, $param, $path, $auth, $csrfgen_func) = @_;
+ my ($res, $data, $param, $path, $auth, $config) = @_;
my $nocomp = 1;