--- /dev/null
+package Newslash::Plugin::BasicAuth;
+use Mojo::Base 'Mojolicious::Plugin';
+
+sub register {
+ my ($self, $app, $conf) = @_;
+
+ if ($app->config->{BasicAuth}) {
+ my $password = $app->config->{BasicAuth}->{password};
+ my $user = $app->config->{BasicAuth}->{username};
+ my $message = $app->config->{BasicAuth}->{message} || "access restricted";
+ return if (!$password || !$user);
+
+ $app->hook(before_dispatch => sub {
+ my $c = shift;
+ my $auth = $c->req->url->to_abs->userinfo;
+ if ($auth) {
+ my ($u, $p) = split(/:/, $auth, 2);
+ if ($u eq $user && $p eq $password) {
+ return;
+ }
+ }
+ $c->res->headers->www_authenticate("Basic realm=\"$message\"");
+ $c->res->code(401);
+ $c->res->body('Authorization required');
+ return $c->rendered;
+ });
+ }
+}
+
+1;
# stash for plugins
$app->config->{_Plugins} = {};
+ # use BasicAuth?
+ if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
+ $app->plugin('Newslash::Plugin::BasicAuth');
+ }
+
# use TimeLimitedCache
my $cache_opts = $app->config->{Cache} || {};
$app->plugin('Newslash::Plugin::TimeLimitedCache');