Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions view-token
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ usage() {
<filename> - file containing a token. Can be a plain text file with the bare token,
or an Rclone config file. In that case, view-token will search for
a line containing "bearer_token" and decode the value.
You can use a trick like this:
view-token <(cat <<<"\$my_token")
This allows you to read from a variable without exposing the variable
to other users with the ps command.
If filename is "-" (dash), view-token will read the token from stdin.
To read the token in a safe way from an environment variable,
you can use this trick:
view-token - <<<"\$my_token"
This will not expose your token to other users with the ps command.

<no filename> - view-token will look for environment variable BEARER_TOKEN
and decode its value.
Expand Down Expand Up @@ -344,13 +345,18 @@ done


if [ -n "$tokenfile" ] ; then
$verbose && echo "Token source: $tokenfile"
#
# Read the tokenfile only once (It might be a file descriptor that will be closed after reading)
tokenfile_contents=$(<"$tokenfile") || {
echo "ERROR: unable to read token from '$tokenfile'" 1>&2
exit 1
}
if [ "$tokenfile" = "-" ]; then
$verbose && echo "Reading token from stdin. Input is not shown. Continue with the Enter key." 1>&2
read -s tokenfile_contents
else
$verbose && echo "Token source: $tokenfile"
tokenfile_contents=$(<"$tokenfile") || {
echo "ERROR: unable to read token from '$tokenfile'" 1>&2
exit 1
}
fi
#
# First, we assume the tokenfile is an Rclone config file.
token=$(sed -n 's/^bearer_token *= *//p' <<<"$tokenfile_contents")
Expand All @@ -376,4 +382,9 @@ else
# read BEARER_TOKEN
fi

if [ -z "$token" ] ; then
echo 1>&2 "ERROR: token is empty."
exit 1
fi

check_token "$token" || exit 1