Arepa/Web/Auth.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 164 CC 13 SCAR 35.9
CC
13
Cov
0%
_get_session
90.0
_check_credentials
12.0
login
12.0
CRAP 36.1
1 package Arepa::Web::Auth;
2  
3 use strict;
4 use warnings;
5  
6 use English qw(-no_match_vars);
7 use base 'Arepa::Web::Base';
8 use DBI;
9 use Digest::MD5;
10 use YAML;
11 use MojoX::Session;
12  
13 # Let session cookies live one week
14 use constant TTL_SESSION_COOKIE => 60 * 60 * 24 * 7;
15  
16 sub _check_credentials {
17 my ($self, $username, $password, $auth_type) = @_;
18  
19 if (defined $auth_type && $auth_type eq 'file_md5') {
20 my $user_file_path =
21 $self->config->get_key('web_ui:authentication:user_file');
22 my %users = %{YAML::LoadFile($user_file_path)};
23 return ($users{users}->{$username} eq Digest::MD5::md5_hex($password));
24 }
25 elsif (!defined $auth_type) {
26 my %users = %{YAML::LoadFile($self->config->
27 get_key('web_ui:user_file'))};
28 return ($users{$username} eq Digest::MD5::md5_hex($password));
29 }
30 else {
31 die "Broken configuration: unknown auth type '$auth_type'\n";
32 }
33 }
34  
35 sub _get_session {
36 my ($self) = @_;
37  
38 my $session_db = $self->config->get_key('web_ui:session_db');
39 my $dbh = DBI->connect("dbi:SQLite:dbname=$session_db");
40 my $session = MojoX::Session->new(tx => $self->tx,
41 store => [dbi => {dbh => $dbh}],
42 expires_delta => TTL_SESSION_COOKIE);
43  
44  
45 # External authentication
46 my $auth_type_key = 'web_ui:authentication:type';
47 my $auth_type;
48 if ($self->config->key_exists($auth_type_key)) {
49 $auth_type = $self->config->get_key($auth_type_key);
50 }
51  
52 if (defined $auth_type && $auth_type eq 'external') {
53 if ($ENV{REMOTE_USER}) {
54 $session->load;
55 if (! $session->sid || $session->is_expired) {
56 $session->create;
57 $session->data(username => $ENV{REMOTE_USER});
58 $session->flush;
59 }
60 }
61 else {
62 $self->vars("error" => "Authentication error: your webserver is " .
63 "not passing the REMOTE_USER " .
64 "environment variable to the " .
65 "application");
66 }
67 }
68 else {
69 if (defined $self->param('username') &&
70 defined $self->param('password')) {
71 my $valid_creds;
72 eval {
73 $valid_creds =
74 $self->_check_credentials($self->param('username'),
75 $self->param('password'),
76 $auth_type);
77 };
78 if ($EVAL_ERROR) {
79 $self->vars("error" => $EVAL_ERROR);
80 }
81 else {
82 if ($valid_creds) {
83 $session->create;
84 $session->data(username => $self->param('username'));
85 $session->flush;
86 }
87 else {
88 $self->vars("error" => "Invalid username or password");
89 }
90 }
91 } else {
92 $session->load;
93 }
94 }
95  
96 # Figure out if the logged in user is an admin
97 if (defined $auth_type) {
98 my $user_file_path =
99 $self->config->get_key('web_ui:authentication:user_file');
100 my %users = %{YAML::LoadFile($user_file_path)};
101 my $is_admin = scalar(grep { $_ eq $session->data('username'); }
102 @{$users{admins}});
103 $session->data(is_user_admin => $is_admin);
104 }
105 else {
106 $session->data(is_user_admin => 1);
107 }
108  
109 $self->stash(username => $session->data('username'),
110 is_user_admin => $session->data('is_user_admin'));
111 return $session;
112 }
113  
114 sub login {
115 my $self = shift;
116  
117 my $session = $self->_get_session;
118  
119 if ($session->sid && ! $session->is_expired) {
120 return 1;
121 }
122 else {
123 # Don't check anything for public URLs
124 my $url_parts = $self->tx->req->url->path->parts;
125 if (scalar @$url_parts && $url_parts->[0] eq 'public') {
126 return 1;
127 }
128 }
129  
130 $self->vars();
131 $self->render('auth/login', layout => 'default');
132 return 0;
133 }
134  
135 sub logout {
136 my $self = shift;
137  
138 my $session_db = $self->config->get_key('web_ui:session_db');
139 my $dbh = DBI->connect("dbi:SQLite:dbname=$session_db");
140 my $session = MojoX::Session->new(tx => $self->tx,
141 store => [dbi => {dbh => $dbh}]);
142 $session->expire;
143 $session->flush;
144  
145 $self->redirect_to('home');
146 }
147  
148 1;