View | Details | Raw Unified | Return to bug 30962
Collapse All | Expand All

(-)a/Koha/REST/V1/Auth/Password.pm (+76 lines)
Line 0 Link Here
1
package Koha::REST::V1::Auth::Password;
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 C4::Auth qw/checkpw/;
23
use Koha::Patrons;
24
25
=head1 NAME
26
27
Koha::REST::V1::Auth::Password - Controller library for handling
28
validation of username and password.
29
30
Intended use case is authenticating Koha patrons in external
31
applications via Koha's REST API.
32
33
=head2 Operations
34
35
=head3 validate
36
37
Controller method that checks a patron's password
38
39
=cut
40
41
sub validate {
42
    my $c = shift->openapi->valid_input or return;
43
    my $body   = $c->validation->param('body');
44
    my $username = $body->{username} // '';
45
    my $patron = Koha::Patrons->find({ userid => $username });
46
47
    unless ($patron) {
48
        return $c->render( status => 400, openapi => { error => "Validation failed" } );
49
    }
50
51
    my $password   = $body->{password}   // "";
52
53
    return try {
54
        my ($status, $cardnumber, $userid) = C4::Auth::checkpw($patron->userid, $password );
55
        unless ( $status ) {
56
            return $c->render(
57
                status  => 400,
58
                openapi => { error => "Validation failed" }
59
            );
60
        }
61
62
        return $c->render( status => 204, openapi => '' );
63
    }
64
    catch {
65
        if ( blessed $_ and $_->isa('Koha::Exceptions::Password') ) {
66
            return $c->render(
67
                status  => 400,
68
                openapi => { error => "$_" }
69
            );
70
        }
71
72
        $c->unhandled_exception($_);
73
    };
74
}
75
76
1;
(-)a/api/v1/swagger/paths/auth.yaml (-1 / +56 lines)
Lines 1055-1058 Link Here
1055
          $ref: ../swagger.yaml#/definitions/error
1055
          $ref: ../swagger.yaml#/definitions/error
1056
    x-koha-authorization:
1056
    x-koha-authorization:
1057
      permissions:
1057
      permissions:
1058
        parameters: manage_identity_providers
1058
        parameters: manage_identity_providers
1059
"/auth/password/validation":
1060
  post:
1061
    x-mojo-to: Auth::Password#validate
1062
    operationId: validateUserAndPassword
1063
    tags:
1064
      - patrons
1065
    summary: Check validity of username and password
1066
    parameters:
1067
      - name: body
1068
        in: body
1069
        description: A JSON object containing username and password information
1070
        schema:
1071
          type: object
1072
          properties:
1073
            username:
1074
              description: Username
1075
              type: string
1076
            password:
1077
              description: Password (plain text)
1078
              type: string
1079
          required:
1080
            - username
1081
            - password
1082
          additionalProperties: false
1083
    produces:
1084
      - application/json
1085
    responses:
1086
      "204":
1087
        description: Validation successful
1088
      "400":
1089
        description: Bad request
1090
        schema:
1091
          $ref: ../swagger.yaml#/definitions/error
1092
      "401":
1093
        description: Authentication required
1094
        schema:
1095
          $ref: ../swagger.yaml#/definitions/error
1096
      "403":
1097
        description: Access forbidden
1098
        schema:
1099
          $ref: ../swagger.yaml#/definitions/error
1100
      "500":
1101
        description: |
1102
          Internal server error. Possible `error_code` attribute values:
1103
1104
          * `internal_server_error`
1105
        schema:
1106
          $ref: ../swagger.yaml#/definitions/error
1107
      "503":
1108
        description: Under maintenance
1109
        schema:
1110
          $ref: ../swagger.yaml#/definitions/error
1111
    x-koha-authorization:
1112
      permissions:
1113
        borrowers: "1"
(-)a/api/v1/swagger/swagger.yaml (-1 / +2 lines)
Lines 125-130 paths: Link Here
125
    $ref: "./paths/article_requests.yaml#/~1article_requests~1{article_request_id}"
125
    $ref: "./paths/article_requests.yaml#/~1article_requests~1{article_request_id}"
126
  /auth/otp/token_delivery:
126
  /auth/otp/token_delivery:
127
    $ref: paths/auth.yaml#/~1auth~1otp~1token_delivery
127
    $ref: paths/auth.yaml#/~1auth~1otp~1token_delivery
128
  "/auth/password/validation":
129
    $ref: "./paths/auth.yaml#/~1auth~1password~1validation"
128
  /auth/two-factor/registration:
130
  /auth/two-factor/registration:
129
    $ref: paths/auth.yaml#/~1auth~1two-factor~1registration
131
    $ref: paths/auth.yaml#/~1auth~1two-factor~1registration
130
  /auth/two-factor/registration/verification:
132
  /auth/two-factor/registration/verification:
131
- 

Return to bug 30962