-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeslaTokens.vbs
150 lines (131 loc) · 5.5 KB
/
TeslaTokens.vbs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'Prerequesites:
'Windows
'Chilkat: http://www.chilkatsoft.com/downloads_ActiveX.asp
'Based on the work on those two websites
'- https://tesla-api.timdorr.com/api-basics/authentication
'- https://github.com/bntan/tesla-tokens
'Version 1.0 (2022-04-30)
Option Explicit
Dim fso, wsh, dict, fortuna, crypt, http, json, outFilePath, outFile, scriptdir
Dim code_verifier, code_challenge, state, lang, success
Dim code, data, response, URL, jsonRequestBody, resp, strValue
Dim LangFilePath, LangFile, English, Line, Pos, EndPos
Const Title = "TeslaTokens"
Set fso = CreateObject("Scripting.FileSystemObject")
Set wsh = CreateObject("WScript.Shell")
Set dict = CreateObject("Scripting.Dictionary")
Set fortuna = CreateObject("Chilkat_9_5_0.Prng")
Set crypt = CreateObject("Chilkat_9_5_0.Crypt2")
Set http = CreateObject("Chilkat_9_5_0.Http")
scriptdir = fso.GetParentFolderName(WScript.ScriptFullName)
outFilePath = Replace(WScript.ScriptFullName,".vbs",".txt")
'Read language from OS and read localisation file in dictionary
lang = ReadFromRegistry("HKEY_CURRENT_USER\Control Panel\International\LocaleName","en-US")
LangFilePath = scriptdir & "\TeslaTokens-" & lang & ".txt"
if fso.FileExists(LangFilePath) then
Set LangFile = fso.OpenTextFile(LangFilePath,1)
Do Until LangFile.AtEndOfStream
Line = LangFile.ReadLine
Pos = InStr(Line,"=")
if Pos > 0 then
dict.Add Left(Line,Pos-1),Mid(Line,Pos+1)
end if
Loop
else
'Fallback to english as default
English = True
end if
'Create random strings with length of 83 and 16 characters
code_verifier = fortuna.RandomString(83,1,1,1)
state = fortuna.RandomString(16,1,1,1)
'SHA256
crypt.HashAlgorithm = "sha256"
crypt.EncodingMode = "hex"
code_challenge = crypt.HashStringENC(code_verifier)
'URLSAFE64
crypt.EncodingMode = "base64url"
code_challenge = crypt.EncodeString(code_challenge,"ansi","base64")
code_challenge = replace(code_challenge,"=","")
MsgBox GetText("Welcome",""),,Title
MsgBox GetText("Introduction",""),,Title
'Make URL and call it in default browser. User has to log in his TESLA account
URL = "https://auth.tesla.com/oauth2/v3/authorize?audience=https%3A%2F%2Fownership.tesla.com%2F&client_id=ownerapi&code_challenge=" & code_challenge & "&code_challenge_method=S256&locale=en-US&prompt=login&redirect_uri=https%3A%2F%2Fauth.tesla.com%2Fvoid%2Fcallback&response_type=code&scope=openid+email+offline_access&state=" & state
wsh.Run URL
'After authentiation user must now enter the URL. Code will be extracted from the URL
code = InputBox(GetText("InputCode",""),Title)
if Len(Code) > 0 then
Pos = InStr(code,"code=")
if Pos > 0 then Pos = Pos + 5
EndPos = InStr(Pos,code,"&")
if EndPos > 0 then EndPos = EndPos
code = mid(code,Pos,EndPos - Pos)
else
WScript.Quit
end if
jsonRequestBody = "{ ""grant_type"": ""authorization_code"", ""client_id"": ""ownerapi"", ""code_verifier"": """ & code_verifier & """, ""code"": """ & code & """, ""redirect_uri"": ""https://auth.tesla.com/void/callback"" }"
http.SetRequestHeader "User-Agent",""
http.SetRequestHeader "x-tesla-user-agent",""
http.SetRequestHeader "X-Requested-With","com.teslamotors.tesla"
http.Accept = "application/json"
URL = "https://auth.tesla.com/oauth2/v3/token"
Set resp = http.PostJson2(url,"application/json",jsonRequestBody)
If (http.LastMethodSuccess = 0) Then
MsgBox http.LastErrorText,,Title
WScript.Quit
End If
if resp.StatusCode = 200 then
Set outFile = fso.CreateTextFile(outFilePath, True)
set json = CreateObject("Chilkat_9_5_0.JsonObject")
success = json.Load(resp.BodyStr)
json.EmitCompact = 0
outFile.WriteLine(GetText("HeaderTokenFile",DateTime()))
outFile.WriteLine(json.Emit())
outFile.Close
MsgBox GetText("Success",outFilePath),,Title
wsh.Run outFilePath
else
MsgBox GetText("Error",resp.StatusCode),,Title
end if
Function DateTime()
DateTime = year(now()) & "-" & right("0" & month(now()),2) & "-" & right("0" & day(now()),2) & " " & right("0" & hour(now()),2) & ":" & right("0" & minute(now()),2) & ":" & right("0" & second(now()),2)
End Function
Function GetText(p, para)
Dim NotFound
if NOT English then
if dict.Exists(p) then
GetText = dict.Item(p)
if len(para) > 0 then
GetText = Replace(GetText,"###",para)
end if
NotFound = False
else
NotFound = True
end if
end if
if English or NotFound then
Select Case p
Case "Welcome"
GetText = "Welcome. This scripts gets the access tokens for your TESLA car. Since you can open this script with any editor you can be sure that none of your data will be grabbed."
Case "Introduction"
GetText = "After you press OK, the TESLA login window will open in your web browser. Please register there."
Case "InputCode"
GetText = "After you are logged in, 'Page Not Found' appears, but everything is fine. Please copy the URL line from your web browser and paste it here:"
Case "Success"
GetText = "Your tokens have been successfully read and are in file ###. This file will then be opened in your editor."
Case "Error"
GetText = "The TESLA website returned status code ###. Your tokens could not be read."
Case "HeaderTokenFile"
GetText = "Your access and refresh tokens (###):"
End Select
end if
End Function
Function ReadFromRegistry(strRegistryKey, strDefault)
Dim value
On Error Resume Next
value = wsh.RegRead( strRegistryKey )
if err.number <> 0 then
readFromRegistry= strDefault
else
readFromRegistry=value
end if
End Function