File: | Dpkg/OpenPGP/KeyHandle.pm |
Coverage: | 84.3% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # Copyright © 2022 Guillem Jover <guillem@debian.org> | ||||||
2 | # | ||||||
3 | # This program is free software; you can redistribute it and/or modify | ||||||
4 | # it under the terms of the GNU General Public License as published by | ||||||
5 | # the Free Software Foundation; either version 2 of the License, or | ||||||
6 | # (at your option) any later version. | ||||||
7 | # | ||||||
8 | # This program is distributed in the hope that it will be useful, | ||||||
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
11 | # GNU General Public License for more details. | ||||||
12 | # | ||||||
13 | # You should have received a copy of the GNU General Public License | ||||||
14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
15 | |||||||
16 | package Dpkg::OpenPGP::KeyHandle; | ||||||
17 | |||||||
18 | 42 42 42 | 84 42 421 | use strict; | ||||
19 | 42 42 42 | 83 42 923 | use warnings; | ||||
20 | |||||||
21 | our $VERSION = '0.01'; | ||||||
22 | |||||||
23 | 42 42 42 | 83 42 885 | use Carp; | ||||
24 | 42 42 42 | 84 42 9037 | use List::Util qw(any none); | ||||
25 | |||||||
26 | sub new { | ||||||
27 | 85 | 0 | 508 | my ($this, %opts) = @_; | |||
28 | 85 | 547 | my $class = ref($this) || $this; | ||||
29 | |||||||
30 | my $self = { | ||||||
31 | type => $opts{type} // 'auto', | ||||||
32 | handle => $opts{handle}, | ||||||
33 | 85 | 247 | }; | ||||
34 | 85 | 190 | bless $self, $class; | ||||
35 | |||||||
36 | 85 | 330 | $self->_sanitize(); | ||||
37 | |||||||
38 | 85 | 129 | return $self; | ||||
39 | } | ||||||
40 | |||||||
41 | my $keyid_regex = qr/^(?:0x)?([[:xdigit:]]+)$/; | ||||||
42 | |||||||
43 | sub _sanitize { | ||||||
44 | 110 | 184 | my ($self) = shift; | ||||
45 | |||||||
46 | 110 | 448 | my $type = $self->{type}; | ||||
47 | 110 | 330 | if ($type eq 'auto') { | ||||
48 | 5 | 39 | if (-e $self->{handle}) { | ||||
49 | 0 | 0 | $type = 'keyfile'; | ||||
50 | } else { | ||||||
51 | 5 | 5 | $type = 'autoid'; | ||||
52 | } | ||||||
53 | } | ||||||
54 | |||||||
55 | 110 | 416 | if ($type eq 'autoid') { | ||||
56 | 5 | 17 | if ($self->{handle} =~ m/$keyid_regex/) { | ||||
57 | 3 | 3 | $self->{handle} = $1; | ||||
58 | 3 | 1 | $type = 'keyid'; | ||||
59 | } else { | ||||||
60 | 2 | 2 | $type = 'userid'; | ||||
61 | } | ||||||
62 | 5 | 2 | $self->{type} = $type; | ||||
63 | } elsif ($type eq 'keyid') { | ||||||
64 | 3 | 8 | if ($self->{handle} =~ m/$keyid_regex/) { | ||||
65 | 3 | 4 | $self->{handle} = $1; | ||||
66 | } | ||||||
67 | } | ||||||
68 | |||||||
69 | 110 341 | 1260 1044 | if (none { $type eq $_ } qw(userid keyid keyfile keystore)) { | ||||
70 | 0 | 0 | croak "unknown type parameter value $type"; | ||||
71 | } | ||||||
72 | |||||||
73 | 110 | 375 | return; | ||||
74 | } | ||||||
75 | |||||||
76 | sub needs_keystore { | ||||||
77 | 98 | 0 | 127 | my $self = shift; | |||
78 | |||||||
79 | 98 196 | 635 319 | return any { $self->{type} eq $_ } qw(keyid userid); | ||||
80 | } | ||||||
81 | |||||||
82 | sub set { | ||||||
83 | 25 | 0 | 1150 | my ($self, $type, $handle) = @_; | |||
84 | |||||||
85 | 25 | 350 | $self->{type} = $type; | ||||
86 | 25 | 275 | $self->{handle} = $handle; | ||||
87 | |||||||
88 | 25 | 375 | $self->_sanitize(); | ||||
89 | } | ||||||
90 | |||||||
91 | sub type { | ||||||
92 | 196 | 0 | 141 | my $self = shift; | |||
93 | |||||||
94 | 196 | 517 | return $self->{type}; | ||||
95 | } | ||||||
96 | |||||||
97 | sub handle { | ||||||
98 | 170 | 0 | 133 | my $self = shift; | |||
99 | |||||||
100 | 170 | 918 | return $self->{handle}; | ||||
101 | } | ||||||
102 | |||||||
103 | 1; |