1 package Koha::REST::V1::Auth::Identity::Providers;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Auth::Identity::Provider::OAuth;
23 use Koha::Auth::Identity::Provider::OIDC;
24 use Koha::Auth::Identity::Providers;
28 use Scalar::Util qw(blessed);
33 Koha::REST::V1::Auth::Identity::Providers - Controller library for handling
34 identity providers routes.
40 Controller method for listing identity providers.
45 my $c = shift->openapi->valid_input or return;
48 my $providers_rs = Koha::Auth::Identity::Providers->new;
51 openapi => $c->objects->search($providers_rs)
54 $c->unhandled_exception($_);
60 Controller method for retrieving an identity provider.
65 my $c = shift->openapi->valid_input or return;
69 my $identity_provider_id = $c->validation->param('identity_provider_id');
70 my $provider = $c->objects->find( Koha::Auth::Identity::Providers->new, $identity_provider_id );
72 unless ( $provider ) {
76 error => 'Object not found',
77 error_code => 'not_found',
82 return $c->render( status => 200, openapi => $provider );
85 $c->unhandled_exception($_);
91 Controller method for adding an identity provider.
96 my $c = shift->openapi->valid_input or return;
100 Koha::Database->new->schema->txn_do(
103 my $body = $c->validation->param('body');
105 my $config = delete $body->{config};
106 my $mapping = delete $body->{mapping};
107 my $protocol = delete $body->{protocol};
109 my $class = Koha::Auth::Identity::Provider::protocol_to_class_mapping->{$protocol};
111 my $provider = $class->new_from_api( $body )
112 ->set_config( $config )
113 ->set_mapping( $mapping )
116 $c->res->headers->location( $c->req->url->to_string . '/' . $provider->identity_provider_id );
119 openapi => $provider->to_api
126 if ( $_->isa('Koha::Exceptions::MissingParameter') ) {
130 error => "Missing parameter config." . $_->parameter,
131 error_code => 'missing_parameter'
137 $c->unhandled_exception($_);
143 Controller method for updating an identity provider.
148 my $c = shift->openapi->valid_input or return;
150 my $identity_provider_id = $c->validation->param('identity_provider_id');
151 my $provider = Koha::Auth::Identity::Providers->find( $identity_provider_id );
153 unless ( $provider ) {
157 error => 'Object not found',
158 error_code => 'not_found',
165 Koha::Database->new->schema->txn_do(
168 my $body = $c->validation->param('body');
170 my $config = delete $body->{config};
171 my $mapping = delete $body->{mapping};
173 $provider = $provider->set_from_api( $body )->upgrade_class;
175 $provider->set_config( $config )
176 ->set_mapping( $mapping )
179 $provider->discard_changes;
183 openapi => $provider->to_api
190 if ( $_->isa('Koha::Exceptions::MissingParameter') ) {
194 error => "Missing parameter config." . $_->parameter,
195 error_code => 'missing_parameter'
201 $c->unhandled_exception($_);
207 Controller method for deleting an identity provider.
212 my $c = shift->openapi->valid_input or return;
214 my $provider = Koha::Auth::Identity::Providers->find( $c->validation->param('identity_provider_id') );
215 unless ( $provider ) {
219 error => 'Object not found',
220 error_code => 'not_found',
233 $c->unhandled_exception($_);