-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRivianSessionInitOTP.py
105 lines (87 loc) · 3.72 KB
/
RivianSessionInitOTP.py
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
# Run this script to initialize the Rivian API session that uses two factor
# authentication.
# Reads username and password from credentials.json and writes the session info
# to rivian-session.json
import json
import requests
import uuid
GATEWAY_URL = 'https://rivian.com/api/gql/gateway/graphql'
CREDENTIALS_FILE = 'charging_automation/credentials.json'
SESSION_FILE = 'charging_automation/rivian-session.json'
def initialize_session():
print('Initializing new Rivian session...')
request = {
"operationName": "CreateCSRFToken",
"variables": [],
"query": "mutation CreateCSRFToken { createCsrfToken { __typename csrfToken appSessionToken } }"
}
headers = {'Content-Type': 'application/json'}
response = requests.post(GATEWAY_URL, headers=headers, json=request)
if response.status_code != 200:
print('Failed to make GraphQL request: {}'.format(response.text))
return
data = response.json()
csrf_token = data['data']['createCsrfToken']['csrfToken']
app_session_token = data['data']['createCsrfToken']['appSessionToken']
with open(CREDENTIALS_FILE) as f:
data = json.load(f)
username = data['rivian-user']
password = data['rivian-pass']
request = {
"operationName": "Login",
"variables": {
"email": username,
"password": password
},
"query": "mutation Login($email: String!, $password: String!) { login(email: $email, password: $password) { __typename ... on MobileLoginResponse { __typename accessToken refreshToken userSessionToken } ... on MobileMFALoginResponse { __typename otpToken } } }"
}
headers = {
'a-sess': app_session_token,
'csrf-token': csrf_token,
"Dc-Cid": f"m-ios-{uuid.uuid4()}",
"User-Agent": "RivianApp/1304 CFNetwork/1404.0.5 Darwin/22.3.0",
"Accept": "application/json",
"Content-Type": "application/json",
"Apollographql-Client-Name": "com.rivian.ios.consumer-apollo-ios",
}
response = requests.post(GATEWAY_URL, headers=headers, json=request)
if response.status_code != 200:
print('Failed to make GraphQL request: {}'.format(response.text))
return
data = response.json()
otp_token = data['data']['login']['otpToken']
# OTP code should be sent to email/sms now
otp_code = input("Enter Authentication Code: ")
request = {
"operationName": "LoginWithOTP",
"variables": {
"email": username,
"otpCode": otp_code,
"otpToken": otp_token
},
"query": "mutation LoginWithOTP($email: String!, $otpCode: String!, $otpToken: String!) { loginWithOTP(email: $email, otpCode: $otpCode, otpToken: $otpToken) { __typename accessToken refreshToken userSessionToken } }"
}
headers = {
'a-sess': app_session_token,
'csrf-token': csrf_token,
"User-Agent": "RivianApp/1304 CFNetwork/1404.0.5 Darwin/22.3.0",
"Accept": "application/json",
"Content-Type": "application/json",
"Apollographql-Client-Name": "com.rivian.ios.consumer-apollo-ios",
}
response = requests.post(GATEWAY_URL, headers=headers, json=request)
if response.status_code != 200:
print('Failed to make GraphQL request: {}'.format(response.text))
return
data = response.json()
user_session_token = data['data']['loginWithOTP']['userSessionToken']
print('Persisting Rivian session')
with open(SESSION_FILE, 'w') as f:
session_json = {
'appSessionToken': app_session_token,
'userSessionToken': user_session_token,
'csrfToken': csrf_token
}
json.dump(session_json, f)
print('Rivian session initialized')
initialize_session()