forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_tesla.go
138 lines (119 loc) Β· 2.85 KB
/
token_tesla.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
package cmd
import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/bogosj/tesla"
"github.com/evcc-io/evcc/util/request"
"github.com/manifoldco/promptui"
"github.com/skratchdot/open-golang/open"
"golang.org/x/oauth2"
)
// copied from https://github.com/bogosj/tesla
func getUsernameAndPassword() (string, string, error) {
user, err := (&promptui.Prompt{
Label: "Username",
Pointer: promptui.PipeCursor,
Validate: func(s string) error {
if len(s) == 0 {
return errors.New("len(s) == 0")
}
return nil
},
}).Run()
if err != nil {
return "", "", err
}
password, err := (&promptui.Prompt{
Label: "Password",
Mask: '*',
Pointer: promptui.PipeCursor,
Validate: func(s string) error {
if len(s) == 0 {
return errors.New("len(s) == 0")
}
return nil
},
}).Run()
if err != nil {
return "", "", err
}
return user, password, nil
}
func codePrompt(ctx context.Context, devices []tesla.Device) (tesla.Device, string, error) {
var i int
if len(devices) > 1 {
var err error
i, _, err = (&promptui.Select{
Label: "Device",
Items: devices,
Pointer: promptui.PipeCursor,
}).Run()
if err != nil {
return tesla.Device{}, "", fmt.Errorf("select device: %w", err)
}
}
code, err := (&promptui.Prompt{
Label: "Passcode",
Pointer: promptui.PipeCursor,
Validate: func(s string) error {
if len(s) != 6 {
return errors.New("len(s) != 6")
}
return nil
},
}).Run()
if err != nil {
return tesla.Device{}, "", err
}
return devices[i], strings.TrimSpace(code), nil
}
func captchaPrompt(ctx context.Context, svg io.Reader) (string, error) {
tmpFile, err := os.CreateTemp(os.TempDir(), "evcc-*.svg")
if err != nil {
return "", fmt.Errorf("cannot create temp file: %w", err)
}
if _, err := io.Copy(tmpFile, svg); err != nil {
return "", fmt.Errorf("cannot write temp file: %w", err)
}
_ = tmpFile.Close()
if err := open.Run(tmpFile.Name()); err != nil {
return "", fmt.Errorf("cannot open captcha for display: %w", err)
}
fmt.Println("Captcha is now being opened in default application for svg files.")
captcha, err := (&promptui.Prompt{
Label: "Captcha",
Pointer: promptui.PipeCursor,
Validate: func(s string) error {
if len(s) < 4 {
return errors.New("len(s) < 4")
}
return nil
},
}).Run()
return strings.TrimSpace(captcha), err
}
func teslaToken() (*oauth2.Token, error) {
username, password, err := getUsernameAndPassword()
if err != nil {
return nil, err
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
client, err := tesla.NewClient(
ctx,
tesla.WithMFAHandler(codePrompt),
tesla.WithCaptchaHandler(captchaPrompt),
tesla.WithCredentials(username, password),
)
if err != nil {
return nil, err
}
token, err := client.Token()
if err != nil {
return nil, err
}
return token, nil
}