diff options
Diffstat (limited to 'extensions/AuthJWT/lib/Source.pm')
-rw-r--r-- | extensions/AuthJWT/lib/Source.pm | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/extensions/AuthJWT/lib/Source.pm b/extensions/AuthJWT/lib/Source.pm new file mode 100644 index 000000000..3d82f4d68 --- /dev/null +++ b/extensions/AuthJWT/lib/Source.pm @@ -0,0 +1,178 @@ +# 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::Source; + +use strict; +use warnings; +use 5.10.1; +use Scalar::Util qw(blessed); + +use Bugzilla::Constants; +use Bugzilla::Field; +use Bugzilla::Util; +use Bugzilla::Error; + +use base qw(Bugzilla::Object); + +use Bugzilla::Extension::AuthJWT::Util; + +use constant DB_TABLE => 'authjwt_source'; +use constant NAME_FIELD => 'kid'; +use constant LIST_ORDER => 'kid'; + +use constant DB_COLUMNS => qw( + id + kid + cert + comment + isactive +); + +use constant UPDATE_COLUMNS => qw( + kid + cert + comment + isactive +); + +use constant VALIDATORS => { + kid => \&_check_cert, + cert => \&_check_cert, + comment => \&_check_cert, + isactive => \&Bugzilla::Object::check_boolean, +}; + +############################### +#### Validators #### +############################### + +sub _check_cert { + my ($invocant, $cert) = @_; + $cert = trim($cert); + return ($cert); +} + +############################### +#### Methods #### +############################### + +sub set_kid { $_[0]->set('kid', $_[1]); return; } +sub set_cert { $_[0]->set('cert', $_[1]); return; } +sub set_comment { $_[0]->set('comment', $_[1]); return; } +sub set_isactive { $_[0]->set('isactive', $_[1]); return; } + +# groups specify who is NOT allowed to use it! +sub set_groups { + my $self = shift; + my $group_ids = shift; + + my @groups; + if (ref $group_ids) { + @groups = @$group_ids; + } + else { + @groups = ($group_ids); + } + + my $dbh = Bugzilla->dbh; + + $dbh->do(q{DELETE FROM authjwt_groups WHERE authjwt_source_id = ?}, + undef, $self->id); + + my $sth = $dbh->prepare(q{INSERT INTO authjwt_groups VALUES (?, ?)}); + + foreach my $group_id (@groups) { + next unless $group_id && $group_id =~ /(\d+)/; + my $id = $1; + my $group = Bugzilla::Group->new($id); + $sth->execute($self->id, $id) if ($group); + } + + delete $self->{groups}; + + return; +} + +############################### +#### Accessors #### +############################### + +sub id { return ($_[0]->{id}); } +sub kid { return ($_[0]->{kid}); } +sub cert { return ($_[0]->{cert}); } +sub comment { return ($_[0]->{comment}); } +sub isactive { return ($_[0]->{isactive}); } + +sub groups { + my $self = shift; + + unless ($self->{groups}) { + my $dbh = Bugzilla->dbh; + + my $ids + = $dbh->selectcol_arrayref( + q{SELECT DISTINCT group_id FROM authjwt_groups WHERE authjwt_source_id = ?}, + undef, $self->id); + + $self->{groups} = $ids; + } + return ($self->{groups}); +} + +1; + +__END__ + +=head1 Description + +Bugzilla::Extension::AuthJWT::Source - A module for encapsulating Sources of JWTs. + +=head1 Fields + +=over 4 + +=item id + +The index for this Source in the database. + +=item kid + +The key ID to validate on. + +=item cert + +The public certificate to validate this kid. + +=back + +=head1 Accessors + +These methods allow you to get the specified field for the JWT Source. + +=over 4 + +=item id + +=item kid + +=item cert + +=item comment + +=back + +=head1 Methods + +These methods allow you to set the specified field for the JWT Source. + +=over 4 + +=item set_kid + +=item set_cert + +=item set comment + +=back |