From 3f7455f5d27d240770a56335c081617223414aac Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:37:48 +0100 Subject: [PATCH 1/9] BMW: Fix hcaptcha --- vehicle/bmw/identity.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/vehicle/bmw/identity.go b/vehicle/bmw/identity.go index 03faf3f588..ef5f268963 100644 --- a/vehicle/bmw/identity.go +++ b/vehicle/bmw/identity.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/evcc-io/evcc/server/db/settings" "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/oauth" "github.com/evcc-io/evcc/util/request" @@ -23,6 +24,8 @@ const ( type Identity struct { *request.Helper region Region + log *util.Logger + user string } // NewIdentity creates BMW identity @@ -30,15 +33,32 @@ func NewIdentity(log *util.Logger, region string) *Identity { v := &Identity{ Helper: request.NewHelper(log), region: regions[strings.ToUpper(region)], + log: log, } return v } -func (v *Identity) Login(user, password string) (oauth2.TokenSource, error) { +func (v *Identity) Login(user, password, hcaptcha string) (oauth2.TokenSource, error) { v.Client.CheckRedirect = request.DontFollow defer func() { v.Client.CheckRedirect = nil }() + v.user = user + + // database token + var tok oauth2.Token + if err := settings.Json(v.settingsKey(), &tok); err == nil { + v.log.DEBUG.Println("identity.Login - database token found") + tok, err := v.RefreshToken(&tok) + if err == nil { + ts := oauth2.ReuseTokenSourceWithExpiry(tok, oauth.RefreshTokenSource(tok, v), 15*time.Minute) + return ts, nil + } + v.log.DEBUG.Println("identity.Login - database token invalid. Proceeding to login via user, password and captcha.") + } else { + v.log.DEBUG.Println("identity.Login - no database token found. Proceeding to login via user, password and captcha.") + } + cv := oauth2.GenerateVerifier() v.Jar, _ = cookiejar.New(&cookiejar.Options{ @@ -60,7 +80,11 @@ func (v *Identity) Login(user, password string) (oauth2.TokenSource, error) { } uri := fmt.Sprintf("%s/oauth/authenticate", v.region.AuthURI) - req, err := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), request.URLEncoding) + headers := map[string]string{ + "Content-Type": "application/x-www-form-urlencoded", + "hcaptchatoken": hcaptcha, + } + req, err := request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), headers) if err != nil { return nil, err } @@ -142,8 +166,15 @@ func (v *Identity) retrieveToken(data url.Values) (*oauth2.Token, error) { if err == nil { err = v.DoJSON(req, &tok) } + if err != nil { + return nil, err + } + + tokex := util.TokenWithExpiry(&tok) + + err = settings.SetJson(v.settingsKey(), tokex) - return util.TokenWithExpiry(&tok), err + return tokex, err } func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) { @@ -155,3 +186,7 @@ func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) { return v.retrieveToken(data) } + +func (v *Identity) settingsKey() string { + return fmt.Sprintf("bmw.%s", v.user) +} From f5900dde02a502a406ba4b8ab34a511f56f797ea Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:39:10 +0100 Subject: [PATCH 2/9] BMW: Fix hcaptcha --- vehicle/bmw.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vehicle/bmw.go b/vehicle/bmw.go index bc7fa5bdb7..fe6958f48d 100644 --- a/vehicle/bmw.go +++ b/vehicle/bmw.go @@ -34,6 +34,7 @@ func NewBMWMiniFromConfig(brand string, other map[string]interface{}) (api.Vehic cc := struct { embed `mapstructure:",squash"` User, Password, VIN string + Hcaptcha string Region string Cache time.Duration }{ @@ -45,7 +46,7 @@ func NewBMWMiniFromConfig(brand string, other map[string]interface{}) (api.Vehic return nil, err } - if cc.User == "" || cc.Password == "" { + if cc.User == "" || cc.Password == "" || cc.Hcaptcha == "" { return nil, api.ErrMissingCredentials } @@ -56,7 +57,7 @@ func NewBMWMiniFromConfig(brand string, other map[string]interface{}) (api.Vehic log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN) identity := bmw.NewIdentity(log, cc.Region) - ts, err := identity.Login(cc.User, cc.Password) + ts, err := identity.Login(cc.User, cc.Password, cc.Hcaptcha) if err != nil { return nil, err } From f043eb5c7fcf91b711d6058fd1969bdbe5e098f1 Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:40:14 +0100 Subject: [PATCH 3/9] BMW: Fix hcaptcha --- templates/definition/vehicle/bmw.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/templates/definition/vehicle/bmw.yaml b/templates/definition/vehicle/bmw.yaml index 1a1391ad0a..48e61ea59d 100644 --- a/templates/definition/vehicle/bmw.yaml +++ b/templates/definition/vehicle/bmw.yaml @@ -1,6 +1,12 @@ template: bmw products: - brand: BMW +requirements: + description: + de: | + Benötigt `hcaptcha` Token. Dieser kann unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. + en: | + Requires `hcaptcha` token. This can be generated at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html params: - preset: vehicle-base - preset: vehicle-identify @@ -19,6 +25,8 @@ params: advanced: true - name: welcomecharge advanced: true + - name: hcaptcha + required: true render: | type: bmw {{ include "vehicle-base" . }} @@ -35,3 +43,4 @@ render: | - welcomecharge {{- end }} {{- end }} + hcaptcha: {{ .hcaptcha }} From 7381a33a8565411a84916a905c9a3be360ec1454 Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:41:52 +0100 Subject: [PATCH 4/9] MINI: Fix hcaptcha --- templates/definition/vehicle/mini.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/templates/definition/vehicle/mini.yaml b/templates/definition/vehicle/mini.yaml index 6b1b7a12f5..b578359359 100644 --- a/templates/definition/vehicle/mini.yaml +++ b/templates/definition/vehicle/mini.yaml @@ -1,6 +1,12 @@ template: mini products: - brand: Mini +requirements: + description: + de: | + Benötigt `hcaptcha` Token. Dieser kann unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. + en: | + Requires `hcaptcha` token. This can be generated at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html params: - preset: vehicle-base - preset: vehicle-identify @@ -17,6 +23,8 @@ params: advanced: true - name: welcomecharge advanced: true + - name: hcaptcha + required: true render: | type: mini {{ include "vehicle-base" . }} @@ -27,3 +35,4 @@ render: | {{- if eq .welcomecharge "true" }} features: ["welcomecharge"] {{- end }} + hcaptcha: {{ .hcaptcha }} From 0d929c0e7d573cd6fbd4dffc1c29bba3f87eb582 Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Mon, 25 Nov 2024 22:48:30 +0100 Subject: [PATCH 5/9] BMW: Fix hcaptcha --- vehicle/bmw/identity.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/vehicle/bmw/identity.go b/vehicle/bmw/identity.go index ef5f268963..f4b4af1ec3 100644 --- a/vehicle/bmw/identity.go +++ b/vehicle/bmw/identity.go @@ -165,10 +165,9 @@ func (v *Identity) retrieveToken(data url.Values) (*oauth2.Token, error) { var tok oauth2.Token if err == nil { err = v.DoJSON(req, &tok) + } else { + return nil, err } - if err != nil { - return nil, err - } tokex := util.TokenWithExpiry(&tok) From b641bf54be03bc26802cc0e35bc2cc598d7db065 Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Tue, 26 Nov 2024 07:42:16 +0100 Subject: [PATCH 6/9] Clarify documentation for hcaptcha --- templates/definition/vehicle/bmw.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/definition/vehicle/bmw.yaml b/templates/definition/vehicle/bmw.yaml index 48e61ea59d..6e1b004362 100644 --- a/templates/definition/vehicle/bmw.yaml +++ b/templates/definition/vehicle/bmw.yaml @@ -4,9 +4,9 @@ products: requirements: description: de: | - Benötigt `hcaptcha` Token. Dieser kann unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. + Benötigt `hcaptcha` Token. Dieser muss einmalig unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. Das Token ist nur für kurze Zeit gültig. Bitte möglichst schnell nach Generierung in die Konfiguration kopieren und evcc starten. en: | - Requires `hcaptcha` token. This can be generated at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html + Requires `hcaptcha` token. This must be generated once at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html. The token is only valid for a short time. Please copy it into the configuration and start evcc as soon as possible after generation. params: - preset: vehicle-base - preset: vehicle-identify From 574bb0cad5d771977e2f4e3230271a7ee1dfaa4f Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Tue, 26 Nov 2024 07:43:29 +0100 Subject: [PATCH 7/9] Clarify documentation for hcaptcha (MINI) --- templates/definition/vehicle/mini.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/definition/vehicle/mini.yaml b/templates/definition/vehicle/mini.yaml index b578359359..7d93bbde4b 100644 --- a/templates/definition/vehicle/mini.yaml +++ b/templates/definition/vehicle/mini.yaml @@ -4,9 +4,9 @@ products: requirements: description: de: | - Benötigt `hcaptcha` Token. Dieser kann unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. + Benötigt `hcaptcha` Token. Dieser muss einmalig unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. Das Token ist nur für kurze Zeit gültig. Bitte möglichst schnell nach Generierung in die Konfiguration kopieren und evcc starten. en: | - Requires `hcaptcha` token. This can be generated at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html + Requires `hcaptcha` token. This must be generated once at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html. The token is only valid for a short time. Please copy it into the configuration and start evcc as soon as possible after generation. params: - preset: vehicle-base - preset: vehicle-identify From 7fe1c35f00c20a179900e884413fa2f0afa62b47 Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Tue, 26 Nov 2024 07:45:57 +0100 Subject: [PATCH 8/9] Clarify documentation for hcaptcha --- templates/definition/vehicle/bmw.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/definition/vehicle/bmw.yaml b/templates/definition/vehicle/bmw.yaml index 6e1b004362..1e36a91878 100644 --- a/templates/definition/vehicle/bmw.yaml +++ b/templates/definition/vehicle/bmw.yaml @@ -4,7 +4,7 @@ products: requirements: description: de: | - Benötigt `hcaptcha` Token. Dieser muss einmalig unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. Das Token ist nur für kurze Zeit gültig. Bitte möglichst schnell nach Generierung in die Konfiguration kopieren und evcc starten. + Benötigt `hcaptcha` Token. Dieses muss einmalig unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. Das Token ist nur für kurze Zeit gültig. Bitte möglichst schnell nach Generierung in die Konfiguration kopieren und evcc starten. en: | Requires `hcaptcha` token. This must be generated once at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html. The token is only valid for a short time. Please copy it into the configuration and start evcc as soon as possible after generation. params: From 746d09c3f7b47d16aa25492fe71a0d4cf7f7bc56 Mon Sep 17 00:00:00 2001 From: BrickTop87 <151788504+BrickTop87@users.noreply.github.com> Date: Tue, 26 Nov 2024 07:46:18 +0100 Subject: [PATCH 9/9] Clarify documentation for hcaptcha (MINI) --- templates/definition/vehicle/mini.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/definition/vehicle/mini.yaml b/templates/definition/vehicle/mini.yaml index 7d93bbde4b..e60cbfe7f6 100644 --- a/templates/definition/vehicle/mini.yaml +++ b/templates/definition/vehicle/mini.yaml @@ -4,7 +4,7 @@ products: requirements: description: de: | - Benötigt `hcaptcha` Token. Dieser muss einmalig unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. Das Token ist nur für kurze Zeit gültig. Bitte möglichst schnell nach Generierung in die Konfiguration kopieren und evcc starten. + Benötigt `hcaptcha` Token. Dieses muss einmalig unter https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html generiert werden. Das Token ist nur für kurze Zeit gültig. Bitte möglichst schnell nach Generierung in die Konfiguration kopieren und evcc starten. en: | Requires `hcaptcha` token. This must be generated once at https://bimmer-connected.readthedocs.io/en/latest/captcha/rest_of_world.html. The token is only valid for a short time. Please copy it into the configuration and start evcc as soon as possible after generation. params: