forked from jsgoecke/tesla
-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth.go
307 lines (256 loc) · 7.36 KB
/
auth.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package tesla
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/oauth2"
)
const (
mfaVerifyURL = "https://auth.tesla.com/oauth2/v3/authorize/mfa/verify"
)
// Device is the multi-factor device returned by the /authorize/mfa/factors endpoint
type Device struct {
DispatchRequired bool `json:"dispatchRequired"`
ID string `json:"id"`
Name string `json:"name"`
FactorType string `json:"factorType"`
FactorProvider string `json:"factorProvider"`
SecurityLevel int `json:"securityLevel"`
Activated time.Time `json:"activatedAt"`
Updated time.Time `json:"updatedAt"`
}
type auth struct {
Client *http.Client
AuthURL string
SelectDevice func(ctx context.Context, devices []Device) (d Device, passcode string, err error)
SolveCaptcha func(ctx context.Context, captcha io.Reader) (res string, err error)
}
func (a *auth) initClient(ctx context.Context) {
if client, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); ok {
a.Client = client
return
}
a.Client = &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
}
func (a *auth) Do(ctx context.Context, username, password string) (code string, err error) {
if a.Client == nil {
a.initClient(ctx)
}
if a.Client.Jar == nil {
var err error
a.Client.Jar, err = cookiejar.New(nil)
if err != nil {
return "", fmt.Errorf("new cookie jar: %w", err)
}
}
cr := a.Client.CheckRedirect
a.Client.CheckRedirect = func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}
defer func() { a.Client.CheckRedirect = cr }()
res, v, err := a.login(ctx, username, password)
if err != nil {
return "", fmt.Errorf("login: %w", err)
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusOK:
case http.StatusFound:
return codeFromResponse(res)
default:
return "", fmt.Errorf("unexpected status code %d", res.StatusCode)
}
transactionID := v.Get("transaction_id")
devices, err := a.listDevices(ctx, transactionID)
if err != nil {
return "", fmt.Errorf("list devices: %w", err)
}
if len(devices) == 0 {
return "", errors.New("no devices")
}
d, passcode, err := a.SelectDevice(ctx, devices)
if err != nil {
return "", fmt.Errorf("select device: %w", err)
}
csrf := v.Get("_csrf")
if err := a.verify(ctx, csrf, transactionID, d, passcode); err != nil {
return "", fmt.Errorf("verify: %w", err)
}
return a.commit(ctx, transactionID)
}
func (a *auth) login(ctx context.Context, username, password string) (*http.Response, url.Values, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, a.AuthURL, nil)
if err != nil {
return nil, nil, fmt.Errorf("new request: %w", err)
}
res, err := a.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("do: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("unexpected status code %d", res.StatusCode)
}
v := url.Values{
"identity": {username},
"credential": {password},
}
d, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, nil, fmt.Errorf("new document: %w", err)
}
d.Find("input[type=hidden],input[name=captcha]").Each(func(_ int, s *goquery.Selection) {
name, ok := s.Attr("name")
if !ok {
return
}
value, ok := s.Attr("value")
if name == "captcha" {
ok = true
}
if !ok {
return
}
v.Set(name, value)
})
if _, required := v["captcha"]; required {
u, _ := url.Parse(a.AuthURL)
u.Path = "/captcha"
res, err := a.Client.Get(u.String())
if err != nil {
return nil, nil, fmt.Errorf("access captcha: %w", err)
}
defer res.Body.Close()
solution, err := a.SolveCaptcha(ctx, res.Body)
if err != nil {
return nil, nil, fmt.Errorf("solve captcha: %w", err)
}
v["captcha"] = []string{solution}
}
v.Set("_phase", "authenticate")
v.Set("_process", "1")
req, err = http.NewRequestWithContext(ctx, http.MethodPost, a.AuthURL, strings.NewReader(v.Encode()))
if err != nil {
return nil, nil, fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for _, cookie := range res.Cookies() {
cookie := &http.Cookie{
Name: cookie.Name,
Value: cookie.Value,
}
req.AddCookie(cookie)
}
res, err = a.Client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("do request: %w", err)
}
return res, v, err
}
func (a *auth) listDevices(ctx context.Context, transactionID string) ([]Device, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, (&url.URL{
Scheme: "https",
Host: "auth.tesla.com",
Path: "oauth2/v3/authorize/mfa/factors",
RawQuery: url.Values{"transaction_id": {transactionID}}.Encode(),
}).String(), nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
res, err := a.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("do: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", res.StatusCode)
}
var out struct {
Data []Device `json:"data"`
}
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
return nil, fmt.Errorf("json decode: %w", err)
}
return out.Data, nil
}
func (a *auth) verify(ctx context.Context, csrf string, transactionID string, d Device, passcode string) error {
if csrf == "" {
return errors.New("csrf token is missing for verifing MFA")
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(map[string]string{
"transaction_id": transactionID,
"factor_id": d.ID,
"passcode": passcode,
"_csrf": csrf,
}); err != nil {
return fmt.Errorf("json encode: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, mfaVerifyURL, &buf)
if err != nil {
return fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
res, err := a.Client.Do(req)
if err != nil {
return fmt.Errorf("do: %w", err)
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read body: %w", err)
}
var out MFAVerify
if err := json.Unmarshal(b, &out); err != nil {
return fmt.Errorf("json decode: %w", err)
}
if !out.Data.Approved {
return errors.New("not approved")
}
return nil
}
func (a *auth) commit(ctx context.Context, transactionID string) (code string, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.AuthURL, strings.NewReader(url.Values{
"transaction_id": {transactionID},
}.Encode()))
if err != nil {
return "", fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err := a.Client.Do(req)
if err != nil {
return "", fmt.Errorf("do: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusFound {
return "", fmt.Errorf("unexpected status code %d", res.StatusCode)
}
return codeFromResponse(res)
}
func codeFromResponse(res *http.Response) (code string, err error) {
u, err := res.Location()
if err != nil {
return "", fmt.Errorf("response location: %w", err)
}
return u.Query().Get("code"), nil
}