| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 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 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Mojolicious::Plugin; | ||
| 13 | use Rex::JobControl::Helper::Project; | ||
| 14 | use Digest::Bcrypt; | ||
| 15 | |||
| 16 | use base 'Mojolicious::Plugin'; | ||
| 17 | |||
| 18 | sub register { | ||
| 19 | my ( $plugin, $app ) = @_; | ||
| 20 | |||
| 21 | $app->helper( | ||
| 22 | get_user => sub { | ||
| 23 | my ( $self, $uid ) = @_; | ||
| 24 | |||
| 25 | my @lines = | ||
| 26 | eval { local (@ARGV) = ( $self->app->config->{auth}->{passwd} ); <>; }; | ||
| 27 | chomp @lines; | ||
| 28 | |||
| 29 | for my $l (@lines) { | ||
| 30 | my ( $name, $pass ) = split( /:/, $l ); | ||
| 31 | if ( $name eq $uid ) { | ||
| 32 | return { name => $name, password => $pass }; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | return undef; | ||
| 37 | }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | $app->helper( | ||
| 41 | check_password => sub { | ||
| 42 | my ( $self, $uid, $pass ) = @_; | ||
| 43 | |||
| 44 | my $user = $app->get_user($uid); | ||
| 45 | |||
| 46 | my $salt = $app->config->{auth}->{salt}; | ||
| 47 | my $cost = $app->config->{auth}->{cost}; | ||
| 48 | |||
| 49 | my $bcrypt = Digest::Bcrypt->new; | ||
| 50 | $bcrypt->salt($salt); | ||
| 51 | $bcrypt->cost($cost); | ||
| 52 | $bcrypt->add($pass); | ||
| 53 | |||
| 54 | my $pw = $bcrypt->hexdigest; | ||
| 55 | |||
| 56 | if ( $user && $user->{password} eq $pw ) { | ||
| 57 | return $user->{name}; | ||
| 58 | } | ||
| 59 | |||
| 60 | return undef; | ||
| 61 | }, | ||
| 62 | ); | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; |