File Coverage

blib/lib/Rex/JobControl/Mojolicious/Plugin/User.pm
Criterion Covered Total %
statement 21 43 48.8
branch 0 4 0.0
condition 0 3 0.0
subroutine 7 9 77.7
pod 1 1 100.0
total 29 60 48.3


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4             # vim: set ts=3 sw=3 tw=0:
5             # vim: set expandtab:
6              
7             package Rex::JobControl::Mojolicious::Plugin::User;
8             $Rex::JobControl::Mojolicious::Plugin::User::VERSION = '0.18.0';
9 1     1   737 use strict;
  1         2  
  1         29  
10 1     1   28 use warnings;
  1         1  
  1         26  
11              
12 1     1   4 use Mojolicious::Plugin;
  1         1  
  1         6  
13 1     1   21 use Rex::JobControl::Helper::Project;
  1         1  
  1         15  
14 1     1   3 use Digest::Bcrypt;
  1         1  
  1         23  
15              
16 1     1   3 use base 'Mojolicious::Plugin';
  1         1  
  1         313  
17              
18             sub register {
19 1     1 1 29 my ( $plugin, $app ) = @_;
20              
21             $app->helper(
22             get_user => sub {
23 0     0   0 my ( $self, $uid ) = @_;
24              
25             my @lines =
26 0         0 eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; };
  0         0  
  0         0  
27 0         0 chomp @lines;
28              
29 0         0 for my $l (@lines) {
30 0         0 my ( $name, $pass ) = split( /:/, $l );
31 0 0       0 if ( $name eq $uid ) {
32 0         0 return { name => $name, password => $pass };
33             }
34             }
35              
36 0         0 return undef;
37             },
38 1         6 );
39              
40             $app->helper(
41             check_password => sub {
42 0     0     my ( $self, $uid, $pass ) = @_;
43              
44 0           my $user = $app->get_user($uid);
45              
46 0           my $salt = $app->config->{auth}->{salt};
47 0           my $cost = $app->config->{auth}->{cost};
48              
49 0           my $bcrypt = Digest::Bcrypt->new;
50 0           $bcrypt->salt($salt);
51 0           $bcrypt->cost($cost);
52 0           $bcrypt->add($pass);
53              
54 0           my $pw = $bcrypt->hexdigest;
55              
56 0 0 0       if ( $user && $user->{password} eq $pw ) {
57 0           return $user->{name};
58             }
59              
60 0           return undef;
61             },
62 1         74 );
63             }
64              
65             1;