Add --auth-response option and expose through entrypoint.sh (#1628)

* This allows for the use of the `--auth-files` switch in the docker environment.

Co-authored-by: abraunegg <alex.braunegg@gmail.com>
This commit is contained in:
Winston R. Milling 2021-11-22 14:08:04 -06:00 committed by GitHub
parent bfeeae91de
commit 5d7e4532ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 7 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash -eu #!/bin/bash -eu
set +H -xeuo pipefail set +H -euo pipefail
: ${ONEDRIVE_UID:=$(stat /onedrive/data -c '%u')} : ${ONEDRIVE_UID:=$(stat /onedrive/data -c '%u')}
: ${ONEDRIVE_GID:=$(stat /onedrive/data -c '%g')} : ${ONEDRIVE_GID:=$(stat /onedrive/data -c '%g')}
@ -27,46 +27,67 @@ chown "${oduser}:${odgroup}" /onedrive/ /onedrive/conf
# Default parameters # Default parameters
ARGS=(--monitor --confdir /onedrive/conf --syncdir /onedrive/data) ARGS=(--monitor --confdir /onedrive/conf --syncdir /onedrive/data)
echo "Base Args: ${ARGS}"
# Make Verbose output optional, based on an environment variable # Make Verbose output optional, based on an environment variable
if [ "${ONEDRIVE_VERBOSE:=0}" == "1" ]; then if [ "${ONEDRIVE_VERBOSE:=0}" == "1" ]; then
echo "# We are being verbose" echo "# We are being verbose"
echo "# Adding --verbose"
ARGS=(--verbose ${ARGS[@]}) ARGS=(--verbose ${ARGS[@]})
fi fi
# Tell client to perform debug output, based on an environment variable # Tell client to perform debug output, based on an environment variable
if [ "${ONEDRIVE_DEBUG:=0}" == "1" ]; then if [ "${ONEDRIVE_DEBUG:=0}" == "1" ]; then
echo "# We are performing debug output" echo "# We are performing debug output"
echo "# Adding --verbose --verbose"
ARGS=(--verbose --verbose ${ARGS[@]}) ARGS=(--verbose --verbose ${ARGS[@]})
fi fi
# Tell client to perform HTTPS debug output, based on an environment variable # Tell client to perform HTTPS debug output, based on an environment variable
if [ "${ONEDRIVE_DEBUG_HTTPS:=0}" == "1" ]; then if [ "${ONEDRIVE_DEBUG_HTTPS:=0}" == "1" ]; then
echo "# We are performing HTTPS debug output" echo "# We are performing HTTPS debug output"
echo "# Adding --debug-https"
ARGS=(--debug-https ${ARGS[@]}) ARGS=(--debug-https ${ARGS[@]})
fi fi
# Tell client to perform a resync based on environment variable # Tell client to perform a resync based on environment variable
if [ "${ONEDRIVE_RESYNC:=0}" == "1" ]; then if [ "${ONEDRIVE_RESYNC:=0}" == "1" ]; then
echo "# We are performing a --resync" echo "# We are performing a --resync"
echo "# Adding --resync"
ARGS=(--resync ${ARGS[@]}) ARGS=(--resync ${ARGS[@]})
fi fi
# Tell client to sync in download-only mode based on environment variable # Tell client to sync in download-only mode based on environment variable
if [ "${ONEDRIVE_DOWNLOADONLY:=0}" == "1" ]; then if [ "${ONEDRIVE_DOWNLOADONLY:=0}" == "1" ]; then
echo "# We are synchronizing in download-only mode" echo "# We are synchronizing in download-only mode"
echo "# Adding --download-only"
ARGS=(--download-only ${ARGS[@]}) ARGS=(--download-only ${ARGS[@]})
fi fi
# Tell client to logout based on environment variable # Tell client to logout based on environment variable
if [ "${ONEDRIVE_LOGOUT:=0}" == "1" ]; then if [ "${ONEDRIVE_LOGOUT:=0}" == "1" ]; then
echo "# We are logging out to perform a reauthentication" echo "# We are logging out to perform a reauthentication"
echo "# Adding --logout"
ARGS=(--logout ${ARGS[@]}) ARGS=(--logout ${ARGS[@]})
fi fi
# Tell client to utilize auth files at the provided locations based on environment variable
if [ -n "${ONEDRIVE_AUTHFILES:=""}" ]; then
echo "# We are using auth files to perform authentication"
echo "# Adding --auth-files ARG"
ARGS=(--auth-files ${ONEDRIVE_AUTHFILES} ${ARGS[@]})
fi
# Tell client to utilize provided auth reponse based on environment variable
if [ -n "${ONEDRIVE_AUTHRESPONSE:=""}" ]; then
echo "# We are providing the auth response directly to perform authentication"
echo "# Adding --auth-response ARG"
ARGS=(--auth-response \"${ONEDRIVE_AUTHRESPONSE}\" ${ARGS[@]})
fi
if [ ${#} -gt 0 ]; then if [ ${#} -gt 0 ]; then
ARGS=("${@}") ARGS=("${@}")
fi fi
echo "# Launching onedrive"
exec gosu "${oduser}" /usr/local/bin/onedrive "${ARGS[@]}" exec gosu "${oduser}" /usr/local/bin/onedrive "${ARGS[@]}"

View file

@ -195,6 +195,8 @@ docker run $firstRun --restart unless-stopped --name onedrive -v onedrive_conf:/
| <B>ONEDRIVE_RESYNC</B> | Controls "--resync" switch on onedrive sync. Default is 0 | 1 | | <B>ONEDRIVE_RESYNC</B> | Controls "--resync" switch on onedrive sync. Default is 0 | 1 |
| <B>ONEDRIVE_DOWNLOADONLY</B> | Controls "--download-only" switch on onedrive sync. Default is 0 | 1 | | <B>ONEDRIVE_DOWNLOADONLY</B> | Controls "--download-only" switch on onedrive sync. Default is 0 | 1 |
| <B>ONEDRIVE_LOGOUT</B> | Controls "--logout" switch. Default is 0 | 1 | | <B>ONEDRIVE_LOGOUT</B> | Controls "--logout" switch. Default is 0 | 1 |
| <B>ONEDRIVE_AUTHFILES</B> | Controls "--auth-files" option. Default is "" | "authUrl:responseUrl" |
| <B>ONEDRIVE_AUTHRESPONSE</B> | Controls "--auth-response" option. Default is "" | See [here](https://github.com/abraunegg/onedrive/blob/master/docs/USAGE.md#authorize-the-application-with-your-onedrive-account) |
### Usage Examples ### Usage Examples
**Verbose Output:** **Verbose Output:**

View file

@ -996,6 +996,8 @@ Options:
Perform authorization via two files passed in as ARG in the format `authUrl:responseUrl` Perform authorization via two files passed in as ARG in the format `authUrl:responseUrl`
The authorization URL is written to the `authUrl`, then onedrive waits for the file `responseUrl` The authorization URL is written to the `authUrl`, then onedrive waits for the file `responseUrl`
to be present, and reads the response from that file. to be present, and reads the response from that file.
--auth-response ARG
Perform authentication not via interactive dialog but via providing the reponse url directly.
--check-for-nomount --check-for-nomount
Check for the presence of .nosync in the syncdir root. If found, do not perform sync. Check for the presence of .nosync in the syncdir root. If found, do not perform sync.
--check-for-nosync --check-for-nosync

View file

@ -289,6 +289,7 @@ final class Config
stringValues["single_directory"] = ""; stringValues["single_directory"] = "";
stringValues["source_directory"] = ""; stringValues["source_directory"] = "";
stringValues["auth_files"] = ""; stringValues["auth_files"] = "";
stringValues["auth_response"] = "";
boolValues["display_config"] = false; boolValues["display_config"] = false;
boolValues["display_sync_status"] = false; boolValues["display_sync_status"] = false;
boolValues["print_token"] = false; boolValues["print_token"] = false;
@ -312,6 +313,9 @@ final class Config
"auth-files", "auth-files",
"Perform authentication not via interactive dialog but via files read/writes to these files.", "Perform authentication not via interactive dialog but via files read/writes to these files.",
&stringValues["auth_files"], &stringValues["auth_files"],
"auth-response",
"Perform authentication not via interactive dialog but via providing the reponse url directly.",
&stringValues["auth_response"],
"check-for-nomount", "check-for-nomount",
"Check for the presence of .nosync in the syncdir root. If found, do not perform sync.", "Check for the presence of .nosync in the syncdir root. If found, do not perform sync.",
&boolValues["check_nomount"], &boolValues["check_nomount"],

View file

@ -509,12 +509,10 @@ final class OneDriveApi
char[] response; char[] response;
string url = authUrl ~ "?client_id=" ~ clientId ~ "&scope=Files.ReadWrite%20Files.ReadWrite.all%20Sites.Read.All%20Sites.ReadWrite.All%20offline_access&response_type=code&prompt=login&redirect_uri=" ~ redirectUrl; string url = authUrl ~ "?client_id=" ~ clientId ~ "&scope=Files.ReadWrite%20Files.ReadWrite.all%20Sites.Read.All%20Sites.ReadWrite.All%20offline_access&response_type=code&prompt=login&redirect_uri=" ~ redirectUrl;
string authFilesString = cfg.getValueString("auth_files"); string authFilesString = cfg.getValueString("auth_files");
if (authFilesString == "") { string authResponseString = cfg.getValueString("auth_response");
log.log("Authorize this app visiting:\n"); if (authResponseString != "") {
write(url, "\n\n", "Enter the response uri: "); response = cast(char[]) authResponseString;
readln(response); } else if (authFilesString != "") {
cfg.applicationAuthorizeResponseUri = true;
} else {
string[] authFiles = authFilesString.split(":"); string[] authFiles = authFilesString.split(":");
string authUrl = authFiles[0]; string authUrl = authFiles[0];
string responseUrl = authFiles[1]; string responseUrl = authFiles[1];
@ -542,6 +540,11 @@ final class OneDriveApi
log.error("Cannot remove files ", authUrl, " ", responseUrl); log.error("Cannot remove files ", authUrl, " ", responseUrl);
return false; return false;
} }
} else {
log.log("Authorize this app visiting:\n");
write(url, "\n\n", "Enter the response uri: ");
readln(response);
cfg.applicationAuthorizeResponseUri = true;
} }
// match the authorization code // match the authorization code
auto c = matchFirst(response, r"(?:[\?&]code=)([\w\d-.]+)"); auto c = matchFirst(response, r"(?:[\?&]code=)([\w\d-.]+)");