summaryrefslogtreecommitdiff
blob: 0e613eb9dd54ebd5a7af48652bc99e768c4d0930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

package Bugzilla::Extension::AuthJWT::Util;

use 5.10.1;
use strict;
use warnings;

use Bugzilla::Extension::AuthJWT::Source;
use JSON;
use Bugzilla::User;

use parent qw(Exporter);
our @EXPORT = qw(
  sources_kids
  user_can_use
);

# This file can be loaded by your extension via
# "use Bugzilla::Extension::AuthJWT::Util". You can put functions
# used by your extension in here. (Make sure you also list them in
# @EXPORT.)

sub sources_kids {
  my %sources;

  foreach my $src (@{Bugzilla::Extension::AuthJWT::Source->match()}) {
    $sources{$src->kid} = $src->cert;
  }

  my $sources = encode_json(\%sources);
  return ($sources);
}

sub user_can_use {
  my ($kid, $user) = @_;

  my $can    = 1;
  my $source = Bugzilla::Extension::AuthJWT::Source->check({kid => $kid});
  $can = 0 if (any { $user->in_group_id($_->id) } @{$source->groups()});

  return ($can);
}

1;