summaryrefslogtreecommitdiff
path: root/lib/ACME/AutoRedact.pm
blob: 69d80df4f4f5ddc9e02ea24d3fd8545ff2cf3551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ACME::AutoRedact; 
use strict;
use warnings;
# VERSION 
use Scope::Upper qw(localize :words);
use Scalar::Util qw(blessed);
 
# ABSTRACT: string-like object that can redact its value out of derived strings 
 
=head1 SYNOPSIS
 
  my $password_redacted = ACME::AutoRedact->new($password);
 
  my $complicated_string = "some text with $password_redacted";
 
  # this gets the password
  do_thing_with($complicated_string);
 
  # this gets asterisks
  { ACME::AutoRedact->redact; log($complicated_string) }
 
=cut
 
use overload
    q{""} => \&stringify,
    q{.} => \&concat,
    fallback => 1,
    ;
 
sub _build_redacted {
    my ($self) = @_;
    return q{*} x length($self->{value});
}
 
=attr C<value>
 
The "revealed" string value of this object.
 
=attr C<redacted>
 
The "redacted" string value of this object. Defaults to a sequence of
C<*> as long as the L</value>.
 
=attr C<default_behaviour>
 
Either C<redact> or C<reveal> (the default). What the object should do
when stringifying, absent any specific directive (see L<< /C<redact>
>> and L<< /C<reveal> >>).
 
=method C<new>
 
  ACME::AutoRedact->new($string);
  ACME::AutoRedact->new({ value => $string });
  ACME::AutoRedact->new({
    value => $string,
    redacted => $other_string,
    default_behaviour => 'redact',
  });
 
Constructs a new object; the L</redacted> version defaults to a
sequence of C<*> as long as the L</value>, the L<default
bahviour|/default_behaviour> is "reveal".
 
=cut
 
sub new {
    my ($class,@args) = @_;
 
    my $self;
    if (@args==1 and not ref $args[0]) {
        $self = { value => $args[0] };
    }
    elsif (@args==1) {
        $self = $args[0];
    }
    else {
        $self = { @args };
    }
    bless $self,$class;
    $self->{redacted//$self->_build_redacted;
    return $self;
}
 
our $requested_behaviour## no critic(ProhibitPackageVars) 
 
=method C<redact>
 
  ACME::AutoRedact->redact;
 
This sets (locally to the scope in which it's called) the
L</default_behaviour> to be "redact".
 
=cut
 
sub redact {
    localize *requested_behaviour, \'redact', UP;
}
 
=method C<reveal>
 
  ACME::AutoRedact->reveal;
 
This sets (locally to the scope in which it's called) the
L</default_behaviour> to be "reveal".
 
=cut
 
sub reveal {
    localize *requested_behaviour, \'reveal', UP;
}
 
=method C<stringify>
 
Overloaded stringification method. Returns either the normal
L</value>, or the L</redacted> one, depending on the current
behaviour.
 
=cut
 
sub stringify {
    my ($self) = @_;
 
    my $behaviour = $requested_behaviour // $self->{default_behaviour// 'reveal';
 
    if ($behaviour eq 'reveal') {
        return $self->{value};
    }
    else {
        return $self->{redacted};
    }
}
 
=method C<concat>
 
Overloaded concatenation method. Returns a new object with both normal
L</value> and L</redacted> value being the (appropriate) concatenation
of the arguments.
 
=cut
 
sub concat {
    my ($self,$other,$swap) = @_;
 
    my %new;
    if ((blessed($other)//''eq __PACKAGE__) {
        ($self,$other) = ($other,$selfif $swap;
        $new{value} = $self->{value} . $other->{value};
        $new{redacted} = $self->{redacted} . $other->{redacted};
 
        $new{default_behaviour} = $self->{default_behaviour};
    }
    else {
        for my $k (qw(value redacted)) {
            $new{$k} = $swap
                "$other" . $self->{$k}
                $self->{$k} . "$other";
        }
        $new{default_behaviour} = $self->{default_behaviour};
    }
 
    return ref($self)->new(\%new);
}
 
1;