Skip to content

Commit a2bc3d0

Browse files
committed
Add revoke token logic
1 parent 9daa667 commit a2bc3d0

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

ghtoken

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,85 @@ version() {
711711
printf "%s\\n" "${_VERSION}"
712712
}
713713

714+
# revoke ######################################################################
715+
716+
describe "revoke" <<HEREDOC
717+
Usage:
718+
${_ME} revoke [-t|-h|<options>]
719+
720+
Options:
721+
-t | --token <token> Access token to revoke. (Required)
722+
-h | --hostname <hostname> The API URL of GitHub. (Default = api.github.com)
723+
724+
Description:
725+
Revoke the provided access token
726+
HEREDOC
727+
revoke() {
728+
local _token=
729+
local _hostname="api.github.com"
730+
731+
for __arg in "${@:-}"
732+
do
733+
case ${__arg} in
734+
-t|--token)
735+
_token=${2:-}
736+
shift
737+
shift
738+
;;
739+
-h|--hostname)
740+
_hostname=${2:-}
741+
shift
742+
shift
743+
;;
744+
-*)
745+
_exit_1 printf "Unexpected option: %s\\n" "${__arg}"
746+
;;
747+
*)
748+
esac
749+
done
750+
751+
if [[ -z "${_token}" ]]
752+
then
753+
_exit_1 echo "-t | --token <token> is required"
754+
fi
755+
756+
if [[ "${_hostname}" == "api.github.com" ]]
757+
then
758+
759+
# ;-------------------------------------
760+
# ; Get installation id from GitHub.com
761+
# ;-------------------------------------
762+
_api_url="https://${_hostname}"
763+
764+
else
765+
# ;-------------------------------------
766+
# ; Get installation id from GHES
767+
# ;-------------------------------------
768+
_api_url="https://${_hostname}/api/v3"
769+
770+
fi
771+
772+
# Revoke the token
773+
local _response=
774+
_response=$(curl --write-out '%{http_code}' \
775+
--output /dev/null \
776+
--silent \
777+
-X DELETE \
778+
-H "Authorization: Token ""${_token}" \
779+
-H "Accept: application/vnd.github.v3+json" \
780+
"${_api_url}"/installation/token | \
781+
jq)
782+
783+
if [[ "${_response}" == 204 ]]
784+
then
785+
echo ${_response}": Token revoked successfully"
786+
else
787+
echo ${_response}": Failed to revoke token. Maybe it expired already?"
788+
fi
789+
}
790+
791+
792+
714793
# installations ###############################################################
715794

716795
describe "installations" <<HEREDOC
@@ -755,7 +834,7 @@ installations() {
755834
shift
756835
shift
757836
;;
758-
-h|--hostname)
837+
-o|--hostname)
759838
_hostname=${2:-}
760839
shift
761840
shift
@@ -923,7 +1002,7 @@ generate() {
9231002
shift
9241003
shift
9251004
;;
926-
-h|--hostname)
1005+
-o|--hostname)
9271006
_hostname=${2:-}
9281007
shift
9291008
shift

0 commit comments

Comments
 (0)