Skip to content

Commit

Permalink
gupload: Rewrite logic for access token usage | Fix #113
Browse files Browse the repository at this point in the history
launch a background service to check access token and update it

checks ACCESS_TOKEN_EXPIRY, try to update before 5 mins of expiry, a fresh token gets 60 mins ( 3600 seconds )

process will be killed when script exits

dont update config in bg service, update at script exit

create a temp file where updated access token will be stored by the bg service

every function that uses access token will source it on every call

make a new function named _api_request for all oauth network calls

decrease a network request ( to fetch expiry of access token, just calculate it locally using remaining time given in json as expires_in )

google-oauth2.[bash|sh]: Apply new changes
  • Loading branch information
Akianonymus committed Oct 28, 2020
1 parent 34519d4 commit 7ee53fe
Show file tree
Hide file tree
Showing 10 changed files with 537 additions and 387 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ This repo contains two types of scripts, posix compatible and bash compatible.
| sed | Miscellaneous |
| mktemp | To generate temporary files ( optional ) |
| sleep | Self explanatory |
| ps | To manage different processes |

<strong>If BASH is not available or BASH is available but version is less tham 4.x, then below programs are also required:</strong>

Expand All @@ -124,11 +125,10 @@ This repo contains two types of scripts, posix compatible and bash compatible.
| cat | Miscellaneous |
| stty or zsh or tput | To determine column size ( optional ) |

<strong>These programs are needed for synchronisation script:</strong>
<strong>These are the additional programs needed for synchronisation script:</strong>

| Program | Role In Script |
| ------------- | ------------------------- |
| ps | To manage background jobs |
| tail | To show indefinite logs |

### Installation
Expand Down
138 changes: 66 additions & 72 deletions bash/drive-utils.bash

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions bash/google-oauth2.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ Usage:
exit 0
}

###################################################
# Method to regenerate access_token ( also updates in config ).
# Make a request on https://www.googleapis.com/oauth2/""${API_VERSION}""/tokeninfo?access_token=${ACCESS_TOKEN} url and check if the given token is valid, if not generate one.
# Requirements: Refresh Token
_get_token_and_update() {
# Globals: 8 variables, 2 functions
# Variables - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, TOKEN_URL, CONFIG, API_URL, API_VERSION and QUIET
# Functions - _update_config and _print_center
# Result: Update access_token and expiry else print error
###################################################
_get_access_token_and_update() {
RESPONSE="${1:-$(curl --compressed -s -X POST --data "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" "${TOKEN_URL}")}" || :
if ACCESS_TOKEN="$(_json_value access_token 1 1 <<< "${RESPONSE}")"; then
{ [[ -n ${UPDATE} ]] && ACCESS_TOKEN_EXPIRY="$(curl --compressed -s "${API_URL}/oauth2/${API_VERSION}/tokeninfo?access_token=${ACCESS_TOKEN}" | _json_value exp 1 1)" &&
_update_config ACCESS_TOKEN_EXPIRY "${ACCESS_TOKEN_EXPIRY}" "${CONFIG}"; } || { "${QUIET:-_print_center}" "justify" "Error: Couldn't update" " access token expiry." 1>&2 && exit 1; }
"${UPDATE:-:}" ACCESS_TOKEN "${ACCESS_TOKEN}" "${CONFIG}"
"${UPDATE:-:}" ACCESS_TOKEN_EXPIRY "${ACCESS_TOKEN_EXPIRY}" "${CONFIG}"
[[ -n ${UPDATE} ]] && {
_update_config ACCESS_TOKEN "${ACCESS_TOKEN}" "${CONFIG}"
_update_config ACCESS_TOKEN_EXPIRY "$(($(printf "%(%s)T\\n" "-1") + $(_json_value expires_in 1 1 <<< "${RESPONSE}")))" "${CONFIG}"
}
else
_print_center "justify" "Error: Something went wrong" ", printing error." 1>&2
printf "%s\n" "${RESPONSE}" 1>&2
Expand All @@ -45,19 +50,17 @@ _get_token_and_update() {

[[ ${1} = create ]] || [[ ${1} = refresh ]] || _short_help

[[ ${2} = update ]] && UPDATE="_update_config"
{ [[ ${2} = update ]] && UPDATE="true"; } || unset UPDATE

UTILS_FOLDER="${UTILS_FOLDER:-$(pwd)}"
{ . "${UTILS_FOLDER}"/common-utils.sh; } || { printf "Error: Unable to source util files.\n" && exit 1; }
{ . "${UTILS_FOLDER}"/common-utils.bash; } || { printf "Error: Unable to source util files.\n" && exit 1; }

_check_debug

_print_center "justify" "Starting script.." "-"

CLIENT_ID=""
CLIENT_SECRET=""
API_URL="https://www.googleapis.com"
API_VERSION="v3"
SCOPE="https://www.googleapis.com/auth/drive"
REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob"
TOKEN_URL="https://accounts.google.com/o/oauth2/token"
Expand All @@ -68,7 +71,7 @@ CONFIG="${CONFIG:-${HOME}/.googledrive.conf}"
# shellcheck source=/dev/null
[[ -f ${CONFIG} ]] && source "${CONFIG}"

! _is_terminal && [[ -z ${CLIENT_ID:+${CLIENT_SECRET:+${REFRESH_TOKEN}}} ]] && {
! [[ -t 2 ]] && [[ -z ${CLIENT_ID:+${CLIENT_SECRET:+${REFRESH_TOKEN}}} ]] && {
printf "%s\n" "Error: Script is not running in a terminal, cannot ask for credentials."
printf "%s\n" "Add in config manually if terminal is not accessible. CLIENT_ID, CLIENT_SECRET and REFRESH_TOKEN is required." && return 1
}
Expand Down Expand Up @@ -103,15 +106,15 @@ if [[ ${1} = create ]]; then
--data "code=${CODE}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&redirect_uri=${REDIRECT_URI}&grant_type=authorization_code" "${TOKEN_URL}")" || :

REFRESH_TOKEN="$(_json_value refresh_token 1 1 <<< "${RESPONSE}" || :)"
if _get_token_and_update "${RESPONSE}"; then
if _get_access_token_and_update "${RESPONSE}"; then
"${UPDATE:-:}" REFRESH_TOKEN "${REFRESH_TOKEN}" "${CONFIG}"
printf "Access Token: %s\n" "${ACCESS_TOKEN}"
printf "Refresh Token: %s\n" "${REFRESH_TOKEN}"
fi
elif [[ ${1} = refresh ]]; then
if [[ -n ${REFRESH_TOKEN} ]]; then
_print_center "justify" "Required credentials set." "="
_get_token_and_update
_get_access_token_and_update
_clear_line 1
printf "Access Token: %s\n" "${ACCESS_TOKEN}"
else
Expand Down
Loading

0 comments on commit 7ee53fe

Please sign in to comment.