From 9e23bb67774007a52f304e368ca2d3847ed09d62 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 22 Dec 2023 09:37:31 +0100 Subject: [PATCH] Smart: simplify UpdateSession --- vehicle/smart/hello/api.go | 3 +- vehicle/smart/hello/helper.go | 8 ++--- vehicle/smart/hello/identity.go | 53 +-------------------------------- 3 files changed, 6 insertions(+), 58 deletions(-) diff --git a/vehicle/smart/hello/api.go b/vehicle/smart/hello/api.go index a76e16816..1a1d2a763 100644 --- a/vehicle/smart/hello/api.go +++ b/vehicle/smart/hello/api.go @@ -156,8 +156,7 @@ func (v *API) UpdateSession(vin string) error { } func (v *API) Status(vin string) (VehicleStatus, error) { - if _, err := v.identity.UpdateSession(vin); err != nil { - // if err := v.UpdateSession(vin); err != nil { + if err := v.UpdateSession(vin); err != nil { return VehicleStatus{}, fmt.Errorf("update session failed: %w", err) } diff --git a/vehicle/smart/hello/helper.go b/vehicle/smart/hello/helper.go index c9ee01b18..cf82541fd 100644 --- a/vehicle/smart/hello/helper.go +++ b/vehicle/smart/hello/helper.go @@ -5,8 +5,8 @@ import ( "crypto/md5" "crypto/sha1" "encoding/base64" - "encoding/json" "fmt" + "io" "net/url" "strconv" "time" @@ -14,13 +14,13 @@ import ( "github.com/samber/lo" ) -func createSignature(method, path string, params url.Values, post any) (string, string, string, error) { +func createSignature(method, path string, params url.Values, body io.Reader) (string, string, string, error) { nonce := lo.RandomString(16, lo.AlphanumericCharset) ts := strconv.FormatInt(time.Now().UnixMilli(), 10) md5Hash := "1B2M2Y8AsgTpgAmY7PhCfg==" - if post != nil { - bytes, err := json.Marshal(post) + if body != nil { + bytes, err := io.ReadAll(body) if err != nil { return "", "", "", err } diff --git a/vehicle/smart/hello/identity.go b/vehicle/smart/hello/identity.go index 854c0edef..80616a440 100644 --- a/vehicle/smart/hello/identity.go +++ b/vehicle/smart/hello/identity.go @@ -173,7 +173,7 @@ func (v *Identity) appToken(token *oauth2.Token) (*oauth2.Token, string, error) } path := "/auth/account/session/secure" - nonce, ts, sign, err := createSignature(http.MethodPost, path, params, data) + nonce, ts, sign, err := createSignature(http.MethodPost, path, params, request.MarshalJSON(data)) if err != nil { return nil, "", err } @@ -211,54 +211,3 @@ func (v *Identity) appToken(token *oauth2.Token) (*oauth2.Token, string, error) return &tok, res.Data.UserId, nil } - -func (v *Identity) UpdateSession(vin string) (string, error) { - token, err := v.Token() - if err != nil { - return "", err - } - - params := url.Values{ - "identity_type": []string{"smart"}, - } - - data := map[string]string{ - "vin": vin, - "sessionToken": token.AccessToken, - "language": "", - } - - path := "/device-platform/user/session/update" - nonce, ts, sign, err := createSignature(http.MethodPost, path, params, data) - if err != nil { - return "", err - } - - uri := fmt.Sprintf("%s/%s?%s", ApiURI, strings.TrimPrefix(path, "/"), params.Encode()) - req, _ := request.New(http.MethodPost, uri, request.MarshalJSON(data), map[string]string{ - "Accept": "application/json;responseformat=3", - "Content-Type": "application/json; charset=utf-8", - "X-Api-Signature-Version": "1.0", - "X-Api-Signature-Nonce": nonce, - "Authorization": token.AccessToken, - "X-App-Id": appID, - "X-Device-Identifier": v.deviceID, - "X-Operator-Code": operatorCode, - "X-Signature": sign, - "X-Timestamp": ts, - }) - - var res struct { - Code ResponseCode - Message string - Data AppToken - } - - if err := v.DoJSON(req, &res); err != nil { - return "", err - } else if res.Code != ResponseOK { - return "", fmt.Errorf("%d: %s", res.Code, res.Message) - } - - return "", nil -}