-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
zwift-auth.sh
executable file
·55 lines (44 loc) · 2.1 KB
/
zwift-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
##!/usr/bin/env bash
set -e
LAUNCHER_CLIENT_ID="Game_Launcher"
LAUNCHER_HOME="https://launcher.zwift.com/launcher"
ZWIFT_REALM_URL=https://secure.zwift.com/auth/realms/zwift
COOKIE="cookie.jar"
curl -sS $LAUNCHER_HOME --cookie-jar "$COOKIE"
REQUEST_STATE=$(grep -oP "OAuth_Token_Request_State\s+\K.*$" "$COOKIE")
AUTHENTICATE_URL=$(curl -sSL --get --cookie "$COOKIE" --cookie-jar "$COOKIE" \
--data-urlencode "response_type=code" \
--data-urlencode "client_id=$LAUNCHER_CLIENT_ID" \
--data-urlencode "redirect_uri=$LAUNCHER_HOME" \
--data-urlencode "login=true" \
--data-urlencode "scope=openid" \
--data-urlencode "state=$REQUEST_STATE" \
"$ZWIFT_REALM_URL/protocol/openid-connect/auth" |
grep -oP '<form id="form" class="zwift-form" action="\K(.+?)(?=" method="post">)' |
sed -e 's/\&/\&/g')
ACCESS_CODE=$(curl -sS --cookie "$COOKIE" --cookie-jar "$COOKIE" \
--data-urlencode "username=$ZWIFT_USERNAME" \
--data-urlencode "password=$ZWIFT_PASSWORD" \
--write-out "%{redirect_url}" \
"$AUTHENTICATE_URL" | grep -oP "code=\K.+$")
AUTH_TOKEN_JSON=$(curl -sS --cookie "$COOKIE" --cookie-jar "$COOKIE" \
--data-urlencode "client_id=$LAUNCHER_CLIENT_ID" \
--data-urlencode "redirect_uri=$LAUNCHER_HOME" \
--data-urlencode "code=$ACCESS_CODE" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "scope=openid" \
"$ZWIFT_REALM_URL/protocol/openid-connect/token")
# Or if going as the launcher does it
# the GET request to /ride redirects with a new refresh token that can be used for auth
# REFRESH_TOKEN=$(curl -sSL --get --cookie "$COOKIE" --cookie-jar "$COOKIE" \
# "$LAUNCHER_HOME/ride" \
# --write-out "%{url_effective}" | grep -oP "code=zwift_refresh_token\K.+$")
# AUTH_TOKEN_JSON=$(curl -sS --cookie "$COOKIE" --cookie-jar "$COOKIE" \
# --data-urlencode "client_id=$LAUNCHER_CLIENT_ID" \
# --data-urlencode "redirect_uri=$LAUNCHER_HOME" \
# --data-urlencode "refresh_token=$REFRESH_TOKEN" \
# --data-urlencode "grant_type=refresh_token" \
# --data-urlencode "scope=openid" \
# "$ZWIFT_REALM_URL/protocol/openid-connect/token")
rm $COOKIE
echo $AUTH_TOKEN_JSON