package app;
use Dancer ':syntax';
use Dancer::Session::Memcached;
use Authen::Simple::Passwd;
our $VERSION = '0.2';
hook 'before' => sub {
if (request->path_info !~ m{^/app/log}) {
# match login and logout
if (session('user') && session('time')) {
my $time_now = time;
if ($time_now - session('time') < config->{'session_timeout'}) {
session 'time' => $time_now;
#context->session 'time' => $time_now;
}
else {
return redirect '/app/logout?msg=session_expired&path=' . request->path_info;
}
}
else {
return redirect '/app/login';
}
}
};
get '/app/login' => sub {
my $hostname = request->{'host'};
my $msg = 'please login';
if (exists params->{'msg'}) {
$msg = params->{'msg'};
$msg =~ s/_/ /g;
}
my $path = '/app/dashboard';
if (exists params->{'path'}) {
$path = params->{'path'};
}
template '
login.tt', { hostname => $hostname, title => 'login', path => $path, message => $msg };
post '/app/login' => sub {
# Validate web<nm> username and password against /etc/passwd
# TODO: use RESTful API to query
api.zootzone.com if (params->{'pass'} ne '') {
my $passwd = Authen::Simple::Passwd->new( path => config->{'passwords'} );
if ($passwd->authenticate(params->{'user'}, params->{'pass'})) {
session 'user' => params->{'user'};
session 'time' => time;
if (params->{'path'}) {
return redirect params->{'path'};
}
else {
return redirect '/app/dashboard';
}
}
else {
return redirect '/app/login?msg=authentication_failed';
}
} else {
return redirect '/app/login?msg=invalid_credentials';
}
};
other routes ....