| 1 | package XTaTIK::Model::Users; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | our $VERSION = '0.005003'; # VERSION | |||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Mojo::Base -base; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Digest; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use MIME::Base64; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Data::Entropy::Algorithms qw/rand_bits/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | has [qw/pg/]; | |||||||||||
| 11 | |||||||||||||
| 12 | 1 | my @VALID_ROLES = sort qw/ | |||||||||||
| 13 | users | ||||||||||||
| 14 | products | ||||||||||||
| 15 | /; | ||||||||||||
| 16 | |||||||||||||
| 17 | 0 | ▶ | sub check { | ||||||||||
|
Subroutine
check: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 18 | 0 | my ( $self, $login, $pass ) = @_; | |||||||||||
| 19 | |||||||||||||
| 20 | 0 | my $user = $self->get( $login ); | |||||||||||
| 21 | |||||||||||||
| 22 | 0 | ▶ | unless ( $user ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | # we don't want a possible attacker to know whether or not they | ||||||||||||
| 24 | # got the login right, just by seeing the page returns fast | ||||||||||||
| 25 | 0 | __hash(rand); | |||||||||||
| 26 | 0 | return; | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | 0 | my ( $hash ) = __hash( $pass, $user->{salt} ); | |||||||||||
| 30 | 0 | ▶ | return $user if $hash eq $user->{pass}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | return; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 0 | ▶ | sub valid_roles { | ||||||||||
|
Subroutine
valid_roles: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 35 | 0 | return @VALID_ROLES; | |||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | sub add { | ||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 39 | 0 | my $self = shift; | |||||||||||
| 40 | 0 | my %values = __prepare_values( @_ ); | |||||||||||
| 41 | 0 | @values{qw/pass salt/} = __hash($values{pass}); | |||||||||||
| 42 | |||||||||||||
| 43 | $self->pg->db->query( | ||||||||||||
| 44 | 'INSERT INTO users (login, pass, salt, name, email, phone, roles) | ||||||||||||
| 45 | VALUES (?, ?, ?, ?, ?, ?, ?)', | ||||||||||||
| 46 | 0 | map $values{$_}, qw/login pass salt name email phone roles/, | |||||||||||
| 47 | ); | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | return 1; | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 0 | ▶ | sub update { | ||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||
| 54 | 0 | my $id = shift; | |||||||||||
| 55 | 0 | my %values = __prepare_values( @_ ); | |||||||||||
| 56 | |||||||||||||
| 57 | $self->pg->db->query( | ||||||||||||
| 58 | 'UPDATE users SET | ||||||||||||
| 59 | login = ?, name = ?, email = ?, phone = ?, roles = ? | ||||||||||||
| 60 | WHERE id = ?', | ||||||||||||
| 61 | 0 | @values{qw/login name email phone roles/}, | |||||||||||
| 62 | $id, | ||||||||||||
| 63 | ); | ||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 0 | ▶ | sub delete { | ||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 67 | 0 | my ( $self, $login ) = @_; | |||||||||||
| 68 | 0 | $self->pg->db->query( 'DELETE FROM users WHERE login = ?', $login ); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 0 | ▶ | sub get { | ||||||||||
|
Subroutine
get: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 72 | 0 | my ( $self, $login ) = @_; | |||||||||||
| 73 | |||||||||||||
| 74 | 0 | ▶ | my $user = $self->pg->db->query( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 'SELECT * FROM users WHERE login = ?', | ||||||||||||
| 76 | lc($login) =~ s/^\s+|\s+$//gr, | ||||||||||||
| 77 | )->hash or return; | ||||||||||||
| 78 | |||||||||||||
| 79 | 0 | $_ = +{ map +( $_ => 1 ), split /,/ } for $user->{roles}; | |||||||||||
| 80 | |||||||||||||
| 81 | 0 | return $user; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 0 | ▶ | sub get_all { | ||||||||||
|
Subroutine
get_all: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||
| 86 | 0 | return $self->pg->db->query( | |||||||||||
| 87 | 'SELECT * FROM users ORDER BY login', | ||||||||||||
| 88 | )->hashes; | ||||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | sub __hash { | ||||||||||
|
Subroutine
__hash: not called CC=2 SCAR=17.9
| |||||||||||||
| 92 | 0 | my ( $pass, $salt ) = @_; | |||||||||||
| 93 | 0 | ▶ | $salt = defined $salt ? decode_base64( $salt) : rand_bits 8*16; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 0 | my $hash = Digest->new("Bcrypt")->cost(15)->salt( $salt ) | |||||||||||
| 95 | ->add( $pass )->hexdigest; | ||||||||||||
| 96 | 0 | return ( $hash, encode_base64 $salt, '' ); | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 0 | ▶ | sub __prepare_values { | ||||||||||
|
Subroutine
__prepare_values: not called CC=3 SCAR=24.8
| |||||||||||||
| 100 | 0 | my %values = @_; | |||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | $_ //= '' for values %values; | ||||||||||
|
Condition0%0/2
$_ //= ""
MC/DC0%0/1
$_ //= ""
$_
Truth table0%0/2
A
A= $_
| |||||||||||||
| 103 | 0 | $values{roles} =~ s/^\s+|\s+$//g; | |||||||||||
| 104 | 0 | $values{roles} =~ s/\s*,\s*/,/g; | |||||||||||
| 105 | 0 | $values{login} = lc($values{login}) =~ s/^\s+|\s+$//gr; | |||||||||||
| 106 | |||||||||||||
| 107 | 0 | return %values; | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | 1 | 1; | |||||||||||
| 111 | |||||||||||||
| 112 | __END__ | ||||||||||||