|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/bin/bash |
|
|
2 |
# |
| 3 |
# koha-api-keys: generate or delete api keys for a given patron and |
| 4 |
# koha instance. |
| 5 |
# Copyright 2024 LMSCloud GmbH |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it 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 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but 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 this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
set -e |
| 21 |
umask 0077 |
| 22 |
|
| 23 |
# include helper functions |
| 24 |
if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then |
| 25 |
. "/usr/share/koha/bin/koha-functions.sh" |
| 26 |
else |
| 27 |
echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2 |
| 28 |
exit 1 |
| 29 |
fi |
| 30 |
|
| 31 |
usage() { |
| 32 |
local scriptname=$0 |
| 33 |
cat <<EOF |
| 34 |
Generate, or delete api keys for specified patron. |
| 35 |
|
| 36 |
Usage: $scriptname generate|delete [ description ] [ borrowernumber ] [ instancename ] |
| 37 |
|
| 38 |
EOF |
| 39 |
} |
| 40 |
|
| 41 |
op="$1" |
| 42 |
|
| 43 |
[ "$#" -ge 2 ] || ( |
| 44 |
usage |
| 45 |
die 'Missing required arguments.' |
| 46 |
) |
| 47 |
|
| 48 |
if [ "$op" == 'generate' ]; then |
| 49 |
if [ "$#" -eq 3 ]; then |
| 50 |
description=$(openssl rand -hex 16) |
| 51 |
borrowernumber="$2" |
| 52 |
instance="$3" |
| 53 |
fi |
| 54 |
|
| 55 |
if [ "$#" -eq 4 ]; then |
| 56 |
description="$2" |
| 57 |
borrowernumber="$3" |
| 58 |
instance="$4" |
| 59 |
fi |
| 60 |
|
| 61 |
is_instance "$instance" || ( |
| 62 |
usage |
| 63 |
die "Unknown instance '$instance'" |
| 64 |
) |
| 65 |
|
| 66 |
if ! perl -MKoha::Patrons -e "Koha::Patrons->find($borrowernumber) or die;" 2>/dev/null; then |
| 67 |
die "Unknown borrowernumber: '$borrowernumber'" |
| 68 |
fi |
| 69 |
|
| 70 |
{ |
| 71 |
read -r client_id |
| 72 |
read -r secret |
| 73 |
} < <( |
| 74 |
perl -MKoha::ApiKeys -e "my \$key = Koha::ApiKey->new( { patron_id => $borrowernumber, description => '$description' } )->store; printf \"%s\n%s\n\", \$key->client_id, \$key->plain_text_secret;" |
| 75 |
) |
| 76 |
|
| 77 |
printf "Client ID:\t%s\nClient Secret:\t%s\n" "$client_id" "$secret" |
| 78 |
|
| 79 |
exit 0 |
| 80 |
fi |
| 81 |
|
| 82 |
if [ "$op" == 'delete' ]; then |
| 83 |
instance="$2" |
| 84 |
is_instance "$instance" || ( |
| 85 |
usage |
| 86 |
die "Unknown instance '$instance'" |
| 87 |
) |
| 88 |
|
| 89 |
read -rp "Client ID to delete: " client_id |
| 90 |
if ! perl -MKoha::ApiKeys -e "Koha::ApiKeys->find('$client_id')->delete or die;" 2>/dev/null; then |
| 91 |
die "Unknown client id: '$client_id'" |
| 92 |
fi |
| 93 |
|
| 94 |
exit 0 |
| 95 |
fi |
| 96 |
|
| 97 |
usage |
| 98 |
die "Invalid operation: '$op'" |