Revert "Bug 22026: Removed 'use Modern::Perl;' from Koha::REST::classes"
[koha.git] / Koha / REST / V1 / OAuth.pm
1 package Koha::REST::V1::OAuth;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Module::Load::Conditional;
21
22 use C4::Context;
23 use Koha::OAuth;
24
25 use Mojo::Base 'Mojolicious::Controller';
26
27 =head1 NAME
28
29 Koha::REST::V1::OAuth - Controller library for handling OAuth2-related token handling
30
31 =head2 Operations
32
33 =head3 token
34
35 Controller method handling token requests
36
37 =cut
38
39 sub token {
40
41     my $c = shift->openapi->valid_input or return;
42
43     if ( Module::Load::Conditional::can_load(
44                 modules => {'Net::OAuth2::AuthorizationServer' => undef} )) {
45         require Net::OAuth2::AuthorizationServer;
46     }
47     else {
48         return $c->render( status => 400, openapi => { error => 'Unimplemented grant type' } );
49     }
50
51     my $grant_type = $c->validation->param('grant_type');
52     unless ( $grant_type eq 'client_credentials' and C4::Context->preference('RESTOAuth2ClientCredentials') ) {
53         return $c->render(status => 400, openapi => {error => 'Unimplemented grant type'});
54     }
55
56     my $client_id = $c->validation->param('client_id');
57     my $client_secret = $c->validation->param('client_secret');
58
59     my $cb = "${grant_type}_grant";
60     my $server = Net::OAuth2::AuthorizationServer->new;
61     my $grant = $server->$cb(Koha::OAuth::config);
62
63     # verify a client against known clients
64     my ( $is_valid, $error ) = $grant->verify_client(
65         client_id     => $client_id,
66         client_secret => $client_secret,
67     );
68
69     unless ($is_valid) {
70         return $c->render(status => 403, openapi => {error => $error});
71     }
72
73     # generate a token
74     my $token = $grant->token(
75         client_id => $client_id,
76         type      => 'access',
77     );
78
79     # store access token
80     my $expires_in = 3600;
81     $grant->store_access_token(
82         client_id    => $client_id,
83         access_token => $token,
84         expires_in   => $expires_in,
85     );
86
87     my $response = {
88         access_token => $token,
89         token_type => 'Bearer',
90         expires_in => $expires_in,
91     };
92
93     return $c->render(status => 200, openapi => $response);
94 }
95
96 1;