Skip to content

Commit

Permalink
Clarified defined logic, param checking, and strings
Browse files Browse the repository at this point in the history
This commit clarifies the defined logic within the codebase to use
defined where needed, and remove where not correct.  Additionally
added more specific param checking on methods to ensure we're not
validating params we're not expecting.  Also updated to single
quotes where interpolation isn't required.
  • Loading branch information
renderorange committed Jul 16, 2024
1 parent 51b6ba3 commit 27ec14b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/Pasteburn/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ C<Pasteburn::Config> loads the project config.
Load the config and return a C<Pasteburn::Config> object.
Required keys and values are validated during load, and exception thrown if not defined or containing the default values.
Required keys and values are validated during load, and exception thrown if key does not exist and doesn't match the expected values.
=back
Expand Down
2 changes: 1 addition & 1 deletion lib/Pasteburn/Crypt/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ sub validate {
@_,
};

if ( !$arg->{hash} ) {
if ( !defined $arg->{hash} ) {
die "hash is required\n";
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Pasteburn/Crypt/Storage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sub encode {
@_,
};

if ( !$arg->{secret} ) {
if ( !defined $arg->{secret} ) {
die "secret argument is required\n";
}

Expand All @@ -44,7 +44,7 @@ sub decode {
@_,
};

if ( !$arg->{secret} ) {
if ( !defined $arg->{secret} ) {
die "secret argument is required\n";
}

Expand Down
26 changes: 13 additions & 13 deletions lib/Pasteburn/Model/Secrets.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ sub get {

my ( @where, @bind_values );

foreach my $key ( keys %{$arg} ) {
foreach my $key (qw{id}) {
if ( !defined $arg->{$key} ) {
next;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ sub store {
@_,
};

foreach my $attribute ( 'passphrase', 'secret' ) {
foreach my $attribute (qw{passphrase secret}) {
if ( !defined $self->{$attribute} ) {
die "$attribute is required";
}
Expand Down Expand Up @@ -170,7 +170,7 @@ sub _generate_id {
my $self = shift;

if ( !Scalar::Util::blessed($self) ) {
die "_generate_id must be called as an object method";
die '_generate_id must be called as an object method';
}

return Digest::SHA::sha256_hex( Crypt::Random::makerandom( Size => 512, Strength => 0 ) );
Expand All @@ -180,7 +180,7 @@ sub _update_object {
my $self = shift;

if ( !Scalar::Util::blessed($self) ) {
die "_update_object must be called as an object method";
die '_update_object must be called as an object method';
}

# always update the object with the data from the database.
Expand Down Expand Up @@ -208,15 +208,15 @@ sub validate_passphrase {
};

if ( !Scalar::Util::blessed($self) ) {
die "validate_passphrase must be called as an object method";
die 'validate_passphrase must be called as an object method';
}

if ( !$self->id ) {
die "validate_passphrase cannot be run for a nonexistent secret";
die 'validate_passphrase cannot be run for a nonexistent secret';
}

if ( !defined $arg->{passphrase} ) {
die "passphrase is required";
die 'passphrase is required';
}

# if the secret is stored with an empty string as passphrase, there is still
Expand All @@ -225,7 +225,7 @@ sub validate_passphrase {
# but not allow an undef to be stored.
# although unlikely to fail, still verify the hashed passphrase is in the object.
if ( !defined $self->passphrase ) {
die "passphrase is not set";
die 'passphrase is not set';
}

my $crypt = Pasteburn::Crypt::Hash->new();
Expand All @@ -241,15 +241,15 @@ sub decode_secret {
};

if ( !Scalar::Util::blessed($self) ) {
die "decode_secret must be called as an object method";
die 'decode_secret must be called as an object method';
}

if ( !$self->id ) {
die "decode_secret cannot be run for a nonexistent secret";
die 'decode_secret cannot be run for a nonexistent secret';
}

if ( !defined $arg->{passphrase} ) {
die "passphrase is required";
die 'passphrase is required';
}

my $crypt_storage = Pasteburn::Crypt::Storage->new( passphrase => $arg->{passphrase} );
Expand All @@ -268,11 +268,11 @@ sub delete_secret {
my $self = shift;

if ( !Scalar::Util::blessed($self) ) {
die "delete_secret must be called as an object method";
die 'delete_secret must be called as an object method';
}

if ( !$self->id ) {
die "delete_secret cannot be run for a nonexistent secret";
die 'delete_secret cannot be run for a nonexistent secret';
}

my $sql = q{
Expand Down
4 changes: 2 additions & 2 deletions t/lib/Pasteburn/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ sub create_test_app {
@_,
);

foreach my $required ( keys %args ) {
foreach my $required ( qw{config} ) {
if ( !defined $args{$required} ) {
die "$required is required";
}
}

if ( !ref $args{config} eq 'HASH' ) {
die "config must be a hashref";
die 'config must be a hashref';
}

override(
Expand Down

0 comments on commit 27ec14b

Please sign in to comment.