Bugzilla – Attachment 141783 Details for
Bug 31378
Add a generic OAuth2/OIDC client implementation
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 31378: Add accessor methods for JSON attributes
Bug-31378-Add-accessor-methods-for-JSON-attributes.patch (text/plain), 9.07 KB, created by
PTFS Europe Sandboxes
on 2022-10-13 09:41:43 UTC
(
hide
)
Description:
Bug 31378: Add accessor methods for JSON attributes
Filename:
MIME Type:
Creator:
PTFS Europe Sandboxes
Created:
2022-10-13 09:41:43 UTC
Size:
9.07 KB
patch
obsolete
>From 5e6dab38be04be062fac7f27fb6167ed7559e106 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Wed, 24 Aug 2022 12:21:08 -0300 >Subject: [PATCH] Bug 31378: Add accessor methods for JSON attributes > >Signed-off-by: Katrin <me@myemail.com> >--- > Koha/Auth/Provider.pm | 122 ++++++++++++++++++++++++ > t/db_dependent/Koha/Auth/Provider.t | 141 ++++++++++++++++++++++++++-- > 2 files changed, 257 insertions(+), 6 deletions(-) > >diff --git a/Koha/Auth/Provider.pm b/Koha/Auth/Provider.pm >index d70a41b7bf..e8de84ada5 100644 >--- a/Koha/Auth/Provider.pm >+++ b/Koha/Auth/Provider.pm >@@ -21,7 +21,12 @@ use Modern::Perl; > > use base qw(Koha::Object); > >+use JSON qw( decode_json encode_json ); >+use Try::Tiny; >+ > use Koha::Auth::Provider::Domains; >+use Koha::Exceptions; >+use Koha::Exceptions::Object; > > =head1 NAME > >@@ -45,6 +50,123 @@ sub domains { > return Koha::Auth::Provider::Domains->_new_from_dbic( scalar $self->_result->domains ); > } > >+=head3 get_config >+ >+ my $config = $provider->get_config; >+ >+Returns a I<hashref> containing the configuration parameters for the provider. >+ >+=cut >+ >+sub get_config { >+ my ($self) = @_; >+ >+ return try { >+ return decode_json( $self->config ); >+ } >+ catch { >+ Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); >+ }; >+} >+ >+=head3 set_config >+ >+ # OAuth >+ $provider->set_config( >+ { >+ key => 'APP_ID', >+ secret => 'SECRET_KEY', >+ authorize_url => 'https://provider.example.com/auth', >+ token_url => 'https://provider.example.com/token', >+ } >+ ); >+ >+ # OIDC >+ $provider->set_config( >+ { >+ key => 'APP_ID', >+ secret => 'SECRET_KEY', >+ well_known_url => 'https://login.microsoftonline.com/tenant-id/v2.0/.well-known/openid-configuration', >+ } >+ ); >+ >+This method stores the passed config in JSON format. >+ >+=cut >+ >+sub set_config { >+ my ($self, $config) = @_; >+ >+ my @mandatory; >+ >+ if ( $self->protocol eq 'OIDC' ) { >+ @mandatory = qw(key secret well_known_url); >+ } >+ elsif ( $self->protocol eq 'OAuth' ) { >+ @mandatory = qw(key secret authorize_url token_url); >+ } >+ else { >+ Koha::Exception->throw( 'Unsupported protocol ' . $self->protocol ); >+ } >+ >+ for my $param (@mandatory) { >+ unless ( defined( $config->{$param} ) ) { >+ Koha::Exceptions::MissingParameter->throw( >+ error => "The $param parameter is mandatory" ); >+ } >+ } >+ >+ try { >+ my $encoded_config = encode_json($config); >+ $self->config($encoded_config)->store; >+ } catch { >+ Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_"); >+ }; >+ >+ return $self; >+} >+ >+=head3 get_mapping >+ >+ my $mapping = $provider->get_mapping; >+ >+Returns a I<hashref> containing the attribute mapping for the provider. >+ >+=cut >+ >+sub get_mapping { >+ my ($self) = @_; >+ >+ return try { >+ return decode_json( $self->mapping ); >+ } >+ catch { >+ Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); >+ }; >+} >+ >+=head3 set_mapping >+ >+ $provider->mapping( $mapping ); >+ >+This method stores the passed mappings in JSON format. >+ >+=cut >+ >+sub set_mapping { >+ my ($self, $mapping) = @_; >+ >+ try { >+ my $encoded_mapping = encode_json( $mapping ); >+ $self->mapping( $encoded_mapping )->store; >+ } >+ catch { >+ Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_"); >+ }; >+ >+ return $self; >+} >+ > =head2 Internal methods > > =head3 _type >diff --git a/t/db_dependent/Koha/Auth/Provider.t b/t/db_dependent/Koha/Auth/Provider.t >index cfe2ccf37a..d55b7bb000 100755 >--- a/t/db_dependent/Koha/Auth/Provider.t >+++ b/t/db_dependent/Koha/Auth/Provider.t >@@ -19,7 +19,12 @@ > > use Modern::Perl; > >-use Test::More tests => 1; >+use Test::More tests => 5; >+ >+use Test::MockModule; >+use Test::Exception; >+ >+use JSON qw(encode_json); > > use Koha::Auth::Providers; > >@@ -35,16 +40,140 @@ subtest 'domains() tests' => sub { > > $schema->storage->txn_begin; > >- my $provider = $builder->build_object({ class => 'Koha::Auth::Providers' }); >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers' } ); > my $domains = $provider->domains; > >- is( ref($domains), 'Koha::Auth::Provider::Domains', 'Type is correct' ); >- is( $domains->count, 0, 'No domains defined' ); >+ is( ref($domains), 'Koha::Auth::Provider::Domains', 'Type is correct' ); >+ is( $domains->count, 0, 'No domains defined' ); > >- $builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } }); >- $builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } }); >+ $builder->build_object( { class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } } ); >+ $builder->build_object( { class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } } ); > > is( $provider->domains->count, 2, 'The provider has 2 domains defined' ); > > $schema->storage->txn_rollback; > }; >+ >+subtest 'get_config() tests' => sub { >+ >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { config => '{' } } ); >+ >+ throws_ok { $provider->get_config() } >+ 'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON'; >+ >+ my $config = { some => 'value', and => 'another' }; >+ $provider->config( encode_json($config) )->store; >+ >+ is_deeply( $provider->get_config, $config, 'Config correctly retrieved' ); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'set_config() tests' => sub { >+ >+ plan tests => 3; >+ >+ $schema->storage->txn_begin; >+ >+ subtest 'OIDC protocol tests' => sub { >+ >+ plan tests => 4; >+ >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'OIDC' } } ); >+ >+ my $config = { >+ key => 'key', >+ secret => 'secret', >+ }; >+ >+ throws_ok { $provider->set_config($config) } >+ 'Koha::Exceptions::MissingParameter', 'Exception thrown on missing parameter'; >+ >+ is( $@->error, 'The well_known_url parameter is mandatory', 'Message is correct' ); >+ >+ $config->{well_known_url} = 'https://koha-community.org/auth'; >+ >+ my $return = $provider->set_config($config); >+ is( ref($return), 'Koha::Auth::Provider', 'Return type is correct' ); >+ >+ is_deeply( $provider->get_config, $config, 'Configuration stored correctly' ); >+ }; >+ >+ subtest 'OAuth protocol tests' => sub { >+ >+ plan tests => 4; >+ >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'OAuth' } } ); >+ >+ my $config = { >+ key => 'key', >+ secret => 'secret', >+ token_url => 'https://koha-community.org/auth/token', >+ }; >+ >+ throws_ok { $provider->set_config($config) } >+ 'Koha::Exceptions::MissingParameter', 'Exception thrown on missing parameter'; >+ >+ is( $@->error, 'The authorize_url parameter is mandatory', 'Message is correct' ); >+ >+ $config->{authorize_url} = 'https://koha-community.org/auth/authorize'; >+ >+ my $return = $provider->set_config($config); >+ is( ref($return), 'Koha::Auth::Provider', 'Return type is correct' ); >+ >+ is_deeply( $provider->get_config, $config, 'Configuration stored correctly' ); >+ }; >+ >+ subtest 'Unsupported protocol tests' => sub { >+ >+ plan tests => 2; >+ >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'CAS' } } ); >+ >+ throws_ok { $provider->set_config() } >+ 'Koha::Exception', 'Exception thrown on unsupported protocol'; >+ >+ like( "$@", qr/Unsupported protocol CAS/, 'Message is correct' ); >+ }; >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'get_mapping() tests' => sub { >+ >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { config => '{' } } ); >+ >+ throws_ok { $provider->get_mapping() } >+ 'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON'; >+ >+ my $mapping = { some => 'value', and => 'another' }; >+ $provider->mapping( encode_json($mapping) )->store; >+ >+ is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' ); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'set_mapping() tests' => sub { >+ >+ plan tests => 1; >+ >+ $schema->storage->txn_begin; >+ >+ my $provider = $builder->build_object( { class => 'Koha::Auth::Providers' } ); >+ >+ my $mapping = { some => 'value', and => 'another' }; >+ $provider->set_mapping($mapping)->store; >+ >+ is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' ); >+ >+ $schema->storage->txn_rollback; >+}; >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 31378
:
141362
|
141363
|
141364
|
141365
|
141366
|
141367
|
141368
|
141369
|
141370
|
141371
|
141372
|
141373
|
141374
|
141375
|
141376
|
141377
|
141378
|
141514
|
141771
|
141772
|
141773
|
141774
|
141775
|
141776
|
141777
|
141778
|
141779
|
141780
|
141781
|
141782
|
141783
|
141784
|
141785
|
141786
|
141787
|
141788
|
141801
|
141802
|
141803
|
141804
|
141805
|
141806
|
141807
|
141808
|
141809
|
141810
|
141811
|
141812
|
141813
|
141814
|
141815
|
141816
|
141817
|
141818
|
141853
|
143056
|
143087
|
143088
|
143089
|
143090
|
143091
|
143092
|
143093
|
143094
|
143095
|
143096
|
143097
|
143098
|
143099
|
143100
|
143101
|
143102
|
143103
|
143104
|
143105
|
143106
|
143107
|
143186
|
143187
|
143188
|
143189
|
143190
|
143191
|
143192
|
143193
|
143194
|
143195
|
143196
|
143197
|
143198
|
143199
|
143200
|
143202
|
143204
|
143205
|
143206
|
143207
|
143208
|
143215
|
143216
|
143217
|
143218
|
143219
|
143220
|
143221
|
143222
|
143223
|
143224
|
143225
|
143226
|
143227
|
143228
|
143229
|
143230
|
143231
|
143232
|
143233
|
143234
|
143235
|
143236
|
143266
|
143267
|
143268
|
143269
|
143270
|
143271
|
143272
|
143273
|
143274
|
143275
|
143277
|
143278
|
143279
|
143280
|
143281
|
143282
|
143283
|
143284
|
143285
|
143286
|
143287
|
143288
|
143396
|
143397
|
143398
|
143399
|
143400
|
143401
|
143402
|
143403
|
143404
|
143405
|
143406
|
143407
|
143408
|
143409
|
143410
|
143411
|
143412
|
143413
|
143414
|
143415
|
143416
|
143417
|
143418
|
143420
|
143421
|
143422
|
143423
|
143424
|
143425
|
143426
|
143427
|
143428
|
143429
|
143430
|
143431
|
143432
|
143433
|
143434
|
143435
|
143436
|
143437
|
143438
|
143439
|
143440
|
143441
|
143442
|
143443
|
143450
|
144071
|
144175