Bug 16223: Tidy
[koha.git] / Koha / Auth / Identity / Provider.pm
1 package Koha::Auth::Identity::Provider;
2
3 # Copyright Theke Solutions 2022
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use base qw(Koha::Object);
23
24 use JSON qw( decode_json encode_json );
25 use Try::Tiny;
26
27 use Koha::Auth::Identity::Provider::Domains;
28 use Koha::Exceptions;
29 use Koha::Exceptions::Object;
30
31 =head1 NAME
32
33 Koha::Auth::Identity::Provider - Koha Auth Provider Object class
34
35 =head1 API
36
37 =head2 Class methods
38
39 =head3 domains
40
41     my $domains = $provider->domains;
42
43 Returns the related I<Koha::Auth::Identity::Provider::Domains> iterator.
44
45 =cut
46
47 sub domains {
48     my ($self) = @_;
49
50     return Koha::Auth::Identity::Provider::Domains->_new_from_dbic( scalar $self->_result->domains );
51 }
52
53 =head3 get_config
54
55     my $config = $provider->get_config;
56
57 Returns a I<hashref> containing the configuration parameters for the provider.
58
59 =cut
60
61 sub get_config {
62     my ($self) = @_;
63
64     return try {
65         return decode_json( $self->config );
66     }
67     catch {
68         Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
69     };
70 }
71
72 =head3 set_config
73
74     # OAuth
75     $provider->set_config(
76         {
77             key           => 'APP_ID',
78             secret        => 'SECRET_KEY',
79             authorize_url => 'https://provider.example.com/auth',
80             token_url     => 'https://provider.example.com/token',
81         }
82     );
83
84     # OIDC
85     $provider->set_config(
86         {
87             key           => 'APP_ID',
88             secret        => 'SECRET_KEY',
89             well_known_url => 'https://login.microsoftonline.com/tenant-id/v2.0/.well-known/openid-configuration',
90         }
91     );
92
93 This method stores the passed config in JSON format.
94
95 =cut
96
97 sub set_config {
98     my ($self, $config) = @_;
99
100     my @mandatory = $self->mandatory_config_attributes;
101
102     for my $param (@mandatory) {
103         unless ( defined( $config->{$param} ) ) {
104             Koha::Exceptions::MissingParameter->throw( parameter => $param );
105         }
106     }
107
108     try {
109         my $encoded_config = encode_json($config);
110         $self->config($encoded_config);
111     } catch {
112         Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
113     };
114
115     return $self;
116 }
117
118 =head3 get_mapping
119
120     my $mapping = $provider->get_mapping;
121
122 Returns a I<hashref> containing the attribute mapping for the provider.
123
124 =cut
125
126 sub get_mapping {
127     my ($self) = @_;
128
129     return try {
130         return decode_json( $self->mapping );
131     }
132     catch {
133         Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
134     };
135 }
136
137 =head3 set_mapping
138
139     $provider->mapping( $mapping );
140
141 This method stores the passed mappings in JSON format.
142
143 =cut
144
145 sub set_mapping {
146     my ($self, $mapping) = @_;
147
148     try {
149         my $encoded_mapping = encode_json( $mapping );
150         $self->mapping( $encoded_mapping );
151     }
152     catch {
153         Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
154     };
155
156     return $self;
157 }
158
159 =head3 upgrade_class
160
161     my $upgraded_object = $provider->upgrade_class
162
163 Returns a new instance of the object, with the right class.
164
165 =cut
166
167 sub upgrade_class {
168     my ( $self ) = @_;
169     my $protocol = $self->protocol;
170
171     my $class = $self->protocol_to_class_mapping->{$protocol};
172
173     Koha::Exception->throw($protocol . ' is not a valid protocol')
174         unless $class;
175
176     eval "require $class";
177     return $class->_new_from_dbic( $self->_result );
178 }
179
180 =head2 Internal methods
181
182 =head3 to_api
183
184     my $json = $provider->to_api;
185
186 Overloaded method that returns a JSON representation of the Koha::Auth::Identity::Provider object,
187 suitable for API output.
188
189 =cut
190
191 sub to_api {
192     my ( $self, $params ) = @_;
193
194     my $config  = $self->get_config;
195     my $mapping = $self->get_mapping;
196
197     my $json = $self->SUPER::to_api($params);
198     $json->{config}  = $config;
199     $json->{mapping} = $mapping;
200
201     return $json;
202 }
203
204 =head3 _type
205
206 =cut
207
208 sub _type {
209     return 'IdentityProvider';
210 }
211
212 =head3 protocol_to_class_mapping
213
214     my $mapping = Koha::Auth::Identity::Provider::protocol_to_class_mapping
215
216 Internal method that returns a mapping between I<protocol> codes and
217 implementing I<classes>. To be used by B<upgrade_class>.
218
219 =cut
220
221 sub protocol_to_class_mapping {
222     return {
223         OAuth => 'Koha::Auth::Identity::Provider::OAuth',
224         OIDC  => 'Koha::Auth::Identity::Provider::OIDC',
225     };
226 }
227
228 =head3 mandatory_config_attributes
229
230 Stub method for raising exceptions on invalid protocols.
231
232 =cut
233
234 sub mandatory_config_attributes {
235     my ($self) = @_;
236     Koha::Exception->throw("This method needs to be subclassed");
237 }
238
239 1;