forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_mercedes.go
68 lines (56 loc) · 1.59 KB
/
token_mercedes.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
package cmd
import (
"errors"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/evcc-io/evcc/vehicle/mercedes"
"golang.org/x/oauth2"
)
func mercedesUsernameAndRegionPrompt() (string, string, error) {
prompt_user := &survey.Input{
Message: "Please enter your Mercedes ME user-account (e-mail or mobile)",
}
var user string
if err := survey.AskOne(prompt_user, &user, survey.WithValidator(survey.Required)); err != nil {
return "", "", err
}
prompt_region := &survey.Select{
Message: "Choose your MB region:",
Options: []string{"APAC", "EMEA", "NORAM"},
}
var region string
if err := survey.AskOne(prompt_region, ®ion, survey.WithValidator(survey.Required)); err != nil {
return "", "", err
}
return user, region, nil
}
func mercedesPinPrompt() (string, error) {
var code string
prompt_pin := &survey.Input{
Message: "Please enter the Pin that you received via email/sms",
}
if err := survey.AskOne(prompt_pin, &code, survey.WithValidator(survey.Required)); err != nil {
return "", err
}
return strings.TrimSpace(code), nil
}
func mercedesToken() (*oauth2.Token, error) {
// Get username and region from user to initiate the email process
username, region, err := mercedesUsernameAndRegionPrompt()
if err != nil {
return nil, err
}
api := mercedes.NewSetupAPI(log, username, region)
result, nonce, err := api.RequestPin()
if err != nil {
return nil, err
}
if !result {
return nil, errors.New("unknown PinResponse - 200, result empty")
}
pin, err := mercedesPinPrompt()
if err != nil {
return nil, err
}
return api.RequestAccessToken(*nonce, pin)
}