mirror of
https://github.com/mrworf/plexupdate.git
synced 2025-08-19 21:03:19 -07:00
Merge pull request #164 from mrworf/snown-stored-token-auth
Use stored token from Plex
This commit is contained in:
commit
4c5669e35f
1 changed files with 69 additions and 47 deletions
116
plexupdate.sh
116
plexupdate.sh
|
@ -187,8 +187,8 @@ rawurlencode() {
|
||||||
case "$c" in
|
case "$c" in
|
||||||
[-_.~a-zA-Z0-9] ) o="${c}" ;;
|
[-_.~a-zA-Z0-9] ) o="${c}" ;;
|
||||||
* ) printf -v o '%%%02x' "'$c"
|
* ) printf -v o '%%%02x' "'$c"
|
||||||
esac
|
esac
|
||||||
encoded+="${o}"
|
encoded+="${o}"
|
||||||
done
|
done
|
||||||
echo "${encoded}"
|
echo "${encoded}"
|
||||||
}
|
}
|
||||||
|
@ -200,6 +200,23 @@ keypair() {
|
||||||
echo "${key}=${val}"
|
echo "${key}=${val}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPlexServerToken() {
|
||||||
|
if [ -f /etc/default/plexmediaserver ]; then
|
||||||
|
source /etc/default/plexmediaserver
|
||||||
|
fi
|
||||||
|
|
||||||
|
# List possible locations to find Plex Server preference file
|
||||||
|
local VALIDPATHS=("${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}" "/var/lib/plexmediaserver/Library/Application Support/" "${HOME}/Library/Application Support/")
|
||||||
|
local PREFFILE="/Plex Media Server/Preferences.xml"
|
||||||
|
|
||||||
|
for I in "${VALIDPATHS[@]}" ; do
|
||||||
|
if [ ! -z "${I}" -a -f "${I}${PREFFILE}" ]; then
|
||||||
|
sed -n 's/.*PlexOnlineToken="\([[:alnum:]]*\).*".*/\1/p' "${I}${PREFFILE}" 2>/dev/null || error "Do not have permission to read token from Plex Server preference file"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
# Setup an exit handler so we cleanup
|
# Setup an exit handler so we cleanup
|
||||||
cleanup() {
|
cleanup() {
|
||||||
for F in "${FILE_RAW}" "${FILE_FAILCAUSE}" "${FILE_POSTDATA}" "${FILE_KAKA}" "${FILE_SHA}" "${FILE_LOCAL}" "${FILE_REMOTE}" "${FILE_WGETLOG}"; do
|
for F in "${FILE_RAW}" "${FILE_FAILCAUSE}" "${FILE_POSTDATA}" "${FILE_KAKA}" "${FILE_SHA}" "${FILE_LOCAL}" "${FILE_REMOTE}" "${FILE_WGETLOG}"; do
|
||||||
|
@ -230,7 +247,7 @@ done
|
||||||
# We have to double-check that both files exist before trying to stat them. This is going away soon.
|
# We have to double-check that both files exist before trying to stat them. This is going away soon.
|
||||||
if [ -z "${CONFIGFILE}" -a -f ~/.plexupdate -a ! -f /etc/plexupdate.conf ] || \
|
if [ -z "${CONFIGFILE}" -a -f ~/.plexupdate -a ! -f /etc/plexupdate.conf ] || \
|
||||||
([ -f "${CONFIGFILE}" -a -f ~/.plexupdate ] && [ `stat -Lc %i "${CONFIGFILE}"` == `stat -Lc %i ~/.plexupdate` ]); then
|
([ -f "${CONFIGFILE}" -a -f ~/.plexupdate ] && [ `stat -Lc %i "${CONFIGFILE}"` == `stat -Lc %i ~/.plexupdate` ]); then
|
||||||
warn ".plexupdate has been deprecated. Please run $(dirname "$0")/extras/installer.sh to update your configuration."
|
warn ".plexupdate has been deprecated. Please run $(dirname "$0")/extras/installer.sh to update your configuration."
|
||||||
if [ -t 1 ]; then
|
if [ -t 1 ]; then
|
||||||
for i in `seq 1 5`; do echo -n .\ ; sleep 1; done
|
for i in `seq 1 5`; do echo -n .\ ; sleep 1; done
|
||||||
echo .
|
echo .
|
||||||
|
@ -371,18 +388,6 @@ if [ "${AUTOUPDATE}" = "yes" ]; then
|
||||||
popd &>/dev/null
|
popd &>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Sanity check
|
|
||||||
if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ]; then
|
|
||||||
error "Need username & password to download PlexPass version. Otherwise run with -p to download public version."
|
|
||||||
exit 1
|
|
||||||
elif [ ! -z "${EMAIL}" ] && [[ "$EMAIL" == *"@"* ]] && [[ "$EMAIL" != *"@"*"."* ]]; then
|
|
||||||
error "EMAIL field must contain a valid email address"
|
|
||||||
exit 1
|
|
||||||
elif [ ! -z "${EMAIL}" -a ! -z "${PASS}" -a "${PUBLIC}" = "yes" ]; then
|
|
||||||
warn "You have defined email and password but PUBLIC is set to yes, this will not download the PlexPass version"
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
if [ "${AUTOINSTALL}" = "yes" -o "${AUTOSTART}" = "yes" ] && [ ${EUID} -ne 0 ]; then
|
if [ "${AUTOINSTALL}" = "yes" -o "${AUTOSTART}" = "yes" ] && [ ${EUID} -ne 0 ]; then
|
||||||
error "You need to be root to use AUTOINSTALL/AUTOSTART option."
|
error "You need to be root to use AUTOINSTALL/AUTOSTART option."
|
||||||
fi
|
fi
|
||||||
|
@ -446,8 +451,6 @@ if [ "${CHECKUPDATE}" = "yes" -a "${AUTOUPDATE}" = "no" ]; then
|
||||||
rm "${FILE_REMOTE}" 2>/dev/null >/dev/null
|
rm "${FILE_REMOTE}" 2>/dev/null >/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Fields we need to submit for login to work
|
# Fields we need to submit for login to work
|
||||||
#
|
#
|
||||||
# Field Value
|
# Field Value
|
||||||
|
@ -459,42 +462,61 @@ fi
|
||||||
# commit Sign in
|
# commit Sign in
|
||||||
|
|
||||||
if [ "${PUBLIC}" = "no" ]; then
|
if [ "${PUBLIC}" = "no" ]; then
|
||||||
info "Authenticating with plex.tv"
|
|
||||||
|
|
||||||
# Clean old session
|
# Clean old session
|
||||||
rm "${FILE_KAKA}" 2>/dev/null
|
rm "${FILE_KAKA}" 2>/dev/null
|
||||||
|
|
||||||
# Build post data
|
# Try to obtain token from Plex Server Installation
|
||||||
echo -ne >"${FILE_POSTDATA}" "$(keypair "user[login]" "${EMAIL}" )"
|
TOKEN=
|
||||||
echo -ne >>"${FILE_POSTDATA}" "&$(keypair "user[password]" "${PASS}" )"
|
if [ -z "${EMAIL}" -o -z "${PASS}" ]; then
|
||||||
echo -ne >>"${FILE_POSTDATA}" "&$(keypair "user[remember_me]" "0" )"
|
TOKEN=$(getPlexServerToken)
|
||||||
|
|
||||||
# Authenticate (using Plex Single Sign On)
|
|
||||||
wget --header "X-Plex-Client-Identifier: 4a745ae7-1839-e44e-1e42-aebfa578c865" --header "X-Plex-Product: Plex SSO" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_LOGIN}" --post-file="${FILE_POSTDATA}" -q -S -O "${FILE_FAILCAUSE}" 2>"${FILE_RAW}"
|
|
||||||
# Delete authentication data ... Bad idea to let that stick around
|
|
||||||
rm "${FILE_POSTDATA}"
|
|
||||||
|
|
||||||
# Provide some details to the end user
|
|
||||||
RESULTCODE=$(head -n1 "${FILE_RAW}" | grep -oe '[1-5][0-9][0-9]')
|
|
||||||
if [ $RESULTCODE -eq 401 ]; then
|
|
||||||
error "Username and/or password incorrect"
|
|
||||||
if [ "$VERBOSE" = "yes" ]; then
|
|
||||||
info "Tried using \"${EMAIL}\" and \"${PASS}\" "
|
|
||||||
fi
|
|
||||||
exit 1
|
|
||||||
elif [ $RESULTCODE -ne 201 ]; then
|
|
||||||
error "Failed to login, debug information:"
|
|
||||||
cat "${FILE_RAW}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If the system got here, it means the login was successfull, so we set the TOKEN variable to the authToken from the response
|
if [ -z "${TOKEN}" ]; then
|
||||||
# I use cut -c 14- to cut off the "authToken":" string from the grepped result, can probably be done in a different way
|
# If no token, go through regular process
|
||||||
TOKEN=$(<"${FILE_FAILCAUSE}" grep -ioe '"authToken":"[^"]*' | cut -c 14-)
|
if [ -z "${EMAIL}" -o -z "${PASS}" ]; then
|
||||||
|
error "Need username & password to download PlexPass version. Otherwise run with -p to download public version."
|
||||||
|
exit 1
|
||||||
|
elif [ ! -z "${EMAIL}" ] && [[ "$EMAIL" == *"@"* ]] && [[ "$EMAIL" != *"@"*"."* ]]; then
|
||||||
|
error "EMAIL field must contain a valid email address"
|
||||||
|
exit 1
|
||||||
|
elif [ ! -z "${EMAIL}" -a ! -z "${PASS}" -a "${PUBLIC}" = "yes" ]; then
|
||||||
|
warn "You have defined email and password but PUBLIC is set to yes, this will not download the PlexPass version"
|
||||||
|
fi
|
||||||
|
info "Authenticating with plex.tv using email and password"
|
||||||
|
|
||||||
# Remove this, since it contains more information than we should leave hanging around
|
# Build post data
|
||||||
rm "${FILE_FAILCAUSE}"
|
echo -ne >"${FILE_POSTDATA}" "$(keypair "user[login]" "${EMAIL}" )"
|
||||||
|
echo -ne >>"${FILE_POSTDATA}" "&$(keypair "user[password]" "${PASS}" )"
|
||||||
|
echo -ne >>"${FILE_POSTDATA}" "&$(keypair "user[remember_me]" "0" )"
|
||||||
|
|
||||||
|
# Authenticate (using Plex Single Sign On)
|
||||||
|
wget --header "X-Plex-Client-Identifier: 4a745ae7-1839-e44e-1e42-aebfa578c865" --header "X-Plex-Product: Plex SSO" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_LOGIN}" --post-file="${FILE_POSTDATA}" -q -S -O "${FILE_FAILCAUSE}" 2>"${FILE_RAW}"
|
||||||
|
# Delete authentication data ... Bad idea to let that stick around
|
||||||
|
rm "${FILE_POSTDATA}"
|
||||||
|
|
||||||
|
# Provide some details to the end user
|
||||||
|
RESULTCODE=$(head -n1 "${FILE_RAW}" | grep -oe '[1-5][0-9][0-9]')
|
||||||
|
if [ $RESULTCODE -eq 401 ]; then
|
||||||
|
error "Username and/or password incorrect"
|
||||||
|
if [ "$VERBOSE" = "yes" ]; then
|
||||||
|
info "Tried using \"${EMAIL}\" and \"${PASS}\" "
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
elif [ $RESULTCODE -ne 201 ]; then
|
||||||
|
error "Failed to login, debug information:"
|
||||||
|
cat "${FILE_RAW}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If the system got here, it means the login was successfull, so we set the TOKEN variable to the authToken from the response
|
||||||
|
# I use cut -c 14- to cut off the "authToken":" string from the grepped result, can probably be done in a different way
|
||||||
|
TOKEN=$(<"${FILE_FAILCAUSE}" grep -ioe '"authToken":"[^"]*' | cut -c 14-)
|
||||||
|
|
||||||
|
# Remove this, since it contains more information than we should leave hanging around
|
||||||
|
rm "${FILE_FAILCAUSE}"
|
||||||
|
else
|
||||||
|
info "Using Plex Server credentials to authenticate"
|
||||||
|
fi
|
||||||
elif [ "$PUBLIC" != "no" ]; then
|
elif [ "$PUBLIC" != "no" ]; then
|
||||||
# It's a public version, so change URL and make doubly sure that cookies are empty
|
# It's a public version, so change URL and make doubly sure that cookies are empty
|
||||||
rm 2>/dev/null >/dev/null "${FILE_KAKA}"
|
rm 2>/dev/null >/dev/null "${FILE_KAKA}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue