chore: simplify token handling
This commit is contained in:
parent
d51604f4ec
commit
b351ca0642
4 changed files with 29 additions and 30 deletions
|
|
@ -41,7 +41,7 @@ func NewTeslaCommandFromConfig(other map[string]interface{}) (api.Vehicle, error
|
|||
Timeout time.Duration
|
||||
Cache time.Duration
|
||||
}{
|
||||
Timeout: 10 * time.Second,
|
||||
Timeout: request.Timeout,
|
||||
Cache: interval,
|
||||
}
|
||||
|
||||
|
|
@ -49,20 +49,18 @@ func NewTeslaCommandFromConfig(other map[string]interface{}) (api.Vehicle, error
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := cc.Tokens.Error(); err != nil {
|
||||
token, err := cc.Tokens.Token()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log := util.NewLogger("tesla-command").Redact(vc.OAuth2Config.ClientID, vc.OAuth2Config.ClientSecret)
|
||||
log := util.NewLogger("tesla-command").Redact(
|
||||
cc.Tokens.Access, cc.Tokens.Refresh,
|
||||
vc.OAuth2Config.ClientID, vc.OAuth2Config.ClientSecret,
|
||||
)
|
||||
|
||||
client := request.NewClient(log)
|
||||
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client)
|
||||
ts := vc.OAuth2Config.TokenSource(ctx, &oauth2.Token{
|
||||
AccessToken: cc.Tokens.Access,
|
||||
RefreshToken: cc.Tokens.Refresh,
|
||||
Expiry: time.Now(),
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
|
||||
ts := vc.OAuth2Config.TokenSource(ctx, token)
|
||||
|
||||
identity, err := vc.NewIdentity(log, ts)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,8 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := cc.Tokens.Error(); err != nil {
|
||||
token, err := cc.Tokens.Token()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -52,13 +53,7 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
log := util.NewLogger("tesla").Redact(cc.Tokens.Access, cc.Tokens.Refresh)
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
|
||||
|
||||
options := []tesla.ClientOption{tesla.WithToken(&oauth2.Token{
|
||||
AccessToken: cc.Tokens.Access,
|
||||
RefreshToken: cc.Tokens.Refresh,
|
||||
Expiry: time.Now(),
|
||||
})}
|
||||
|
||||
client, err := tesla.NewClient(ctx, options...)
|
||||
client, err := tesla.NewClient(ctx, tesla.WithToken(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,19 +90,16 @@ func NewTronityFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
}
|
||||
|
||||
var ts oauth2.TokenSource
|
||||
token, err := cc.Tokens.Token()
|
||||
|
||||
// https://app.platform.tronity.io/docs#tag/Authentication
|
||||
if err := cc.Tokens.Error(); err != nil {
|
||||
if err != nil {
|
||||
// use app flow if we don't have tokens
|
||||
ts = oauth.RefreshTokenSource(nil, v)
|
||||
} else {
|
||||
// use provided tokens generated by code flow
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
|
||||
ts = oc.TokenSource(ctx, &oauth2.Token{
|
||||
AccessToken: cc.Tokens.Access,
|
||||
RefreshToken: cc.Tokens.Refresh,
|
||||
Expiry: time.Now(),
|
||||
})
|
||||
ts = oc.TokenSource(ctx, token)
|
||||
}
|
||||
|
||||
// replace client transport with authenticated transport
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
package vehicle
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// ClientCredentials contains OAuth2 client id and secret
|
||||
type ClientCredentials struct {
|
||||
|
|
@ -25,11 +30,15 @@ type Tokens struct {
|
|||
Access, Refresh string
|
||||
}
|
||||
|
||||
// Error validates the token and returns an error if they are incomplete
|
||||
func (t *Tokens) Error() error {
|
||||
// Token builds token from credentials and returns an error if they are incomplete
|
||||
func (t *Tokens) Token() (*oauth2.Token, error) {
|
||||
if t.Access == "" && t.Refresh == "" {
|
||||
return errors.New("missing access and/or refresh token, use `evcc token` to create")
|
||||
return nil, errors.New("missing access and/or refresh token, use `evcc token` to create")
|
||||
}
|
||||
|
||||
return nil
|
||||
return &oauth2.Token{
|
||||
AccessToken: t.Access,
|
||||
RefreshToken: t.Refresh,
|
||||
Expiry: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue