]> git.koha-community.org Git - koha.git/blob - Koha/REST/V1/Auth/Identity/Providers.pm
Bug 33859: Use the phrase 'Identity providers' instead of 'Authentication providers'
[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 identity providers routes.
35
36 =head2 Operations
37
38 =head3 list
39
40 Controller method for listing identity 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 identity 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 identity 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                                      ->set_config( $config )
113                                      ->set_mapping( $mapping )
114                                      ->store;
115
116                 $c->res->headers->location( $c->req->url->to_string . '/' . $provider->identity_provider_id );
117                 return $c->render(
118                     status  => 201,
119                     openapi => $provider->to_api
120                 );
121             }
122         );
123     }
124     catch {
125         if ( blessed($_) ) {
126             if ( $_->isa('Koha::Exceptions::MissingParameter') ) {
127                 return $c->render(
128                     status  => 400,
129                     openapi => {
130                         error      => "Missing parameter config." . $_->parameter,
131                         error_code => 'missing_parameter'
132                     }
133                 );
134             }
135         }
136
137         $c->unhandled_exception($_);
138     };
139 }
140
141 =head3 update
142
143 Controller method for updating an identity provider.
144
145 =cut
146
147 sub update {
148     my $c = shift->openapi->valid_input or return;
149
150     my $identity_provider_id = $c->validation->param('identity_provider_id');
151     my $provider = Koha::Auth::Identity::Providers->find( $identity_provider_id );
152
153     unless ( $provider ) {
154         return $c->render(
155             status  => 404,
156             openapi => {
157                 error      => 'Object not found',
158                 error_code => 'not_found',
159             }
160         );
161     }
162
163     return try {
164
165         Koha::Database->new->schema->txn_do(
166             sub {
167
168                 my $body = $c->validation->param('body');
169
170                 my $config   = delete $body->{config};
171                 my $mapping  = delete $body->{mapping};
172
173                 $provider = $provider->set_from_api( $body )->upgrade_class;
174
175                 $provider->set_config( $config )
176                          ->set_mapping( $mapping )
177                          ->store;
178
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 identity 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;