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

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