VAG: simplify token refresh handling (replaces #9700) (#9702)

This commit is contained in:
andig 2023-09-03 18:31:30 +02:00 • committed by GitHub
parent 5cc4c146ee
commit 5dd4d9e6e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 44 deletions

View file

@ -10,7 +10,7 @@ import (
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle/seat"
"github.com/evcc-io/evcc/vehicle/seat/cupra"
"github.com/evcc-io/evcc/vehicle/vag/tokenrefreshservice"
"github.com/evcc-io/evcc/vehicle/vag/service"
"github.com/evcc-io/evcc/vehicle/vag/vwidentity"
"golang.org/x/oauth2"
)
@ -51,20 +51,11 @@ func NewCupraFromConfig(other map[string]interface{}) (api.Vehicle, error) {
log := util.NewLogger("cupra").Redact(cc.User, cc.Password, cc.VIN)
// get initial VW identity id_token
q, err := vwidentity.Login(log, seat.AuthParams, cc.User, cc.Password)
ts, err := service.TokenRefreshServiceTokenSource(log, seat.TRSParams, seat.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
trs := tokenrefreshservice.New(log, seat.TRSParams)
token, err := trs.Exchange(q)
if err != nil {
return nil, err
}
ts := trs.TokenSource(token)
// get OIDC user information
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, request.NewClient(log))
ui, err := vwidentity.Config.NewProvider(ctx).UserInfo(ctx, ts)

View file

@ -8,7 +8,6 @@ import (
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle/seat"
"github.com/evcc-io/evcc/vehicle/vag/service"
"github.com/evcc-io/evcc/vehicle/vag/tokenrefreshservice"
"github.com/evcc-io/evcc/vehicle/vw"
)
@ -51,12 +50,12 @@ func NewSeatFromConfig(other map[string]interface{}) (api.Vehicle, error) {
log := util.NewLogger("seat").Redact(cc.User, cc.Password, cc.VIN)
trs := tokenrefreshservice.New(log, seat.TRSParams)
ts, err := service.MbbTokenSource(log, trs, seat.AuthClientID, seat.AuthParams, cc.User, cc.Password)
trs, err := service.TokenRefreshServiceTokenSource(log, seat.TRSParams, seat.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
ts := service.MbbTokenSource(log, trs, seat.AuthClientID)
api := vw.NewAPI(log, ts, seat.Brand, seat.Country)
api.Client.Timeout = cc.Timeout

View file

@ -8,7 +8,6 @@ import (
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle/skoda"
"github.com/evcc-io/evcc/vehicle/vag/service"
"github.com/evcc-io/evcc/vehicle/vag/tokenrefreshservice"
"github.com/evcc-io/evcc/vehicle/vw"
)
@ -52,12 +51,12 @@ func NewSkodaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
log := util.NewLogger("skoda").Redact(cc.User, cc.Password, cc.VIN)
// use Skoda api to resolve list of vehicles
ts, err := service.TokenRefreshServiceTokenSource(log, skoda.TRSParams, skoda.AuthParams, cc.User, cc.Password)
trs, err := service.TokenRefreshServiceTokenSource(log, skoda.TRSParams, skoda.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
api := skoda.NewAPI(log, ts)
api := skoda.NewAPI(log, trs)
api.Client.Timeout = cc.Timeout
vehicle, err := ensureVehicleEx(
@ -72,12 +71,7 @@ func NewSkodaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
}
if err == nil {
trs := tokenrefreshservice.New(log, skoda.TRSParams)
ts, err := service.MbbTokenSource(log, trs, skoda.AuthClientID, skoda.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
ts := service.MbbTokenSource(log, trs, skoda.AuthClientID)
api := vw.NewAPI(log, ts, skoda.Brand, skoda.Country)
api.Client.Timeout = cc.Timeout

View file

@ -6,26 +6,14 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/vag"
"github.com/evcc-io/evcc/vehicle/vag/mbb"
"github.com/evcc-io/evcc/vehicle/vag/vwidentity"
)
// MbbTokenSource creates a refreshing token source for use with the MBB api.
// Once the MBB token expires, it is recreated from the token exchanger (either TokenRefreshService or IDK)
func MbbTokenSource(log *util.Logger, tox vag.TokenExchanger, clientID string, q url.Values, user, password string) (vag.TokenSource, error) {
q, err := vwidentity.Login(log, q, user, password)
if err != nil {
return nil, err
}
token, err := tox.Exchange(q)
if err != nil {
return nil, err
}
trs := tox.TokenSource(token)
func MbbTokenSource(log *util.Logger, trs vag.TokenSource, clientID string) vag.TokenSource {
mbb := mbb.New(log, clientID)
mts := vag.MetaTokenSource(func() (*vag.Token, error) {
return vag.MetaTokenSource(func() (*vag.Token, error) {
// get TRS token from refreshing TRS token source
itoken, err := trs.TokenEx()
if err != nil {
@ -42,6 +30,4 @@ func MbbTokenSource(log *util.Logger, tox vag.TokenExchanger, clientID string, q
// produce tokens from refresh MBB token source
}, mbb.TokenSource)
return mts, nil
}

View file

@ -4,12 +4,12 @@ import (
"net/url"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/vag"
"github.com/evcc-io/evcc/vehicle/vag/tokenrefreshservice"
"github.com/evcc-io/evcc/vehicle/vag/vwidentity"
"golang.org/x/oauth2"
)
func TokenRefreshServiceTokenSource(log *util.Logger, data, q url.Values, user, password string) (oauth2.TokenSource, error) {
func TokenRefreshServiceTokenSource(log *util.Logger, data, q url.Values, user, password string) (vag.TokenSource, error) {
q, err := vwidentity.Login(log, q, user, password)
if err != nil {
return nil, err

View file

@ -7,7 +7,6 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle/vag/service"
"github.com/evcc-io/evcc/vehicle/vag/tokenrefreshservice"
"github.com/evcc-io/evcc/vehicle/vw"
)
@ -50,12 +49,12 @@ func NewVWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
log := util.NewLogger("vw").Redact(cc.User, cc.Password, cc.VIN)
trs := tokenrefreshservice.New(log, vw.TRSParams)
ts, err := service.MbbTokenSource(log, trs, vw.AuthClientID, vw.AuthParams, cc.User, cc.Password)
trs, err := service.TokenRefreshServiceTokenSource(log, vw.TRSParams, vw.AuthParams, cc.User, cc.Password)
if err != nil {
return nil, err
}
ts := service.MbbTokenSource(log, trs, vw.AuthClientID)
api := vw.NewAPI(log, ts, vw.Brand, vw.Country)
api.Client.Timeout = cc.Timeout