Smart: simplify UpdateSession
This commit is contained in:
parent
b61546f215
commit
9e23bb6777
3 changed files with 6 additions and 58 deletions
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue