blib/lib/Lemonldap/NG/Portal/Lib/Key.pm

filter: Statementstmt 86.0% 43 / 50 Branchbran 71.8% 23 / 32 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 69.0% 78 / 113 CC 25 SCAR 31.1
CC
25
Cov
73%
_get_key_config
32.4
get_private_key
4.6
get_public_key
4.0
CRAP 22.5
« blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/OfflineTokens.pm »
1 package Lemonldap::NG::Portal::Lib::Key;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Lemonldap::NG::Portal::Lib::Key - A role for private key management in Portal
8 plugins
9  
10 =head1 SYNOPSIS
11  
12 use Mouse;
13  
14 with 'Lemonldap::NG::Portal::Lib::Key';
15  
16 =head1 DESCRIPTION
17  
18 This role is meant to be composed into portal modules to give them the ability
19 to lookup private/public keys from the General parameters > Keys interface
20  
21 All asymetric key retrieval should be performed through the functions provided
22 here
23  
24 =head1 METHODS
25 =cut
26  
27 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 55 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 55 our $VERSION = '2.22.0';
31  
32 55 requires qw(conf);
33  
34 =pod
35  
36 =head2 get_private_key
37  
38 This method returns a private key structure containing the following fields
39  
40 =over
41  
42 =item private: PEM private key
43  
44 =item password: optional password for key decryption
45  
46 =item public: PEM public key or certificate
47  
48 =item external_id: optional external key identified (kid)
49  
50 =back
51  
52 =cut
53  
54 sub get_private_key {
55 11 ▶ my ( $self, $key_id ) = @_;
Subroutine get_private_key: called CC=4 SCAR=15.2
Pod documented
56 11 ▶ my $key_id_log = $key_id // '[undef]' || "[empty]";
Condition50%2/4
($key_id // '[undef]') || '[empty]'
l!l
110
$key_id // "[undef]"
l!l
110
MC/DC0%0/1
($key_id // '[undef]') || '[empty]'
$key_id
Truth table50%1/2
A
A= $key_id
Aresult
11
00
57  
58 11 $self->logger->debug("Looking up private key $key_id_log");
59  
60 11 my $key_config = $self->_get_key_config($key_id);
61  
62 11 ▶ if ($key_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_config) 11 0 11
63 11 $self->logger->debug("Found private key $key_id_log");
64 return {
65 private => $key_config->{keyPrivate},
66 public => $key_config->{keyPublic},
67 external_id => $key_config->{keyId},
68 password => $key_config->{keyPrivatePwd},
69 11 };
70 }
71  
72 0 $self->logger->debug("Private key $key_id_log not found");
73 0 return;
74 }
75  
76 sub get_public_key {
77 104 ▶ my ( $self, $key_id ) = @_;
Subroutine get_public_key: called CC=4 SCAR=14.0
Pod undocumented
78 104 ▶ my $key_id_log = $key_id // '[undef]' || "[empty]";
Condition50%2/4
($key_id // '[undef]') || '[empty]'
l!l
1040
$key_id // "[undef]"
l!l
1040
MC/DC0%0/1
($key_id // '[undef]') || '[empty]'
$key_id
Truth table50%1/2
A
A= $key_id
Aresult
11
00
79 104 $self->logger->debug("Looking up public key $key_id_log");
80  
81 104 my $key_config = $self->_get_key_config($key_id);
82  
83 104 ▶ if ($key_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_config) 47 57 104
84 47 $self->logger->debug("Found public key $key_id_log");
85 return {
86 public => $key_config->{keyPublic},
87 external_id => $key_config->{keyId},
88 47 };
89 }
90  
91 57 $self->logger->debug("Public key $key_id_log not found");
92 57 return;
93 }
94  
95 sub _get_key_config {
96 115 ▶ my ( $self, $key_id ) = @_;
Subroutine _get_key_config: called CC=19 SCAR=34.8
97  
98 115 ▶ return unless $key_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_id 0 115 115
99 115 ▶ if ( my $key = $self->conf->{'keys'}->{$key_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $self->conf->{"keys"}{$key_id}) 6 109 115
100 return {
101 keyPrivate => $key->{keyPrivate},
102 keyPrivatePwd => ( $key->{keyPrivatePwd} // "" ),
103 keyPublic => $key->{keyPublic},
104 6 ▶ keyId => ( $key->{keyId} || $key_id ),
Condition40%2/5
$key->{"keyPrivatePwd"} // ""
l!l
06
$key->{'keyId'} || $key_id
l!l&&r!l&&!r
600
MC/DC0%0/1
$key->{"keyPrivatePwd"} // ""
$key->{"keyPrivatePwd"}
MC/DC0%0/2
$key->{'keyId'} || $key_id
$key->{'keyId'} $key_id
Truth table50%1/2
A
A= $key->{"keyPrivatePwd"}
Aresult
11
00
Truth table33%1/3
A || B
A= $key->{'keyId'}B= $key_id
ABresult
1X1
011
000
105 };
106 }
107  
108 109 ▶ if ( $key_id eq "default-saml-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-saml-sig") 0 109 109
109 0 ▶ if ( $self->conf->{samlServicePrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"samlServicePrivateKeySig"}) 0 0 0
110 return {
111 keyPrivate => $self->conf->{samlServicePrivateKeySig},
112 keyPrivatePwd =>
113 ( $self->conf->{samlServicePrivateKeySigPwd} // '' ),
114 keyPublic => $self->conf->{samlServicePublicKeySig},
115 0 ▶ keyId => "default-saml-sig",
Condition0%0/2
$self->conf->{"samlServicePrivateKeySigPwd"} // ""
l!l
00
MC/DC0%0/1
$self->conf->{"samlServicePrivateKeySigPwd"} // ""
$self->conf->{"samlServicePrivateKeySigPwd"}
Truth table0%0/2
A
A= $self->conf->{"samlServicePrivateKeySigPwd"}
Aresult
11
00
116 };
117 }
118 }
119  
120 109 ▶ if ( $key_id eq "default-saml-enc" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-saml-enc") 0 109 109
121 0 ▶ if ( $self->conf->{samlServicePrivateKeyEnc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"samlServicePrivateKeyEnc"}) 0 0 0
122 return {
123 keyPrivate => $self->conf->{samlServicePrivateKeyEnc},
124 keyPrivatePwd =>
125 ( $self->conf->{samlServicePrivateKeyEncPwd} // '' ),
126 keyPublic => $self->conf->{samlServicePublicKeyEnc},
127 0 ▶ keyId => "default-saml-enc",
Condition0%0/2
$self->conf->{"samlServicePrivateKeyEncPwd"} // ""
l!l
00
MC/DC0%0/1
$self->conf->{"samlServicePrivateKeyEncPwd"} // ""
$self->conf->{"samlServicePrivateKeyEncPwd"}
Truth table0%0/2
A
A= $self->conf->{"samlServicePrivateKeyEncPwd"}
Aresult
11
00
128 };
129 }
130 }
131  
132 109 ▶ if ( $key_id eq "default-oidc-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-oidc-sig") 44 65 109
133 44 ▶ if ( $self->conf->{oidcServicePrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServicePrivateKeySig"}) 41 3 44
134 return {
135 keyPrivate => $self->conf->{oidcServicePrivateKeySig},
136 keyPrivatePwd => '',
137 keyPublic => $self->conf->{oidcServicePublicKeySig},
138 keyId => $self->conf->{oidcServiceKeyIdSig},
139 41 };
140 }
141 }
142  
143 68 ▶ if ( $key_id eq "old-oidc-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "old-oidc-sig") 23 45 68
144 23 ▶ if ( $self->conf->{oidcServiceOldPrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServiceOldPrivateKeySig"}) 8 15 23
145 return {
146 keyPrivate => $self->conf->{oidcServiceOldPrivateKeySig},
147 keyPrivatePwd => '',
148 keyPublic => $self->conf->{oidcServiceOldPublicKeySig},
149 keyId => $self->conf->{oidcServiceOldKeyIdSig},
150 8 };
151 }
152 }
153  
154 60 ▶ if ( $key_id eq "new-oidc-sig" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "new-oidc-sig") 20 40 60
155 20 ▶ if ( $self->conf->{oidcServiceNewPrivateKeySig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServiceNewPrivateKeySig"}) 0 20 20
156 return {
157 keyPrivate => $self->conf->{oidcServiceNewPrivateKeySig},
158 keyPrivatePwd => '',
159 keyPublic => $self->conf->{oidcServiceNewPublicKeySig},
160 keyId => $self->conf->{oidcServiceNewKeyIdSig},
161 0 };
162 }
163 }
164  
165 60 ▶ if ( $key_id eq "default-oidc-enc" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_id eq "default-oidc-enc") 22 38 60
166 22 ▶ if ( $self->conf->{oidcServicePrivateKeyEnc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"oidcServicePrivateKeyEnc"}) 3 19 22
167 return {
168 keyPrivate => $self->conf->{oidcServicePrivateKeyEnc},
169 keyPrivatePwd => '',
170 keyPublic => $self->conf->{oidcServicePublicKeyEnc},
171 keyId => $self->conf->{oidcServiceKeyIdEnc},
172 3 };
173 }
174 }
175  
176 57 return;
177 }
178  
179 55 1;
180  
181 __END__
182  
183 =head1 AUTHORS
184  
185 =over
186  
187 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
188  
189 =back
190  
191 =head1 BUG REPORT
192  
193 Use OW2 system to report bug or ask for features:
194 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
195  
196 =head1 DOWNLOAD
197  
198 Lemonldap::NG is available at
199 L<https://lemonldap-ng.org/download>
200  
201 =head1 COPYRIGHT AND LICENSE
202  
203 See COPYING file for details.
204  
205 This library is free software; you can redistribute it and/or modify
206 it under the terms of the GNU General Public License as published by
207 the Free Software Foundation; either version 2, or (at your option)
208 any later version.
209  
210 This program is distributed in the hope that it will be useful,
211 but WITHOUT ANY WARRANTY; without even the implied warranty of
212 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
213 GNU General Public License for more details.
214  
215 You should have received a copy of the GNU General Public License
216 along with this program. If not, see L<http://www.gnu.org/licenses/>.
« blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/OIDC/OfflineTokens.pm »