Skip to content

Commit

Permalink
Smart: simplify UpdateSession
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 22, 2023
1 parent b61546f commit 9e23bb6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 58 deletions.
3 changes: 1 addition & 2 deletions vehicle/smart/hello/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions vehicle/smart/hello/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
"strconv"
"time"

"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
}
Expand Down
53 changes: 1 addition & 52 deletions vehicle/smart/hello/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 9e23bb6

Please sign in to comment.