1 package Koha::Auth::Identity::Provider;
3 # Copyright Theke Solutions 2022
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use base qw(Koha::Object);
24 use JSON qw( decode_json encode_json );
27 use Koha::Auth::Identity::Provider::Domains;
29 use Koha::Exceptions::Object;
33 Koha::Auth::Identity::Provider - Koha Auth Provider Object class
41 my $domains = $provider->domains;
43 Returns the related I<Koha::Auth::Identity::Provider::Domains> iterator.
50 return Koha::Auth::Identity::Provider::Domains->_new_from_dbic( scalar $self->_result->domains );
55 my $config = $provider->get_config;
57 Returns a I<hashref> containing the configuration parameters for the provider.
65 return decode_json( $self->config );
68 Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
75 $provider->set_config(
78 secret => 'SECRET_KEY',
79 authorize_url => 'https://provider.example.com/auth',
80 token_url => 'https://provider.example.com/token',
85 $provider->set_config(
88 secret => 'SECRET_KEY',
89 well_known_url => 'https://login.microsoftonline.com/tenant-id/v2.0/.well-known/openid-configuration',
93 This method stores the passed config in JSON format.
98 my ($self, $config) = @_;
100 my @mandatory = $self->mandatory_config_attributes;
102 for my $param (@mandatory) {
103 unless ( defined( $config->{$param} ) ) {
104 Koha::Exceptions::MissingParameter->throw( parameter => $param );
109 my $encoded_config = encode_json($config);
110 $self->config($encoded_config);
112 Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
120 my $mapping = $provider->get_mapping;
122 Returns a I<hashref> containing the attribute mapping for the provider.
130 return decode_json( $self->mapping );
133 Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
139 $provider->mapping( $mapping );
141 This method stores the passed mappings in JSON format.
146 my ($self, $mapping) = @_;
149 my $encoded_mapping = encode_json( $mapping );
150 $self->mapping( $encoded_mapping );
153 Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
161 my $upgraded_object = $provider->upgrade_class
163 Returns a new instance of the object, with the right class.
169 my $protocol = $self->protocol;
171 my $class = $self->protocol_to_class_mapping->{$protocol};
173 Koha::Exception->throw($protocol . ' is not a valid protocol')
176 eval "require $class";
177 return $class->_new_from_dbic( $self->_result );
180 =head2 Internal methods
184 my $json = $provider->to_api;
186 Overloaded method that returns a JSON representation of the Koha::Auth::Identity::Provider object,
187 suitable for API output.
192 my ( $self, $params ) = @_;
194 my $config = $self->get_config;
195 my $mapping = $self->get_mapping;
197 my $json = $self->SUPER::to_api($params);
198 $json->{config} = $config;
199 $json->{mapping} = $mapping;
209 return 'IdentityProvider';
212 =head3 protocol_to_class_mapping
214 my $mapping = Koha::Auth::Identity::Provider::protocol_to_class_mapping
216 Internal method that returns a mapping between I<protocol> codes and
217 implementing I<classes>. To be used by B<upgrade_class>.
221 sub protocol_to_class_mapping {
223 OAuth => 'Koha::Auth::Identity::Provider::OAuth',
224 OIDC => 'Koha::Auth::Identity::Provider::OIDC',
228 =head3 mandatory_config_attributes
230 Stub method for raising exceptions on invalid protocols.
234 sub mandatory_config_attributes {
236 Koha::Exception->throw("This method needs to be subclassed");