diff --git a/cmd/setup.go b/cmd/setup.go index e0b06b0f6..068c89dcd 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -86,10 +86,9 @@ func configureSponsorship(token string) error { defer cancel() res, err := client.IsAuthorized(ctx, &pb.AuthRequest{Token: token}) - if err == nil { - if res.Authorized { - sponsor.Subject = res.Subject - } + if err == nil && res.Authorized { + sponsor.Subject = res.Subject + sponsor.Token = token } if err != nil { diff --git a/cmd/soc/main.go b/cmd/soc/main.go index b41e0082f..ede2b3d18 100644 --- a/cmd/soc/main.go +++ b/cmd/soc/main.go @@ -10,6 +10,7 @@ import ( "github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/util" + "github.com/evcc-io/evcc/util/sponsor" "github.com/evcc-io/evcc/vehicle" ) @@ -49,6 +50,10 @@ func main() { case "action": action = arg key = "" + case "token": + sponsor.Subject = arg // TODO placeholder + sponsor.Token = arg + key = "" default: params[key] = arg key = "" diff --git a/util/sponsor/auth.go b/util/sponsor/auth.go index eae92c328..487373b4a 100644 --- a/util/sponsor/auth.go +++ b/util/sponsor/auth.go @@ -1,6 +1,6 @@ package sponsor -var Subject string +var Subject, Token string func IsAuthorized() bool { return len(Subject) > 0 diff --git a/vehicle/cloud.go b/vehicle/cloud.go index a8b1beb13..cba81b044 100644 --- a/vehicle/cloud.go +++ b/vehicle/cloud.go @@ -2,7 +2,6 @@ package vehicle import ( "context" - "errors" "strings" "time" @@ -12,6 +11,7 @@ import ( "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/cloud" "github.com/evcc-io/evcc/util/request" + "github.com/evcc-io/evcc/util/sponsor" ) // Cloud is an api.Vehicle implementation @@ -32,7 +32,6 @@ func init() { // NewCloudFromConfig creates a new vehicle func NewCloudFromConfig(other map[string]interface{}) (api.Vehicle, error) { cc := struct { - Token string embed `mapstructure:",squash"` Brand string Other map[string]string `mapstructure:",remain"` @@ -45,8 +44,8 @@ func NewCloudFromConfig(other map[string]interface{}) (api.Vehicle, error) { return nil, err } - if cc.Token == "" { - return nil, errors.New("missing required token") + if !sponsor.IsAuthorized() { + return nil, api.ErrSponsorRequired } host := util.Getenv("GRPC_URI", cloud.Host) @@ -57,7 +56,7 @@ func NewCloudFromConfig(other map[string]interface{}) (api.Vehicle, error) { v := &Cloud{ embed: &cc.embed, - token: cc.Token, + token: sponsor.Token, brand: cc.Brand, config: cc.Other, client: pb.NewVehicleClient(conn), diff --git a/vehicle/config.go b/vehicle/config.go index ba255c1b8..743b708a0 100644 --- a/vehicle/config.go +++ b/vehicle/config.go @@ -6,6 +6,7 @@ import ( "time" "github.com/evcc-io/evcc/api" + "github.com/evcc-io/evcc/util" ) const ( @@ -43,9 +44,23 @@ func Types() []string { // NewFromConfig creates vehicle from configuration func NewFromConfig(typ string, other map[string]interface{}) (v api.Vehicle, err error) { + cc := struct { + Cloud bool + Other map[string]interface{} `mapstructure:",remain"` + }{} + + if err := util.DecodeOther(other, &cc); err != nil { + return nil, err + } + + if cc.Cloud { + cc.Other["brand"] = typ + typ = "cloud" + } + factory, err := registry.Get(strings.ToLower(typ)) if err == nil { - if v, err = factory(other); err != nil { + if v, err = factory(cc.Other); err != nil { err = fmt.Errorf("cannot create vehicle '%s': %w", typ, err) } } else {