Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mercedes: remove user account #18046

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions templates/definition/vehicle/mercedes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ requirements:
params:
- preset: vehicle-common
- name: user
required: true
deprecated: true
- name: region
required: true
type: choice
Expand All @@ -29,7 +29,6 @@ params:
render: |
type: mercedes
vin: {{ .vin }}
user: {{ .user }}
region: {{ .region }}
tokens:
access: {{ .accessToken }}
Expand Down
4 changes: 2 additions & 2 deletions templates/definition/vehicle/smart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ requirements:
params:
- preset: vehicle-common
- name: user
required: true
deprecated: true
- name: region
required: true
choice: [EMEA, APAC, NORAM]
Expand All @@ -24,13 +24,13 @@ params:
required: true
mask: true
- name: vin
required: true
example: V...
- name: cache
default: 15m
render: |
type: smart-eq
vin: {{ .vin }}
user: {{ .user }}
region: {{ .region }}
tokens:
access: {{ .accessToken }}
Expand Down
18 changes: 6 additions & 12 deletions vehicle/mercedes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ func init() {
// newMercedesFromConfig creates a new vehicle
func newMercedesFromConfig(brand string, other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
Tokens Tokens
User string
Account_ string `mapstructure:"account"` // TODO deprecated
VIN string
Cache time.Duration
Region string
embed `mapstructure:",squash"`
Tokens Tokens
VIN string
Cache time.Duration
Region string
}{
Cache: interval,
}
Expand All @@ -47,12 +45,8 @@ func newMercedesFromConfig(brand string, other map[string]interface{}) (api.Vehi
return nil, err
}

if cc.User == "" && cc.Account_ != "" {
cc.User = cc.Account_
}

log := util.NewLogger(brand).Redact(cc.Tokens.Access, cc.Tokens.Refresh)
identity, err := mercedes.NewIdentity(log, token, cc.User, cc.Region)
identity, err := mercedes.NewIdentity(log, token, cc.Region)
if err != nil {
return nil, err
}
Expand Down
17 changes: 13 additions & 4 deletions vehicle/mercedes/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/evcc-io/evcc/util/oauth"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"golang.org/x/oauth2"
)
Expand All @@ -40,15 +41,23 @@ var OAuth2Config = &oauth2.Config{
}

// NewIdentity creates Mercedes identity
func NewIdentity(log *util.Logger, token *oauth2.Token, account string, region string) (*Identity, error) {
func NewIdentity(log *util.Logger, token *oauth2.Token, region string) (*Identity, error) {
var claims struct {
Email string
jwt.RegisteredClaims
}
if _, _, err := new(jwt.Parser).ParseUnverified(token.AccessToken, &claims); err != nil {
return nil, err
}

// serialise instance handling
mu.Lock()
defer mu.Unlock()

v := &Identity{
Helper: request.NewHelper(log),
log: log,
account: account,
account: claims.Email,
region: region,
}

Expand All @@ -59,7 +68,7 @@ func NewIdentity(log *util.Logger, token *oauth2.Token, account string, region s
}

// reuse identity instance
if instance := getInstance(account); instance != nil {
if instance := getInstance(v.account); instance != nil {
v.log.DEBUG.Println("identity.NewIdentity - token found in instance store")
return instance, nil
}
Expand Down Expand Up @@ -91,7 +100,7 @@ func NewIdentity(log *util.Logger, token *oauth2.Token, account string, region s
v.TokenSource = oauth.RefreshTokenSource(token, v)

// add instance
addInstance(account, v)
addInstance(v.account, v)

return v, nil
}
Expand Down
Loading