-
Notifications
You must be signed in to change notification settings - Fork 3
/
trakt-auth.sh
executable file
·115 lines (103 loc) · 3.14 KB
/
trakt-auth.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
#
# Backup personal data from trakt.tv service.
#
# This script authenticates with the trakt.tv API so that subsequent
# requests for personal data can succeed. Authentication information is
# stored in a separate file for each account. The script can be used as
# a cronjob for regular re-authentication.
#
# (c) Copyright 2015 Michael Starzinger. All Rights Reserved.
# Use of this work is governed by a license found in the LICENSE file.
#
BASE="$(cd "$(dirname "$0")" && pwd)"
CLIENT_FILE="$BASE/api-client"
# Parse all command line options.
while [[ $# > 1 ]]; do
case "$1" in
-c|--code)
AUTH_CODE="$2"
shift
;;
-u|--username)
USERNAME="$2"
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
shift
done
# Check that a username has been provided.
if [ -z "$USERNAME" ]; then
echo "No username has been provided."
exit 1
fi
# The API client ID we are using to connect.
CLIENT_ID=$(grep "CLIENT_ID" "$CLIENT_FILE" | awk -F '=' '{ print $2 }')
if [ -z "$CLIENT_ID" ]; then
echo "No CLIENT_ID has been specified."
exit 1
fi
# The API client secret we are using to authenticate.
CLIENT_SEC=$(grep "CLIENT_SECRET" "$CLIENT_FILE" | awk -F '=' '{ print $2 }')
if [ -z "$CLIENT_SEC" ]; then
echo "No CLIENT_SECRET has been specified."
exit 1
fi
# Check that the 'auth' file exists.
AUTH_FILE="$BASE/auth-$USERNAME"
if [ ! -f "$AUTH_FILE" ]; then
echo "Creating a brand new 'auth-$USERNAME' file ..."
cat <<EOF > "$AUTH_FILE"
# This file has been automatically generated.
# Do not edit, run trakt-auth.sh script instead!
EOF
fi
# Check that permissions of 'auth' are good.
if [ "$(stat -c "%a" "$AUTH_FILE")" != "600" ]; then
echo "Fixing permission of 'auth-$USERNAME' file ..."
chmod 600 "$AUTH_FILE"
fi
# The authentication refresh token to be used.
AUTH_REFRESH=$(grep -e '^[^#]' "$AUTH_FILE" | tail -n 1 | awk -F ' ' '{ print $3 }')
if [ -z "$AUTH_REFRESH" ] && [ -z "$AUTH_CODE" ]; then
echo "No authentication code or refresh token provided."
exit 1
fi
if [ -n "$AUTH_CODE" ]; then
GRANT_TYPE="authorization_code"
GRANT_FIELD="code"
GRANT_DATA="$AUTH_CODE"
else
GRANT_TYPE="refresh_token"
GRANT_FIELD="refresh_token"
GRANT_DATA="$AUTH_REFRESH"
fi
# Request headers to be sent along.
CONTENT_TYPE="Content-Type: application/json"
# Post the authentication request.
URL="https://api-v2launch.trakt.tv/oauth/token"
RESPONSE=$(curl --silent --header "$CONTENT_TYPE" --data @- "$URL" <<EOF
{
"$GRANT_FIELD": "$GRANT_DATA",
"client_id": "$CLIENT_ID",
"client_secret": "$CLIENT_SEC",
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"grant_type": "$GRANT_TYPE"
}
EOF
)
# Extract the new authentication access and refresh token.
NEW_ACCESS=$(echo "$RESPONSE" | sed -n 's/.*"access_token":"\([^"]*\)".*$/\1/p')
NEW_REFRESH=$(echo "$RESPONSE" | sed -n 's/.*"refresh_token":"\([^"]*\)".*$/\1/p')
if [ -z "$NEW_ACCESS" ] || [ -z "$NEW_REFRESH" ]; then
echo "Authentication seems to have failed."
echo "$RESPONSE"
exit 1
fi
# Record new authentication information 'auth' file.
TIMESTAMP=$(date -u +%Y-%m-%d@%H:%M:%S)
echo "$TIMESTAMP $NEW_ACCESS $NEW_REFRESH $GRANT_TYPE" >> "$AUTH_FILE"