Bug 31378: Rename Auth Provider to Identity Provider and add Client.t tests
[koha.git] / Koha / REST / V1 / Auth / Identity / Providers.pm
1 package Koha::REST::V1::Auth::Identity::Providers;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Auth::Identity::Provider::OAuth;
23 use Koha::Auth::Identity::Provider::OIDC;
24 use Koha::Auth::Identity::Providers;
25
26 use Koha::Database;
27
28 use Scalar::Util qw(blessed);
29 use Try::Tiny;
30
31 =head1 NAME
32
33 Koha::REST::V1::Auth::Identity::Providers - Controller library for handling
34 authentication providers routes.
35
36 =head2 Operations
37
38 =head3 list
39
40 Controller method for listing authentication providers.
41
42 =cut
43
44 sub list {
45     my $c = shift->openapi->valid_input or return;
46
47     return try {
48         my $providers_rs = Koha::Auth::Identity::Providers->new;
49         return $c->render(
50             status  => 200,
51             openapi => $c->objects->search($providers_rs)
52         );
53     } catch {
54         $c->unhandled_exception($_);
55     };
56 }
57
58 =head3 get
59
60 Controller method for retrieving an authentication provider.
61
62 =cut
63
64 sub get {
65     my $c = shift->openapi->valid_input or return;
66
67     return try {
68
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 );
71
72         unless ( $provider ) {
73             return $c->render(
74                 status  => 404,
75                 openapi => {
76                     error      => 'Object not found',
77                     error_code => 'not_found',
78                 }
79             );
80         }
81
82         return $c->render( status => 200, openapi => $provider );
83     }
84     catch {
85         $c->unhandled_exception($_);
86     }
87 }
88
89 =head3 add
90
91 Controller method for adding an authentication provider.
92
93 =cut
94
95 sub add {
96     my $c = shift->openapi->valid_input or return;
97
98     return try {
99
100         Koha::Database->new->schema->txn_do(
101             sub {
102
103                 my $body = $c->validation->param('body');
104
105                 my $config   = delete $body->{config};
106                 my $mapping  = delete $body->{mapping};
107                 my $protocol = delete $body->{protocol};
108
109                 my $class = Koha::Auth::Identity::Provider::protocol_to_class_mapping->{$protocol};
110
111                 my $provider = $class->new_from_api( $body );
112                 $provider->store;
113
114                 $provider->set_config( $config );
115                 $provider->set_mapping( $mapping );
116
117                 $c->res->headers->location( $c->req->url->to_string . '/' . $provider->identity_provider_id );
118                 return $c->render(
119                     status  => 201,
120                     openapi => $provider->to_api
121                 );
122             }
123         );
124     }
125     catch {
126         if ( blessed($_) ) {
127             if ( $_->isa('Koha::Exceptions::MissingParameter') ) {
128                 return $c->render(
129                     status  => 400,
130                     openapi => {
131                         error      => "Missing parameter config." . $_->parameter,
132                         error_code => 'missing_parameter'
133                     }
134                 );
135             }
136         }
137
138         $c->unhandled_exception($_);
139     };
140 }
141
142 =head3 update
143
144 Controller method for updating an authentication provider.
145
146 =cut
147
148 sub update {
149     my $c = shift->openapi->valid_input or return;
150
151     my $identity_provider_id = $c->validation->param('identity_provider_id');
152     my $provider = Koha::Auth::Identity::Providers->find( $identity_provider_id );
153
154     unless ( $provider ) {
155         return $c->render(
156             status  => 404,
157             openapi => {
158                 error      => 'Object not found',
159                 error_code => 'not_found',
160             }
161         );
162     }
163
164     return try {
165
166         Koha::Database->new->schema->txn_do(
167             sub {
168
169                 my $body = $c->validation->param('body');
170
171                 my $config   = delete $body->{config};
172                 my $mapping  = delete $body->{mapping};
173
174                 $provider = $provider->set_from_api( $body )->upgrade_class;
175
176                 $provider->set_config( $config );
177                 $provider->set_mapping( $mapping );
178                 # set_config and set_mapping already called store()
179                 $provider->discard_changes;
180
181                 return $c->render(
182                     status  => 200,
183                     openapi => $provider->to_api
184                 );
185             }
186         );
187     }
188     catch {
189         if ( blessed($_) ) {
190             if ( $_->isa('Koha::Exceptions::MissingParameter') ) {
191                 return $c->render(
192                     status  => 400,
193                     openapi => {
194                         error      => "Missing parameter config." . $_->parameter,
195                         error_code => 'missing_parameter'
196                     }
197                 );
198             }
199         }
200
201         $c->unhandled_exception($_);
202     };
203 }
204
205 =head3 delete
206
207 Controller method for deleting an authentication provider.
208
209 =cut
210
211 sub delete {
212     my $c = shift->openapi->valid_input or return;
213
214     my $provider = Koha::Auth::Identity::Providers->find( $c->validation->param('identity_provider_id') );
215     unless ( $provider ) {
216         return $c->render(
217             status  => 404,
218             openapi => {
219                 error      => 'Object not found',
220                 error_code => 'not_found',
221             }
222         );
223     }
224
225     return try {
226         $provider->delete;
227         return $c->render(
228             status  => 204,
229             openapi => q{}
230         );
231     }
232     catch {
233         $c->unhandled_exception($_);
234     };
235 }
236
237 1;