lib/Myriad/Util/Secret.pm

filter: Statementstmt 100% 45 / 45 Branchbran 75.0% 9 / 12 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 12 / 12 Podpod 75.0% 3 / 4 totaltotal 90.1% 73 / 81 CC 10 SCAR 13.6
CC
10
Cov
92%
equal
8.1
DESTROY
2.0
new
2.0
CRAP 3.9
« lib/Myriad/Exception.pm ↑ Summary lib/Myriad/Service/Storage.pm »
1 package Myriad::Util::Secret;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '1.001'; # VERSION
7 7 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
8  
9 =head1 Name
10  
11 Myriad::Util::Secret - protect secrets from getting exposed accidentally
12  
13 =head1 SYNOPSIS
14  
15 my $secret = Myriad::Util::Secret->new('shh.. secret!');
16  
17 =head1 DESCRIPTION
18  
19 When stringified, this will return C<***> instead of the real data.
20  
21 =cut
22  
23 use overload
24 78 ▶ q{""} => sub { "***" },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
25 7 eq => 'equal',
26 ne => 'not_equal',
27 7 fallback => 1;
28  
29 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Actual secret values are stored here, so that Dumper() doesn't expose them
32 7 my %secrets;
33  
34 17 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
35 17 my ($class, $value) = @_;
36 17 ▶ die 'need secret value' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 17 17
37 17 my $self = bless \(my $placeholder), $class;
38 17 $secrets{$self} = $value;
39 17 return $self;
40 }
41  
42 =head2 not_equal
43  
44 Returns true if the secret value does not match the provided value.
45  
46 =cut
47  
48 1 ▶sub not_equal {
Subroutine not_equal: called CC=1 SCAR=0.0
Pod documented
49 1 my ($self, $other) = @_;
50 1 return !$self->equal($other);
51 }
52  
53 =head2 equal
54  
55 Returns true if the secret value matches the provided value.
56  
57 =cut
58  
59 34 ▶sub equal {
Subroutine equal: called CC=8 SCAR=20.9
Pod documented
60 34 my ($self, $other) = @_;
61 34 ▶ return 0 unless defined $other;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 34 34
62  
63 34 ▶ if(blessed($other) and $other->isa('Myriad::Util::Secret')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($other) and $other->isa("Myriad::Util::Secret")) 8 26 34
Condition66%2/3
builtin::blessed($other) and $other->isa("Myriad::Util::Secret")
!ll&&!rl&&r
2608
MC/DC50%1/2
builtin::blessed($other) and $other->isa("Myriad::Util::Secret")
builtin::blessed($other) $other->isa("Myriad::Util::Secret")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("Myriad::Util::Secret")
ABresult
0X0
100
111
64 8 return $other->equal($self->secret_value);
65 }
66  
67 26 ▶ my $comparison = $secrets{$self} // '';
Condition50%1/2
$secrets{$self} // ""
l!l
260
MC/DC0%0/1
$secrets{$self} // ""
$secrets{$self}
Truth table50%1/2
A
A= $secrets{$self}
Aresult
11
00
68  
69 # Simple stepwise logic here - we start by assuming that the values _do_ match
70 26 my $match = 1;
71  
72 # ... then we loop through the characters of the provided string, and we
73 # mark as not matching if any of those characters don't match
74 7 ▶ no warnings 'substr'; # substr out of string returns undef...
Subroutine BEGIN: called CC=1 SCAR=0.0
75 7 ▶ no warnings 'uninitialized'; # ... so we expect and don't care for mismatched lengths
Subroutine BEGIN: called CC=1 SCAR=0.0
76 26 for my $idx (0..length($other)) {
77 3360 ▶ $match = 0 if substr($other, $idx, 1) ne substr($comparison, $idx, 1);
Branch: 1 branch
BranchTrueFalseTotal
if substr($other, $idx, 1) ne substr($comparison, $idx, 1) 4 3356 3360
78 }
79 # At this point, $match is true if the characters in the provided string match
80 # the equivalent characters in the real string - but that's not good enough,
81 # we need to confirm that _all_ characters were compared
82 26 ▶ $match = 0 unless length($comparison) == length($other);
Branch: 1 branch
BranchTrueFalseTotal
unless length $comparison == length $other 1 25 26
83  
84 26 return $match;
85 }
86  
87 =head2 secret_value
88  
89 Returns the original secret value as text.
90  
91 =cut
92  
93 17 ▶sub secret_value {
Subroutine secret_value: called CC=1 SCAR=0.0
Pod documented
94 17 my ($self) = @_;
95 17 return '' . $secrets{$self}
96 }
97  
98 17 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.1
99 17 my ($self) = @_;
100 17 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 17 17
101 17 delete $secrets{$self}
102 }
103  
104 7 1;
105  
106 __END__
« lib/Myriad/Exception.pm ↑ Summary lib/Myriad/Service/Storage.pm »