Nissan: refactor

This commit is contained in:
andig 2022-04-10 14:54:20 +02:00
parent 36e867a01e
commit 2024556f1e
2 changed files with 18 additions and 11 deletions

View file

@ -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 (

View file

@ -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