PWSafe3/Record.pm

filter: Statementstmt 13.7% 18 / 131 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 15.3% 4 / 26 Podpod 0.0% 0 / 5 totaltotal 9.8% 22 / 224 CC 27 SCAR 25.0
CC
27
Cov
10%
modifyfield
56.0
new
6.0
addfield
6.0
CRAP 12.2
« PWSafe3/PasswordPolicy.pm ↑ Summary PWSafe3/SHA256.pm »
1 #
2 # Copyright (c) 2011-2015 T.v.Dein <tlinden |AT| cpan.org>.
3 #
4 # Licensed under the terms of the Artistic License 2.0
5 # see: http://www.perlfoundation.org/artistic_license_2_0
6 #
7 package Crypt::PWSafe3::Record;
8  
9 1 ▶use Carp::Heavy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars qw(@ISA @EXPORT %map2name %map2type);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my %map2type = %Crypt::PWSafe3::Field::map2type;
15  
16 1 my %map2name = %Crypt::PWSafe3::Field::map2name;
17  
18 1 $Crypt::PWSafe3::Record::VERSION = '1.10';
19  
20 1 foreach my $field (keys %map2type ) {
21 0 ▶ eval qq(
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Branch: 17 branches
BranchTrueFalseTotal
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
22 *Crypt::PWSafe3::Record::$field = sub {
23 my(\$this, \$arg) = \@_;
24 if (\$arg) {
25 return \$this->modifyfield("$field", \$arg);
26 }
27 else {
28 return \$this->{field}->{$field}->{value};
29 }
30 }
31 );
32 }
33  
34 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
35 #
36 # new record object
37 0 my($this, %param) = @_;
38 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
39 0 my $self = \%param;
40 0 bless($self, $class);
41 0 $self->{field} = ();
42  
43 # just in case this is a record to be filled by the user,
44 # initialize it properly
45 0 my $newuuid = $self->genuuid();
46  
47 0 my $time = time;
48  
49 0 $self->addfield(Crypt::PWSafe3::Field->new(
50 name => 'uuid',
51 raw => $newuuid,
52 ));
53  
54 0 $self->addfield(Crypt::PWSafe3::Field->new(
55 name => 'ctime',
56 value => $time,
57 ));
58  
59 0 $self->addfield(Crypt::PWSafe3::Field->new(
60 name => 'mtime',
61 value => $time
62 ));
63  
64 0 $self->addfield(Crypt::PWSafe3::Field->new(
65 name => 'lastmod',
66 value => $time
67 ));
68  
69 0 $self->addfield(Crypt::PWSafe3::Field->new(
70 name => 'passwd',
71 value => ''
72 ));
73  
74 0 $self->addfield(Crypt::PWSafe3::Field->new(
75 name => 'user',
76 value => ''
77 ));
78  
79 0 $self->addfield(Crypt::PWSafe3::Field->new(
80 name => 'title',
81 value => ''
82 ));
83  
84 0 $self->addfield(Crypt::PWSafe3::Field->new(
85 name => 'notes',
86 value => ''
87 ));
88  
89 0 $self->addfield(Crypt::PWSafe3::Field->new(
90 name => 'group',
91 value => ''
92 ));
93  
94 0 return $self;
95 }
96  
97 0 ▶sub modifyfield {
Subroutine modifyfield: not called CC=7 SCAR=40.3
Pod undocumented
98 #
99 # add or modify a record field
100 0 my($this, $name, $value) = @_;
101 0 ▶ if (exists $map2type{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map2type{$name}) { } 0 0 0
102 0 my $type = $map2type{$name};
103 0 my $field = Crypt::PWSafe3::Field->new(
104 type => $type,
105 value => $value
106 );
107  
108 0 my $time = time;
109  
110 # we are in fact just overwriting an eventually
111 # existing field with a new one, instead of modifying
112 # it, so we are using the conversion automatism in
113 # Field::new()
114 0 $this->addfield($field);
115  
116 # mark the field as modified if it's passwd field
117 0 ▶ $this->addfield(Crypt::PWSafe3::Field->new(
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "passwd" 0 0 0
118 name => 'mtime',
119 value => $time
120 )) if $name eq 'passwd';
121  
122 0 $this->addfield(Crypt::PWSafe3::Field->new(
123 name => "lastmod",
124 value => $time
125 ));
126  
127 0 my ($package, $filename, $line, $subroutine, @ignore) = caller(1);
128  
129 # this looks a little bit weird but it's a cool feat.
130 # 'super' contains the vault object (of class Crypt::PWSafe3),
131 # which initially called our new() method, so we know to which
132 # vault we belong.
133 # therefore, if the user just calls $record->passwd('newpw'),
134 # then we can update the record directly on the vault object,
135 # so that the user doesn't have to call modifyrecord. this is
136 # especially usefull inside a loop.
137 # also note, that the 'super' parameter to Crypt::PWSafe3::Record::new()
138 # is not documented, so it's an internal parameter not to be used
139 # by users. however, maybe in the future it would be useful to
140 # have it populated so that if a user has a function which takes a
141 # record as parameter, then in this function he could access the
142 # vault as well. maybe.
143 #
144 # Thu May 21 10:04:15 CEST 2015 tlinden\@cpan.org
145 0 ▶ if (exists $this->{super} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"super"} and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::newrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::Record::modifyfield$/) 0 0 0
Condition0%0/9
exists $this->{"super"} and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::newrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::Record::modifyfield$/
!ll&&!rl&&r
000
exists $this->{"super"} and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::newrecord$/
!ll&&!rl&&r
000
exists $this->{"super"} and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/
!ll&&!rl&&r
000
MC/DC0%0/4
exists $this->{"super"} and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::newrecord$/ and not +("${package}::$subroutine") =~ /Crypt::PWSafe3::Record::modifyfield$/
exists $this->{"super"} not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/ not +("${package}::$subroutine") =~ /Crypt::PWSafe3::newrecord$/ not +("${package}::$subroutine") =~ /Crypt::PWSafe3::Record::modifyfield$/
Truth table0%0/5
A and not(B) and not(C) and not(D)
A= exists $this->{"super"}B= not +("${package}::$subroutine") =~ /Crypt::PWSafe3::modifyrecord$/C= not +("${package}::$subroutine") =~ /Crypt::PWSafe3::newrecord$/D= not +("${package}::$subroutine") =~ /Crypt::PWSafe3::Record::modifyfield$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
146 "${package}::${subroutine}" !~ /Crypt::PWSafe3::modifyrecord$/ &&
147 "${package}::${subroutine}" !~ /Crypt::PWSafe3::newrecord$/ &&
148 "${package}::${subroutine}" !~ /Crypt::PWSafe3::Record::modifyfield$/
149 ) {
150 # we've been called from the outside (the user in fact) and
151 # we're attached to a vault, so update ourselfes there as well
152 0 $this->{super}->modifyrecord($this->uuid, $name, $value);
153 }
154  
155 0 return $field;
156 }
157 else {
158 0 croak "Unknown field $name";
159 }
160 }
161  
162 0 ▶sub genuuid {
Subroutine genuuid: not called CC=1 SCAR=6.9
Pod undocumented
163 #
164 # generate a v4 uuid string
165 0 my($this) = @_;
166 0 my $ug = Data::UUID->new();
167 0 my $uuid = $ug->create();
168 0 return $uuid;
169 }
170  
171 0 ▶sub addfield {
Subroutine addfield: not called CC=2 SCAR=17.9
Pod undocumented
172 #
173 # add a field to the record
174 0 my ($this, $field) = @_;
175 0 my $name = $map2name{$field->type};
176 0 ▶ unless( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
177 0 $name = $field->type; # consistent with Field->new
178 }
179 0 $this->{field}->{ $name } = $field;
180 }
181  
182 0 ▶sub policy {
Subroutine policy: not called CC=2 SCAR=17.9
Pod undocumented
183 #
184 # return or set a password policy
185 0 my ($this, $policy) = @_;
186  
187 0 ▶ if($policy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy) { } 0 0 0
188 0 $this->{policy} = $policy;
189 0 $this->pwpol($policy->encode());
190 }
191 else {
192 0 $this->{policy} = Crypt::PWSafe3::PasswordPolicy->new(raw => $this->pwpol);
193 }
194  
195 0 return $this->{policy};
196 }
197  
198 =head1 NAME
199  
200 Crypt::PWSafe3::Record - Represents a Passwordsafe v3 data record
201  
202 =head1 SYNOPSIS
203  
204 use Crypt::PWSafe3;
205 my $record = $vault->getrecord($uuid);
206 $record->title('t2');
207 $record->passwd('foobar');
208 print $record->notes;
209  
210 =head1 DESCRIPTION
211  
212 B<Crypt::PWSafe3::Record> represents a Passwordsafe v3 data record.
213 Each record consists of a number of fields of type B<Crypt::PWSafe3::Field>.
214 The class provides get/set methods to access the values of those
215 fields.
216  
217 It is also possible to access the raw unencoded values of the fields
218 by accessing them directly, refer to L<Crypt::PWSafe3::Field> for more
219 details on this.
220  
221 If the record object has been created by L<Crypt::PWSafe3> (and fetched with
222 Crypt::PWSafe3::getrecord), then it's still associated with the L<Crypt::PWSafe3>
223 parent object. Changes to the record will therefore automatically populated
224 back into the parent object (the vault). This is not the case if you created
225 the record object yourself.
226  
227 =head1 METHODS
228  
229 =head2 B<uuid([string])>
230  
231 Returns the UUID without argument. Sets the UUID if an argument
232 is given. Must be a hex representation of an L<Data::UUID> object.
233  
234 This will be generated automatically for new records, so you
235 normally don't have to cope with.
236  
237 =head2 B<user([string])>
238  
239 Returns the username without argument. Sets the username
240 if an argument is given.
241  
242 =head2 B<title([string])>
243  
244 Returns the title without argument. Sets the title
245 if an argument is given.
246  
247 =head2 B<passwd([string])>
248  
249 Returns the password without argument. Sets the password
250 if an argument is given.
251  
252 =head2 B<notes([string])>
253  
254 Returns the notes without argument. Sets the notes
255 if an argument is given.
256  
257 =head2 B<group([string])>
258  
259 Returns the group without argument. Sets the group
260 if an argument is given.
261  
262 Group hierarchy can be done by separating subgroups
263 by dot, eg:
264  
265 $record->group('accounts.banking');
266  
267 =head2 B<ctime([time_t])>
268  
269 Returns the creation time without argument. Sets the creation time
270 if an argument is given. Argument must be an integer timestamp
271 as returned by L<time()>.
272  
273 This will be generated automatically for new records, so you
274 normally don't have to cope with.
275  
276 =head2 B<atime([time_t])>
277  
278 Returns the access time without argument. Sets the access time
279 if an argument is given. Argument must be an integer timestamp
280 as returned by L<time()>.
281  
282 B<Crypt::PWSafe3> doesn't update the atime field currently. So if
283 you mind, do it yourself.
284  
285 =head2 B<mtime([time_t])>
286  
287 Returns the modification time of the passwd field without argument. Sets the modification time
288 if an argument is given. Argument must be an integer timestamp
289 as returned by L<time()>.
290  
291 This will be generated automatically for modified records if the passwd field changed, so you
292 normally don't have to cope with.
293  
294 =head2 B<lastmod([string])>
295  
296 Returns the modification time without argument. Sets the modification time
297 if an argument is given. Argument must be an integer timestamp
298 as returned by L<time()>.
299  
300 This will be generated automatically for modified records, so you
301 normally don't have to cope with.
302  
303 =head2 B<url([string])>
304  
305 Returns the url without argument. Sets the url
306 if an argument is given. The url must be in the well
307 known notation as:
308  
309 proto://host/path
310  
311 =head2 B<pwhist([string])>
312  
313 Returns the password history without argument. Sets the password history
314 if an argument is given.
315  
316 B<Crypt::PWSafe3> doesn't update the pwhist field currently. So if
317 you mind, do it yourself. Refer to L<Crypt::PWSafe3::Databaseformat>
318 for more details.
319  
320 =head2 B<pwpol([string])>
321  
322 Returns the password policy without argument. Sets the password policy
323 if an argument is given.
324  
325 This is the raw encoded policy string. If you want to access it, use the
326 B<policy()> method, see below.
327  
328 =head2 B<policy([Crypt::PWSafe3::PasswordPolicy object])>
329  
330 If called without arguments, returns a Crypt::PWSafe3::PasswordPolicy
331 object. See L<Crypt::PWSafe3::PasswordPolicy> for details, how to access
332 it.
333  
334 To modify the password policy, create new Crypt::PWSafe3::PasswordPolicy
335 object or modify the existing one and pass it as argument to the
336 B<policy> method.
337  
338 =head2 B<pwexp([string])>
339  
340 Returns the password expire time without argument. Sets the password expire time
341 if an argument is given.
342  
343 B<Crypt::PWSafe3> doesn't update the pwexp field currently. So if
344 you mind, do it yourself. Refer to L<Crypt::PWSafe3::Databaseformat>
345 for more details.
346  
347 =head1 MANDATORY FIELDS
348  
349 B<Crypt::PWSafe3::Record> creates the following fields automatically
350 on creation, because those fields are mandatory:
351  
352 B<uuid> will be generated using L<Data::UUID>.
353  
354 B<user, password, title> will be set to the empty string.
355  
356 B<ctime, atime, mtime, lastmod> will be set to current
357 time of creation time.
358  
359 =head1 SEE ALSO
360  
361 L<Crypt::PWSafe3>
362  
363 =head1 AUTHOR
364  
365 T.v.Dein <tlinden@cpan.org>
366  
367 =head1 COPYRIGHT
368  
369 Copyright (c) 2011-2015 by T.v.Dein <tlinden@cpan.org>.
370 All rights reserved.
371  
372 =head1 LICENSE
373  
374 This program is free software; you can redistribute it
375 and/or modify it under the same terms of the Artistic
376 License 2.0, see: L<http://www.perlfoundation.org/artistic_license_2_0>
377  
378 =cut
379  
380 1 1;
« PWSafe3/PasswordPolicy.pm ↑ Summary PWSafe3/SHA256.pm »