Tesla: remove user/password in favour of tokens only (#1220)
This commit is contained in:
parent
66d170b49a
commit
6df1530df5
4 changed files with 98 additions and 65 deletions
122
cmd/tesla.go
122
cmd/tesla.go
|
|
@ -1,7 +1,6 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -14,17 +13,17 @@ import (
|
|||
"github.com/andig/evcc/util"
|
||||
"github.com/andig/evcc/util/request"
|
||||
"github.com/bogosj/tesla"
|
||||
"github.com/manifoldco/promptui"
|
||||
"github.com/skratchdot/open-golang/open"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/thoas/go-funk"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// teslaCmd represents the vehicle command
|
||||
var teslaCmd = &cobra.Command{
|
||||
Use: "tesla-token [name]",
|
||||
Short: "Generate Tesla access token for configured vehicle",
|
||||
Use: "tesla-token",
|
||||
Short: "Generate Tesla token credentials",
|
||||
Run: runTeslaToken,
|
||||
}
|
||||
|
||||
|
|
@ -32,19 +31,69 @@ func init() {
|
|||
rootCmd.AddCommand(teslaCmd)
|
||||
}
|
||||
|
||||
func codePrompt(ctx context.Context, devices []tesla.Device) (tesla.Device, string, error) {
|
||||
fmt.Println("Authentication devices:", funk.Map(devices, func(d tesla.Device) string {
|
||||
return d.Name
|
||||
}))
|
||||
if len(devices) > 1 {
|
||||
return tesla.Device{}, "", errors.New("multiple devices found, only single device supported")
|
||||
// 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
|
||||
}
|
||||
|
||||
fmt.Print("Please enter passcode: ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
code, err := reader.ReadString('\n')
|
||||
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 devices[0], strings.TrimSpace(code), 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) {
|
||||
|
|
@ -64,11 +113,17 @@ func captchaPrompt(ctx context.Context, svg io.Reader) (string, error) {
|
|||
}
|
||||
|
||||
fmt.Println("Captcha is now being opened in default application for svg files.")
|
||||
fmt.Println()
|
||||
|
||||
fmt.Print("Please enter captcha: ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
captcha, err := reader.ReadString('\n')
|
||||
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
|
||||
}
|
||||
|
|
@ -102,37 +157,10 @@ func runTeslaToken(cmd *cobra.Command, args []string) {
|
|||
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))
|
||||
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit)
|
||||
|
||||
// load config
|
||||
conf, err := loadConfigFile(cfgFile)
|
||||
user, password, err := getUsernameAndPassword()
|
||||
if err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
teslas := funk.Filter(conf.Vehicles, func(v qualifiedConfig) bool {
|
||||
return strings.ToLower(v.Type) == "tesla"
|
||||
}).([]qualifiedConfig)
|
||||
|
||||
var vehicleConf qualifiedConfig
|
||||
if len(teslas) == 1 {
|
||||
vehicleConf = teslas[0]
|
||||
} else if len(args) == 1 {
|
||||
vehicleConf = funk.Find(teslas, func(v qualifiedConfig) bool {
|
||||
return strings.EqualFold(v.Name, args[0])
|
||||
}).(qualifiedConfig)
|
||||
}
|
||||
|
||||
if vehicleConf.Name == "" {
|
||||
log.FATAL.Fatal("vehicle not found")
|
||||
}
|
||||
|
||||
var credentials struct {
|
||||
User, Password string
|
||||
Other map[string]interface{} `mapstructure:",remain"`
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(vehicleConf.Other, &credentials); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
generateToken(credentials.User, credentials.Password)
|
||||
generateToken(user, password)
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -47,6 +47,7 @@ require (
|
|||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/lorenzodonini/ocpp-go v0.14.0
|
||||
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
|
||||
github.com/manifoldco/promptui v0.8.0
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/mitchellh/mapstructure v1.4.1
|
||||
github.com/muka/go-bluetooth v0.0.0-20210508070623-03c23c62f181
|
||||
|
|
|
|||
6
go.sum
6
go.sum
|
|
@ -94,8 +94,11 @@ github.com/chromedp/cdproto v0.0.0-20190812224334-39ef923dcb8d/go.mod h1:0YChpVz
|
|||
github.com/chromedp/cdproto v0.0.0-20190926234355-1b4886c6fad6/go.mod h1:0YChpVzuLJC5CPr+x3xkHN6Z8KOSXjNbL7qV8Wc4GW0=
|
||||
github.com/chromedp/chromedp v0.3.1-0.20190619195644-fd957a4d2901/go.mod h1:mJdvfrVn594N9tfiPecUidF6W5jPRKHymqHfzbobPsM=
|
||||
github.com/chromedp/chromedp v0.4.0/go.mod h1:DC3QUn4mJ24dwjcaGQLoZrhm4X/uPHZ6spDbS2uFhm4=
|
||||
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
|
|
@ -395,6 +398,7 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
|
|||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
|
||||
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
|
||||
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
|
|
@ -434,6 +438,7 @@ github.com/lorenzodonini/ocpp-go v0.14.0 h1:r0Gu2oEbVUauvHFppaH9nwuwSXB8VwYWt0pC
|
|||
github.com/lorenzodonini/ocpp-go v0.14.0/go.mod h1:EiMdBqW0YO2Yq1JICuNxBZBaV5V4ygZM+C/h/6Xqu44=
|
||||
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 h1:EnfXoSqDfSNJv0VBNqY/88RNnhSGYkrHaO0mmFGbVsc=
|
||||
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg=
|
||||
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
|
||||
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
|
|
@ -445,6 +450,7 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
|
|||
github.com/mailru/easyjson v0.0.0-20190620125010-da37f6c1e481/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
|
||||
github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo=
|
||||
github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
|
||||
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ=
|
||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
|
|
|
|||
|
|
@ -34,12 +34,11 @@ func init() {
|
|||
// NewTeslaFromConfig creates a new Tesla vehicle
|
||||
func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
||||
cc := struct {
|
||||
embed `mapstructure:",squash"`
|
||||
ClientID, ClientSecret string
|
||||
User, Password string
|
||||
Tokens teslaTokens
|
||||
VIN string
|
||||
Cache time.Duration
|
||||
embed `mapstructure:",squash"`
|
||||
User, Password string // deprecated
|
||||
Tokens teslaTokens
|
||||
VIN string
|
||||
Cache time.Duration
|
||||
}{
|
||||
Cache: interval,
|
||||
}
|
||||
|
|
@ -48,8 +47,12 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if cc.User == "" && cc.Tokens.Access == "" {
|
||||
return nil, errors.New("missing credentials")
|
||||
if cc.User != "" {
|
||||
return nil, errors.New("user/password authentication deprecated, use tesla-token command to create credentials")
|
||||
}
|
||||
|
||||
if cc.Tokens.Access == "" {
|
||||
return nil, errors.New("missing token credentials")
|
||||
}
|
||||
|
||||
v := &Tesla{
|
||||
|
|
@ -60,16 +63,11 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
log := util.NewLogger("tesla")
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewHelper(log).Client)
|
||||
|
||||
var options []tesla.ClientOption
|
||||
if cc.Tokens.Access != "" {
|
||||
options = append(options, tesla.WithToken(&oauth2.Token{
|
||||
AccessToken: cc.Tokens.Access,
|
||||
RefreshToken: cc.Tokens.Refresh,
|
||||
Expiry: time.Now(),
|
||||
}))
|
||||
} else {
|
||||
options = append(options, tesla.WithCredentials(cc.User, cc.Password))
|
||||
}
|
||||
options := []tesla.ClientOption{tesla.WithToken(&oauth2.Token{
|
||||
AccessToken: cc.Tokens.Access,
|
||||
RefreshToken: cc.Tokens.Refresh,
|
||||
Expiry: time.Now(),
|
||||
})}
|
||||
|
||||
client, err := tesla.NewClient(ctx, options...)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue