Bug 30914: (24153 follow-up) Fix cleanup_database.pl --transfers --old-reserves
[koha.git] / Koha / ApiKey.pm
1 package Koha::ApiKey;
2
3 # Copyright BibLibre 2015
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 Carp;
23
24 use Koha::AuthUtils qw(hash_password);
25 use Koha::Exceptions::Object;
26
27 use List::MoreUtils qw(any);
28 use UUID;
29
30 use base qw(Koha::Object);
31
32 =head1 NAME
33
34 Koha::ApiKey - Koha API Key Object class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 store
41
42     my $api_key = Koha::ApiKey->new({ patron_id => $patron_id })->store;
43
44 Overloaded I<store> method.
45
46 =cut
47
48 sub store {
49     my ($self) = @_;
50
51     if ( $self->in_storage ) {
52         my %dirty_columns = $self->_result->get_dirty_columns;
53
54         # only allow 'description' and 'active' to be updated
55         for my $property ( keys %dirty_columns ) {
56             Koha::Exceptions::Object::ReadOnlyProperty->throw( property => $property )
57               if $property ne 'description' and $property ne 'active';
58         }
59     } else {
60         $self->{_plain_text_secret} = $self->_generate_unused_uuid('secret');
61         $self->set(
62             {   secret    => Koha::AuthUtils::hash_password( $self->{_plain_text_secret} ),
63                 client_id => $self->_generate_unused_uuid('client_id'),
64             }
65         );
66     }
67
68     return $self->SUPER::store();
69 }
70
71 =head3 validate_secret
72
73     if ( $api_key->validate_secret( $secret ) ) { ... }
74
75 Returns a boolean that tells if the passed secret matches the one on the DB.
76
77 =cut
78
79 sub validate_secret {
80     my ( $self, $secret ) = @_;
81
82     my $digest = Koha::AuthUtils::hash_password( $secret, $self->secret );
83
84     return ( $self->secret eq $digest ) ? 1 : 0;
85 }
86
87 =head3 plain_text_secret
88
89     my $generated_secret = $api_key->store->plain_text_secret;
90
91 Returns the generated I<secret> so it can be displayed to  the end user.
92 This is only accessible when the object is new and has just been stored.
93
94 Returns I<undef> if the object was retrieved from the database.
95
96 =cut
97
98 sub plain_text_secret {
99     my ($self) = @_;
100
101     return $self->{_plain_text_secret}
102         if $self->{_plain_text_secret};
103
104     return;
105 }
106
107 =head2 Internal methods
108
109 =cut
110
111 =head3 _type
112
113 =cut
114
115 sub _type {
116     return 'ApiKey';
117 }
118
119 =head3 _generate_unused_uuid
120
121     my $string = $self->_generate_unused_uuid($column);
122
123 $column can be 'client_id' or 'secret'.
124
125 =cut
126
127 sub _generate_unused_uuid {
128     my ($self, $column) = @_;
129
130     my ( $uuid, $uuidstring );
131
132     UUID::generate($uuid);
133     UUID::unparse( $uuid, $uuidstring );
134
135     while ( Koha::ApiKeys->search({ $column => $uuidstring })->count > 0 ) {
136         # Make sure $secret is unique
137         UUID::generate($uuid);
138         UUID::unparse( $uuid, $uuidstring );
139     }
140
141     return $uuidstring;
142 }
143
144 1;