blib/lib/Lemonldap/NG/Portal/Issuer/JitsiMeetTokens.pm

filter: Statementstmt 86.1% 118 / 137 Branchbran 60.4% 29 / 48 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 95.8% 23 / 24 Podpod 40.0% 2 / 5 totaltotal 80.2% 179 / 223 CC 28 SCAR 18.4
CC
28
Cov
80%
jitsi
18.7
asap
5.0
_sendAsap
4.1
CRAP 6.3
« blib/lib/Lemonldap/NG/Portal/Plugins/ForceAuthn.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Register/AD.pm »
1 package Lemonldap::NG::Portal::Issuer::JitsiMeetTokens;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Base64 qw/decode_base64url encode_base64url/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Crypt::JWT qw(encode_jwt);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Crypt::OpenSSL::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 PE_REDIRECT
15 PE_UNAUTHORIZEDPARTNER
16 PE_ERROR
17 PE_OK
18 2 );
19  
20 2 our $VERSION = '2.23.0';
21  
22 2 extends 'Lemonldap::NG::Portal::Main::Issuer';
23 2 with 'Lemonldap::NG::Portal::Lib::Key';
24  
25 2 has rule => ( is => 'rw' );
26  
27 has jitsi_default_server => (
28 is => "rw",
29 lazy => 1,
30 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 2 $_[0]->conf->{jitsiDefaultServer};
32 }
33 2 );
34  
35 has jitsi_appid => (
36 is => "rw",
37 lazy => 1,
38 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 2 $_[0]->conf->{jitsiAppId};
40 }
41 2 );
42  
43 has jitsi_expiration => (
44 is => "rw",
45 lazy => 1,
46 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
47 2 ▶ $_[0]->conf->{jitsiExpiration} || "300";
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"jitsiExpiration"} or 300 2 0 2
48 }
49 2 );
50  
51 has jitsi_signing_alg => (
52 is => "rw",
53 lazy => 1,
54 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
55 2 ▶ $_[0]->conf->{jitsiSigningAlg} || "HS256";
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"jitsiSigningAlg"} or "HS256" 2 0 2
56 }
57 2 );
58  
59 has jitsi_appsecret => (
60 is => "rw",
61 lazy => 1,
62 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1 $_[0]->conf->{jitsiAppSecret};
64 }
65 2 );
66  
67 has jitsi_id_attribute => (
68 is => "rw",
69 lazy => 1,
70 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
71 2 ▶ $_[0]->conf->{jitsiIdAttribute} || $_[0]->conf->{whatToTrace};
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"jitsiIdAttribute"} or $_[0]->conf->{"whatToTrace"} 0 2 2
72 }
73 2 );
74  
75 has jitsi_name_attribute => (
76 is => "rw",
77 lazy => 1,
78 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
79 2 ▶ $_[0]->conf->{jitsiNameAttribute} || "cn";
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"jitsiNameAttribute"} or "cn" 0 2 2
80 }
81 2 );
82 has jitsi_mail_attribute => (
83 is => "rw",
84 lazy => 1,
85 2 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
86 2 ▶ $_[0]->conf->{jitsiMailAttribute} || "mail";
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"jitsiMailAttribute"} or "mail" 0 2 2
87 }
88 2 );
89  
90 9 ▶sub init {
Subroutine init: called CC=3 SCAR=11.1
Pod undocumented
91 9 my ($self) = @_;
92  
93 my $compiled_rule =
94 $self->p->buildRule( $self->conf->{issuerDBJitsiMeetTokensRule},
95 9 "Jitsi JWT issuer rule" );
96 9 ▶ return 0 if !$compiled_rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $compiled_rule 0 9 9
97  
98 9 ▶ return 0 unless $self->SUPER::init();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::init 0 9 9
99  
100 9 $self->addUnauthRoute( $self->path() => { asap => "asap" }, ['GET'] );
101 9 $self->addAuthRoute( $self->path() => { asap => "asap" }, ['GET'] );
102 9 $self->rule($compiled_rule);
103 9 return 1;
104  
105 }
106  
107 9 ▶sub asap {
Subroutine asap: called CC=5 SCAR=16.1
Pod undocumented
108 9 my ( $self, $req, @path ) = @_;
109  
110 9 my $filename = $path[0];
111  
112 9 my $hash = $filename =~ s/\.pem$//r;
113  
114 9 for my $key_id ( split( /\s*,\s*/, $self->conf->{jitsiSigningKey} ) ) {
115 18 my $key = $self->get_public_key($key_id);
116 18 ▶ if ( $key
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $key->{"external_id"} and $hash eq &sha256_hex($key->{"external_id"})) 6 12 18
Condition83%5/6
$key and $key->{"external_id"} and $hash eq &sha256_hex($key->{"external_id"})
!ll&&!rl&&r
396
$key and $key->{"external_id"}
!ll&&!rl&&r
3015
MC/DC66%2/3
$key and $key->{"external_id"} and $hash eq &sha256_hex($key->{"external_id"})
$key $key->{"external_id"} $hash eq &sha256_hex($key->{"external_id"})
Truth table75%3/4
A and B and C
A= $keyB= $key->{"external_id"}C= $hash eq &sha256_hex($key->{"external_id"})
ABCresult
0XX0
10X0
1100
1111
117 and $key->{external_id}
118 and $hash eq sha256_hex( $key->{external_id} ) )
119 {
120 6 return $self->_sendAsap( $req, $key->{public} );
121 }
122 }
123  
124 3 return $self->p->sendError( $req, "Unknown key id hash", 404 );
125 }
126  
127 6 ▶sub _sendAsap {
Subroutine _sendAsap: called CC=4 SCAR=14.1
128 6 my ( $self, $req, $pem ) = @_;
129  
130 6 my $res;
131  
132 # Try to parse as RSA key
133 6 eval {
134 6 my $pubkey = Crypt::OpenSSL::RSA->new_public_key($pem);
135 1 $res = $pubkey->get_public_key_x509_string;
136 };
137 6 my $parse_pubkey_error = $@;
138  
139 6 ▶ if ( !$res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 5 1 6
140  
141 # Try to parse as X.509 cert
142 5 eval {
143 5 my $x509 = Crypt::OpenSSL::X509->new_from_string( $pem,
144 Crypt::OpenSSL::X509::FORMAT_PEM );
145 5 my $pub = $x509->pubkey;
146 5 my $type = $x509->pubkey_type;
147 5 ▶ if ( ($type) eq "rsa" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'rsa') { } 5 0 5
148 5 my $pubkey = Crypt::OpenSSL::RSA->new_public_key($pub);
149 5 $res = $pubkey->get_public_key_x509_string;
150 }
151 else {
152 0 die "Unsupported pubkey type $type";
153 }
154 };
155 }
156 6 my $parse_cert_error = $@;
157  
158 6 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 6 0 6
159 6 return $self->p->sendBinaryResponse( $req, $res,
160 type => 'application/x-pem-file' );
161 }
162 else {
163 0 $self->logger->error(
164 "Could not parse public key as RSA ($parse_pubkey_error)"
165 . " or X.509 ($parse_cert_error)" );
166 0 return $self->p->sendError( $req, "Unsupported public key format",
167 500 );
168 }
169 }
170  
171 5 ▶sub run {
Subroutine run: called CC=3 SCAR=11.2
Pod documented
172 5 my ( $self, $req, @path ) = @_;
173  
174 # Check activation rule
175 5 ▶ unless ( $self->rule->( $req, $req->sessionInfo ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->rule->($req, $req->sessionInfo)) 1 4 5
176 1 $self->userLogger->error('Jitsi JWT service not authorized');
177 1 return PE_UNAUTHORIZEDPARTNER;
178 }
179  
180 4 ▶ if ( $path[0] eq "login" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path[0] eq "login") 4 0 4
181 4 return $self->jitsi($req);
182 }
183 0 return PE_OK;
184 }
185  
186 # Nothing to do here for now
187 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
188 0 return PE_OK;
189 }
190  
191 4 ▶sub jitsi {
Subroutine jitsi: called CC=12 SCAR=29.3
Pod undocumented
192 4 my ( $self, $req ) = @_;
193 4 my $room = $req->param('room');
194  
195 4 ▶ if ( !$self->jitsi_default_server ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->jitsi_default_server) 0 4 4
196 0 $self->logger->error("Jitsi Server URL not set in configuration");
197 0 return PE_ERROR;
198 }
199 4 ▶ if ( !$self->jitsi_appid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->jitsi_appid) 0 4 4
200 0 $self->logger->error("Jitsi Application ID not set in configuration");
201 0 return PE_ERROR;
202 }
203 4 ▶ if ( !$room ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($room) 2 2 4
204 2 $self->logger->error("Missing room parameter");
205 2 return PE_ERROR;
206 }
207  
208 my $payload = {
209 iss => $self->p->buildUrl(),
210 room => $room,
211 exp => ( time + $self->jitsi_expiration ),
212 sub => '*',
213 aud => $self->jitsi_appid,
214 context => {
215 user => {
216 id => $req->userData->{ $self->jitsi_id_attribute },
217 (
218 $self->jitsi_name_attribute
219 ? ( name => $req->userData->{ $self->jitsi_name_attribute }
220 )
221 : ()
222 ),
223 (
224 $self->jitsi_mail_attribute
225 ? ( email =>
226 2 ▶ $req->userData->{ $self->jitsi_mail_attribute } )
Branch: 2 branches
BranchTrueFalseTotal
$self->jitsi_name_attribute ? : 2 0 2
$self->jitsi_mail_attribute ? : 2 0 2
227 : ()
228 ),
229 affiliation => "owner"
230 }
231 }
232 };
233 2 my $server;
234 2 my $u = URI->new_abs( $room, URI->new( $self->jitsi_default_server ) );
235  
236 2 my @extra_headers;
237 my $key;
238 2 ▶ if ( $self->jitsi_signing_alg =~ /^HS/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->jitsi_signing_alg =~ /^HS/) { } 1 1 2
239 1 ▶ if ( !$self->jitsi_appsecret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->jitsi_appsecret) 0 1 1
240 0 $self->logger->error(
241 "Jitsi Application secret not set in configuration");
242 0 return PE_ERROR;
243 }
244 1 $key = $self->jitsi_appsecret;
245  
246 }
247 else {
248 1 my ($key_id) = split( /\s*,\s*/, $self->conf->{jitsiSigningKey} );
249 1 ▶ if ( !$key_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key_id) 0 1 1
250 0 $self->logger->error("jitsiSigningKey is not set");
251 0 return PE_ERROR;
252 }
253  
254 1 my $pkey = $self->get_private_key($key_id);
255 1 ▶ if ( !$pkey ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkey) 0 1 1
256 0 $self->logger->error("Jitsi signing key $key_id was not found");
257 0 return PE_ERROR;
258 }
259  
260 1 ▶ if ( !$pkey->{external_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkey->{"external_id"}) 0 1 1
261 0 $self->logger->error(
262 "Jitsi signing key does not have an identified."
263 . " You must set oidcServiceKeyIdSig" );
264 0 return PE_ERROR;
265 }
266 1 @extra_headers = ( kid => $pkey->{external_id} );
267 1 my $private = $pkey->{private};
268 1 $key = \$private;
269 }
270  
271 2 my $jwt = eval {
272 2 encode_jwt(
273 payload => to_json($payload),
274 alg => $self->jitsi_signing_alg,
275 key => $key,
276 extra_headers => { typ => "JWT", @extra_headers },
277 );
278 };
279 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
280 0 $self->logger->error("Could not encode JWT: $@");
281 0 return $self->p->doPE( $req, PE_ERROR );
282 }
283  
284 2 $u->query_form( jwt => $jwt );
285 2 $req->urldc( $u->as_string );
286 2 return PE_REDIRECT;
287 }
288  
289 2 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/ForceAuthn.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Register/AD.pm »