| 1 | # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # vim:ts=8:sw=2:et:sta:sts=2 | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ######### | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Author: rmp | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | package ClearPress::authenticator::db; | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use base qw(ClearPress::authenticator Class::Accessor); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = q[2026.07.25]; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | __PACKAGE__->mk_accessors(qw(dbh)); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | our $SUPPORTED_CIPHERS = { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | mysql => sub { my ($self, $str) = @_; $self->dyn_use('Crypt::MySQL'); return Crypt::MySQL::password($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | mysql41 => sub { my ($self, $str) = @_; $self->dyn_use('Crypt::MySQL'); return Crypt::MySQL::password41($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | sha1 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha1_hex($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sha128 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha128_hex($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sha256 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha256_hex($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sha384 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha384_hex($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sha512 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha512_hex($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | md5 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::MD5'); return Digest::MD5::md5_hex($str); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | Readonly::Scalar our $DEFAULT_TABLE => 'user'; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | Readonly::Scalar our $DEFAULT_USERNAME_FIELD => 'username'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | Readonly::Scalar our $DEFAULT_PASSWORD_FIELD => 'pass'; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | Readonly::Scalar our $DEFAULT_CIPHER => 'sha1'; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | sub table { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | my ($self, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | if($v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $self->{table} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | if($self->{table}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | return $self->{table}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | return $DEFAULT_TABLE; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | sub username_field { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
username_field: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | my ($self, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | if($v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $self->{username_field} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | if($self->{username_field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | return $self->{username_field}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | return $DEFAULT_USERNAME_FIELD; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | sub password_field { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
password_field: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | my ($self, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 3 | ▶ | if($v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $self->{password_field} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | if($self->{password_field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return $self->{password_field}; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | return $DEFAULT_PASSWORD_FIELD; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | sub cipher { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cipher: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | my ($self, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if($v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $self->{cipher} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | if($self->{cipher}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return $self->{cipher}; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | return $DEFAULT_CIPHER; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | ▶ | sub authen_credentials { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
authen_credentials: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | my ($self, $ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 6 | ▶ | if(!$ref || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$ref and $ref->{"username"} and $ref->{"password"}
$ref and $ref->{"username"}
MC/DC100%3/3
$ref and $ref->{"username"} and $ref->{"password"}
$ref
$ref->{"username"}
$ref->{"password"}
Truth table100%4/4
A and B and C
A= $refB= $ref->{"username"}C= $ref->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | !$ref->{username} || | ||||||||||||||||||||||||||||||||||||||||||
| 94 | !$ref->{password} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | my $dbh = $self->dbh(); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | my $table = $self->table; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | my $user_f = $self->username_field; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | my $pass_f = $self->password_field; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | my $c_type = $self->cipher; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | my $cipher = $SUPPORTED_CIPHERS->{$c_type}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | ▶ | if(!$cipher) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | croak qq[Unsupported cipher: $c_type]; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | my $digest = $cipher->($self, $ref->{password}); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | my $query = qq[SELECT $user_f FROM $table WHERE $user_f=? AND $pass_f=?]; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | my $results = $dbh->selectall_arrayref($query, {}, $ref->{username}, $digest); | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | if(!scalar @{$results}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||||||||||