« blib/lib/Mojolicious/Plugin/Fondation/User/Controller/User.pm
↑ Summary
blib/lib/Mojolicious/Plugin/Fondation/User/Schema/ResultSet/User.pm »
| 1 | package Mojolicious::Plugin::Fondation::User::Schema::Result::User; | ||||||||||||||||||||||||||||
| 2 | 1 | $Mojolicious::Plugin::Fondation::User::Schema::Result::User::VERSION = '0.04'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: DBIx::Class Result class for users table | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base 'Mojolicious::Plugin::Fondation::Schema::Result::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Crypt::Passphrase; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | __PACKAGE__->load_components(qw/TimeStamp Core/); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # ── Internal ────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Lazy-initialized passphrase instance (per-process singleton). | ||||||||||||||||||||||||||||
| 16 | 1 | my $_PASSPHRASE; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _passphrase { | ||||||||||||||||||||||||||
|
Subroutine
_passphrase: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return $_PASSPHRASE //= Crypt::Passphrase->new( | ||||||||||||||||||||||||||
|
Condition0%0/3
$_PASSPHRASE //= "Crypt::Passphrase"->new("encoder", {"module", "Argon2", "time", 3, "memory", 65536, "threads", 4})
MC/DC0%0/2
$_PASSPHRASE //= "Crypt::Passphrase"->new("encoder", {"module", "Argon2", "time", 3, "memory", 65536, "threads", 4})
$_PASSPHRASE
"Crypt::Passphrase"->new("encoder", {"module", "Argon2", "time", 3, "memory", 65536, "threads", 4})
Truth table0%0/3
A //= B
A= $_PASSPHRASEB= "Crypt::Passphrase"->new("encoder", {"module", "Argon2", "time", 3, "memory", 65536, "threads", 4})
| |||||||||||||||||||||||||||||
| 20 | encoder => { | ||||||||||||||||||||||||||||
| 21 | module => 'Argon2', | ||||||||||||||||||||||||||||
| 22 | time => 3, | ||||||||||||||||||||||||||||
| 23 | memory => 64 * 1024, | ||||||||||||||||||||||||||||
| 24 | threads => 4, | ||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub _hash_password { | ||||||||||||||||||||||||||
|
Subroutine
_hash_password: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 0 | my ($password) = @_; | |||||||||||||||||||||||||||
| 31 | 0 | return _passphrase()->hash_password($password); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | __PACKAGE__->table('users'); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | __PACKAGE__->add_columns( | |||||||||||||||||||||||||||
| 37 | id => { data_type => 'integer', is_auto_increment => 1, is_nullable => 0 }, | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | username => { | ||||||||||||||||||||||||||||
| 40 | data_type => 'varchar', | ||||||||||||||||||||||||||||
| 41 | size => 100, | ||||||||||||||||||||||||||||
| 42 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 43 | extra => { | ||||||||||||||||||||||||||||
| 44 | openapi => { | ||||||||||||||||||||||||||||
| 45 | minLength => 4, | ||||||||||||||||||||||||||||
| 46 | pattern => '^[a-zA-Z0-9_-]{4,}$', | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | email => { | ||||||||||||||||||||||||||||
| 52 | data_type => 'varchar', | ||||||||||||||||||||||||||||
| 53 | size => 255, | ||||||||||||||||||||||||||||
| 54 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 55 | extra => { openapi => { format => 'email' } }, | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | password => { | ||||||||||||||||||||||||||||
| 59 | data_type => 'varchar', | ||||||||||||||||||||||||||||
| 60 | size => 255, | ||||||||||||||||||||||||||||
| 61 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 62 | extra => { | ||||||||||||||||||||||||||||
| 63 | openapi => { | ||||||||||||||||||||||||||||
| 64 | writeOnly => 1, | ||||||||||||||||||||||||||||
| 65 | format => 'password', | ||||||||||||||||||||||||||||
| 66 | minLength => 8, | ||||||||||||||||||||||||||||
| 67 | create => { required => 1 }, | ||||||||||||||||||||||||||||
| 68 | update => { required => 0 }, | ||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | created_at => { data_type => 'datetime', is_nullable => 0, set_on_create => 1, set_on_update => 1 }, | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | updated_at => { data_type => 'datetime', is_nullable => 0, set_on_create => 1, set_on_update => 1 }, | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | active => { | ||||||||||||||||||||||||||||
| 78 | data_type => 'integer', | ||||||||||||||||||||||||||||
| 79 | default_value => 1, | ||||||||||||||||||||||||||||
| 80 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 81 | extra => { | ||||||||||||||||||||||||||||
| 82 | openapi => { | ||||||||||||||||||||||||||||
| 83 | enum => [0, 1], | ||||||||||||||||||||||||||||
| 84 | create => { required => 0 }, | ||||||||||||||||||||||||||||
| 85 | update => { required => 0 }, | ||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||
| 87 | }, | ||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | __PACKAGE__->set_primary_key('id'); | |||||||||||||||||||||||||||
| 92 | 1 | __PACKAGE__->resultset_class('Mojolicious::Plugin::Fondation::User::Schema::ResultSet::User'); | |||||||||||||||||||||||||||
| 93 | 1 | __PACKAGE__->add_unique_constraint([qw(username email)]); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Hash password automatically on create | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub insert { | ||||||||||||||||||||||||||
|
Subroutine
insert: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (defined $self->password) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $self->password(_hash_password($self->password)); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | $self->next::method(@_); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Hash password automatically on update (when password column is changed) | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | 0 | my $upd = {@_}; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (exists $upd->{password} && defined $upd->{password}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $upd->{"password"} and defined $upd->{"password"}
MC/DC0%0/2
exists $upd->{"password"} and defined $upd->{"password"}
exists $upd->{"password"}
defined $upd->{"password"}
Truth table0%0/3
A and B
A= exists $upd->{"password"}B= defined $upd->{"password"}
| |||||||||||||||||||||||||||||
| 109 | 0 | $upd->{password} = _hash_password($upd->{password}); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | $self->next::method($upd); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # ── Verify a plaintext password against the stored hash ─────────────── | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub check_password { | ||||||||||||||||||||||||||
|
Subroutine
check_password: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self, $password) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $hash = $self->password; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | return undef unless $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $pp = Crypt::Passphrase->new( | |||||||||||||||||||||||||||
| 124 | encoder => { | ||||||||||||||||||||||||||||
| 125 | module => 'Argon2', | ||||||||||||||||||||||||||||
| 126 | time => 3, | ||||||||||||||||||||||||||||
| 127 | memory => 64 * 1024, | ||||||||||||||||||||||||||||
| 128 | threads => 4, | ||||||||||||||||||||||||||||
| 129 | }, | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | return $pp->verify_password($password, $hash); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # ── TO_JSON — exclude password from serialization ── | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 139 | 0 | my $data = $self->SUPER::TO_JSON; | |||||||||||||||||||||||||||
| 140 | 0 | delete $data->{password}; | |||||||||||||||||||||||||||
| 141 | 0 | return $data; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||