From cc1e948be2befdb8be7c82db17095121d21164c3 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Tue, 4 Jun 2024 16:25:16 +0200 Subject: [PATCH] Bug 37025: Add CLI tool to generate/delete api keys for a given patron on a Koha instance To test: 1) Apply the patch. 2) Suggestion: `cp debian/scripts/koha-api-keys /usr/sbin/`. 3) Familiarize yourself with the tool by just running `koha-api-keys`. 4) Test that you can generate an api key with the generate op. 5) Test that you can delete an api key with the delete op. 6) Run `xsltproc /usr/share/xml/docbook/stylesheet/docbook-xsl-ns/manpages/docbook.xsl debian/docs/koha-api-keys.xml`. 7) Use man to verify that the manpage renders as expected (install man if not present, e.g. koha-testing-docker). 8) Run the syntax test for the docbook files `prove -v xt/verify-debian-docbook.t`. 9) Sign off. Or leave a comment on what you thinks needs to be improved. --- debian/docs/koha-common.xml | 7 +++ debian/koha-common.install | 1 + debian/scripts/koha-api-keys | 98 ++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100755 debian/scripts/koha-api-keys diff --git a/debian/docs/koha-common.xml b/debian/docs/koha-common.xml index 5060dec2ac..dc88dc21bb 100644 --- a/debian/docs/koha-common.xml +++ b/debian/docs/koha-common.xml @@ -233,6 +233,13 @@ This command behaves something like su(1), but provides you with a shell as the koha user, and with the environment pointing to the right places. + + + + + Create an api key for a given patron on a koha instance or delete one based on its client id. + + diff --git a/debian/koha-common.install b/debian/koha-common.install index 2b8c0f4646..23c9b719f1 100644 --- a/debian/koha-common.install +++ b/debian/koha-common.install @@ -7,6 +7,7 @@ debian/unavailable.html usr/share/koha/intranet/htdocs debian/unavailable.html usr/share/koha/opac/htdocs debian/templates/* etc/koha debian/scripts/koha-functions.sh usr/share/koha/bin +debian/scripts/koha-api-keys usr/sbin debian/scripts/koha-create usr/sbin debian/scripts/koha-create-dirs usr/sbin debian/scripts/koha-disable usr/sbin diff --git a/debian/scripts/koha-api-keys b/debian/scripts/koha-api-keys new file mode 100755 index 0000000000..774912e297 --- /dev/null +++ b/debian/scripts/koha-api-keys @@ -0,0 +1,98 @@ +#!/bin/bash +# +# koha-api-keys: generate or delete api keys for a given patron and +# koha instance. +# Copyright 2024 LMSCloud GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e +umask 0077 + +# include helper functions +if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then + . "/usr/share/koha/bin/koha-functions.sh" +else + echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2 + exit 1 +fi + +usage() { + local scriptname=$0 + cat <find($borrowernumber) or die;" 2>/dev/null; then + die "Unknown borrowernumber: '$borrowernumber'" + fi + + { + read -r client_id + read -r secret + } < <( + 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;" + ) + + printf "Client ID:\t%s\nClient Secret:\t%s\n" "$client_id" "$secret" + + exit 0 +fi + +if [ "$op" == 'delete' ]; then + instance="$2" + is_instance "$instance" || ( + usage + die "Unknown instance '$instance'" + ) + + read -rp "Client ID to delete: " client_id + if ! perl -MKoha::ApiKeys -e "Koha::ApiKeys->find('$client_id')->delete or die;" 2>/dev/null; then + die "Unknown client id: '$client_id'" + fi + + exit 0 +fi + +usage +die "Invalid operation: '$op'" -- 2.45.2