Skip to content

Commit 769d825

Browse files
committed
[BUGFIX] auth-utils: Use loopback method to fetch authorizaton code | Fix labbots#183
It will use either netcat or python3 to start the http server Now no need to copy paste the authorizaton ccode manually https://developers.google.com/identity/protocols/oauth2/native-app#create-code-challenge
1 parent 080e807 commit 769d825

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

src/common/auth-utils.sh

+63-19
Original file line numberDiff line numberDiff line change
@@ -336,30 +336,74 @@ _check_refresh_token() {
336336
"${QUIET:-_print_center}" "normal" " No Refresh token given, follow below steps to generate.. " "-" && unset refresh_token_value_check_refresh_token
337337
fi
338338

339+
server_string_check_refresh_token='Now go back to command line..'
340+
server_port_check_refresh_token='8079'
341+
# run a loop until an open port has been found
342+
# check for 50 ports
343+
while :; do
344+
: "$((server_port_check_refresh_token += 1))"
345+
if [ "${server_port_check_refresh_token}" -gt 8130 ]; then
346+
"${QUIET:-_print_center}" "normal" "Error: No open ports found ( 8080 to 8130 )." "-"
347+
return 1
348+
fi
349+
{ curl -Is "http://localhost:${server_port_check_refresh_token}" && continue; } || break
350+
done
351+
352+
# https://docs.python.org/3/library/http.server.html
353+
if command -v python 1> /dev/null && python -V | grep -q 'Python 3'; then
354+
python << EOF 1> "${TMPFILE}.code" 2>&1 &
355+
from http.server import BaseHTTPRequestHandler, HTTPServer
356+
357+
class handler(BaseHTTPRequestHandler):
358+
def do_GET(self):
359+
self.send_response(200)
360+
self.end_headers()
361+
if '/?code' in self.path:
362+
message = '${server_string_check_refresh_token}'
363+
self.wfile.write(bytes(message, "utf8"))
364+
365+
with HTTPServer(('', ${server_port_check_refresh_token}), handler) as server:
366+
server.serve_forever()
367+
EOF
368+
_tmp_server_pid="${!}"
369+
elif command -v nc 1> /dev/null; then
370+
# https://stackoverflow.com/a/58436505
371+
printf "%b" "HTTP/1.1 200 OK\nContent-Length: $(printf "%s" "${server_string_check_refresh_token}" | wc -c)\n\n${server_string_check_refresh_token}" | nc -c -l -p "${server_port_check_refresh_token}" 1> "${TMPFILE}.code" 2>&1 &
372+
_tmp_server_pid="${!}"
373+
else
374+
"${QUIET:-_print_center}" "normal" " Error: neither netcat (nc) nor python3 is installed. It is required to required a http server which is used in fetching authorization code. Install and proceed." "-"
375+
return 1
376+
fi
377+
378+
# https://developers.google.com/identity/protocols/oauth2/native-app#obtainingaccesstokens
379+
code_challenge_check_refresh_token="$(_epoch)authorization_code"
339380
[ -z "${refresh_token_value_check_refresh_token}" ] && {
340-
printf "\n" && "${QUIET:-_print_center}" "normal" "Visit the below URL, tap on allow and then enter the code obtained" " "
341-
URL="https://accounts.google.com/o/oauth2/auth?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=${SCOPE}&response_type=code&prompt=consent"
381+
printf "\n" && "${QUIET:-_print_center}" "normal" "Visit the below URL, follow the instructions and then come back to commandline" " "
382+
URL="https://accounts.google.com/o/oauth2/auth?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}%3A${server_port_check_refresh_token}&scope=${SCOPE}&response_type=code&code_challenge_method=plain&code_challenge=${code_challenge_check_refresh_token}"
342383
printf "\n%s\n" "${URL}"
343-
unset AUTHORIZATION_CODE authorization_code AUTHORIZATION_CODE_VALID response
344-
until [ -n "${AUTHORIZATION_CODE}" ] && [ -n "${AUTHORIZATION_CODE_VALID}" ]; do
345-
[ -n "${AUTHORIZATION_CODE}" ] && {
346-
if _assert_regex "${authorization_code_regex}" "${AUTHORIZATION_CODE}"; then
347-
AUTHORIZATION_CODE_VALID="true" && continue
348-
else
349-
"${QUIET:-_print_center}" "normal" " Invalid CODE given, try again.. " "-" && unset AUTHORIZATION_CODE authorization_code
350-
fi
351-
}
352-
{ [ -z "${authorization_code}" ] && printf "\n" && "${QUIET:-_print_center}" "normal" " Enter the authorization code " "-"; } || _clear_line 1
353-
printf -- "-> \033[?7l"
354-
read -r AUTHORIZATION_CODE && authorization_code=1
355-
printf '\033[?7h'
356-
done
357-
response_check_refresh_token="$(curl --compressed "${CURL_PROGRESS}" -X POST \
358-
--data "code=${AUTHORIZATION_CODE}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&redirect_uri=${REDIRECT_URI}&grant_type=authorization_code" "${TOKEN_URL}")" || :
384+
385+
"${QUIET:-_print_center}" "normal" " Press enter if you have completed the process in browser" "-"
386+
read -r _
387+
kill "${_tmp_server_pid}"
388+
389+
if ! authorization_code="$(grep -m1 'GET.*code.*HTTP/1.1' < "${TMPFILE}.code" | sed -e 's/.*GET.*code=//' -e 's/\&.*//')" &&
390+
_assert_regex "${authorization_code_regex}" "${authorization_code}"; then
391+
"${QUIET:-_print_center}" "normal" " Code was not fetched properly , here is some info that maybe helpful.. " "-"
392+
"${QUIET:-_print_center}" "normal" " Code that was grabbed: ${authorization_code} " "-"
393+
printf "Output of http server:\n"
394+
cat "${TMPFILE}.code"
395+
(rm -f "${TMPFILE}.code" &)
396+
return 1
397+
fi
398+
(rm -f "${TMPFILE}.code" &)
399+
400+
# https://developers.google.com/identity/protocols/oauth2/native-app#handlingresponse
401+
response_check_refresh_token="$(_curl --compressed "${CURL_PROGRESS}" -X POST \
402+
--data "code=${authorization_code}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&redirect_uri=${REDIRECT_URI}%3A${server_port_check_refresh_token}&grant_type=authorization_code&code_verifier=${code_challenge_check_refresh_token}" "${TOKEN_URL}")" || :
359403
_clear_line 1 1>&2
360404

361405
refresh_token_value_check_refresh_token="$(printf "%s\n" "${response_check_refresh_token}" | _json_value refresh_token 1 1)" ||
362-
{ printf "%s\n" "Error: Cannot fetch refresh token, make sure the authorization code was correct." && return 1; }
406+
{ printf "%s\n" "Error: Cannot fetch refresh token, make sure the authorization code was correct." && printf "%s\n" "${response_check_refresh_token}" && return 1; }
363407

364408
_set_value direct REFRESH_TOKEN "${refresh_token_value_check_refresh_token}"
365409
{ _check_access_token "${account_name_check_refresh_token}" skip_check "${response_check_refresh_token}" &&

0 commit comments

Comments
 (0)