From 2024556f1e72d7cc93a5869f47f9044674a50fe5 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 10 Apr 2022 14:54:20 +0200 Subject: [PATCH] Nissan: refactor --- vehicle/nissan/api.go | 11 ++++------- vehicle/nissan/identity.go | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/vehicle/nissan/api.go b/vehicle/nissan/api.go index 53c09c251..8b8e8e3dc 100644 --- a/vehicle/nissan/api.go +++ b/vehicle/nissan/api.go @@ -12,13 +12,6 @@ import ( // api constants const ( - APIVersion = "protocol=1.0,resource=2.1" - ClientID = "a-ncb-prod-android" - ClientSecret = "0sAcrtwvwEXXZp5nzQhPexSRhxUVKa0d76F4uqDvxvvKFHXpo4myoJwUuV4vuNqC" - Scope = "openid profile vehicles" - AuthBaseURL = "https://prod.eu2.auth.kamereon.org/kauth" - Realm = "a-ncb-prod" - RedirectURI = "org.kamereon.service.nci:/oauth2redirect" CarAdapterBaseURL = "https://alliance-platform-caradapter-prod.apps.eu2.kamereon.io/car-adapter" UserAdapterBaseURL = "https://alliance-platform-usersadapter-prod.apps.eu2.kamereon.io/user-adapter" UserBaseURL = "https://nci-bff-web-prod.apps.eu.kamereon.io/bff-web" @@ -98,6 +91,10 @@ func (v *API) RefreshRequest(vin string, typ string) (ActionResponse, error) { return res, err } +// more commands: https://github.com/TA2k/ioBroker.nissan/commit/0e32ab743af3cbecd756633e52e9baa869766c7d +// refresh-location +// wake-up-vehicle + type Action string const ( diff --git a/vehicle/nissan/identity.go b/vehicle/nissan/identity.go index 4f43aaf01..30b37a82f 100644 --- a/vehicle/nissan/identity.go +++ b/vehicle/nissan/identity.go @@ -13,6 +13,16 @@ import ( "golang.org/x/oauth2" ) +const ( + APIVersion = "protocol=1.0,resource=2.1" + ClientID = "a-ncb-prod-android" + ClientSecret = "0sAcrtwvwEXXZp5nzQhPexSRhxUVKa0d76F4uqDvxvvKFHXpo4myoJwUuV4vuNqC" + Scope = "openid profile vehicles" + Realm = "a-ncb-prod" + AuthURL = "https://prod.eu2.auth.kamereon.org/kauth" + RedirectURI = "org.kamereon.service.nci:/oauth2redirect" +) + type Identity struct { *request.Helper oauth2.TokenSource @@ -26,7 +36,7 @@ func NewIdentity(log *util.Logger) *Identity { } func (v *Identity) Login(user, password string) error { - uri := fmt.Sprintf("%s/json/realms/root/realms/%s/authenticate", AuthBaseURL, Realm) + uri := fmt.Sprintf("%s/json/realms/root/realms/%s/authenticate", AuthURL, Realm) req, err := request.New(http.MethodPost, uri, nil, map[string]string{ "Accept-Api-Version": APIVersion, "X-Username": "anonymous", @@ -88,7 +98,7 @@ func (v *Identity) Login(user, password string) error { "nonce": {"sdfdsfez"}, } - uri := fmt.Sprintf("%s/oauth2/%s/authorize?%s", AuthBaseURL, realm, data.Encode()) + uri := fmt.Sprintf("%s/oauth2/%s/authorize?%s", AuthURL, realm, data.Encode()) req, err = request.New(http.MethodGet, uri, nil, map[string]string{ "Cookie": "i18next=en-UK; amlbcookie=05; kauthSession=" + nToken.TokenID, }) @@ -115,7 +125,7 @@ func (v *Identity) Login(user, password string) error { "grant_type": {"authorization_code"}, } - uri = fmt.Sprintf("%s/oauth2/%s/access_token?%s", AuthBaseURL, realm, data.Encode()) + uri = fmt.Sprintf("%s/oauth2/%s/access_token?%s", AuthURL, realm, data.Encode()) req, err = request.New(http.MethodPost, uri, nil, request.URLEncoding) if err == nil { err = v.DoJSON(req, &token) @@ -137,7 +147,7 @@ func (v *Identity) RefreshToken(token *oauth2.Token) (*oauth2.Token, error) { "refresh_token": {token.RefreshToken}, } - uri := fmt.Sprintf("%s/oauth2/%s/access_token?%s", AuthBaseURL, Realm, data.Encode()) + uri := fmt.Sprintf("%s/oauth2/%s/access_token?%s", AuthURL, Realm, data.Encode()) req, err := request.New(http.MethodPost, uri, nil, request.URLEncoding) var res oauth.Token