Skip to content

Commit

Permalink
chore: simplify token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jan 24, 2024
1 parent d51604f commit b351ca0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
20 changes: 9 additions & 11 deletions vehicle/tesla-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,26 @@ func NewTeslaCommandFromConfig(other map[string]interface{}) (api.Vehicle, error
Timeout time.Duration
Cache time.Duration
}{
Timeout: 10 * time.Second,
Timeout: request.Timeout,
Cache: interval,
}

if err := util.DecodeOther(other, &cc); err != nil {
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)

client := request.NewClient(log)
log := util.NewLogger("tesla-command").Redact(
cc.Tokens.Access, cc.Tokens.Refresh,
vc.OAuth2Config.ClientID, vc.OAuth2Config.ClientSecret,
)

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 {
Expand Down
11 changes: 3 additions & 8 deletions vehicle/tesla.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
9 changes: 3 additions & 6 deletions vehicle/tronity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions vehicle/types.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

0 comments on commit b351ca0

Please sign in to comment.