From 09e6128a4c5fde7298f25fc4777a4fa6eae4b2ad Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Wed, 23 Jun 2021 22:08:56 +0300 Subject: [PATCH] wndclient: user registration WIP --- main.go | 5 +++++ wndclient/messages.go | 18 ++++++++++++++++++ wndclient/wndclient.go | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 0b7bb48..fd67887 100644 --- a/main.go +++ b/main.go @@ -17,5 +17,10 @@ func main() { if err != nil { panic(err) } + + err = wndc.Users(context.TODO()) + if err != nil { + panic(err) + } fmt.Printf("wndc=%#v\n", wndc) } diff --git a/wndclient/messages.go b/wndclient/messages.go index aeeac81..dd77470 100644 --- a/wndclient/messages.go +++ b/wndclient/messages.go @@ -13,3 +13,21 @@ type RegisterTokenResponse struct { TokenTime int64 `json:"time"` } `json:"data"` } + +type UsersRequest struct { + ClientAuthHash string `json:"client_auth_hash"` + SessionType int `json:"session_type_id"` + Time int64 `json:"time,string"` + Token string `json:"token"` +} + +type UsersResponse struct { + Data *struct { + UserID string `json:"user_id"` + SessionAuthHash string `json:"session_auth_hash"` + Status int `json:"status"` + IsPremium int `json:"is_premium"` + LocationRevision int `json:"loc_rev"` + LocationHash string `json:"loc_hash"` + } `json:"data"` +} diff --git a/wndclient/wndclient.go b/wndclient/wndclient.go index 0c41a16..2fbffd7 100644 --- a/wndclient/wndclient.go +++ b/wndclient/wndclient.go @@ -114,7 +114,7 @@ func (c *WndClient) RegisterToken(ctx context.Context) error { clientAuthHash, authTime := MakeAuthHash(c.Settings.ClientAuthSecret) input := RegisterTokenRequest{ ClientAuthHash: clientAuthHash, - Time: authTime, + Time: authTime, } var output RegisterTokenResponse @@ -135,6 +135,38 @@ func (c *WndClient) RegisterToken(ctx context.Context) error { return nil } +func (c *WndClient) Users(ctx context.Context) error { + c.Mux.Lock() + defer c.Mux.Unlock() + + clientAuthHash, authTime := MakeAuthHash(c.Settings.ClientAuthSecret) + input := UsersRequest{ + ClientAuthHash: clientAuthHash, + Time: authTime, + SessionType: SESSION_TYPE_EXT, + Token: c.Token, + } + + var output UsersResponse + + err := c.postJSON(ctx, c.Settings.Endpoints.Users, input, &output) + if err != nil { + return err + } + if output.Data == nil { + return ErrNoDataInResponse + } + + c.UserID = output.Data.UserID + c.SessionAuthHash = output.Data.SessionAuthHash + c.Status = output.Data.Status + c.IsPremium = output.Data.IsPremium != 0 + c.LocationRevision = output.Data.LocationRevision + c.LocationHash = output.Data.LocationHash + + return nil +} + func (c *WndClient) postJSON(ctx context.Context, endpoint string, input, output interface{}) error { var reqBuf bytes.Buffer reqEncoder := json.NewEncoder(&reqBuf) @@ -162,7 +194,7 @@ func (c *WndClient) postJSON(ctx context.Context, endpoint string, input, output return err } - if resp.StatusCode != http.StatusOK { + if resp.StatusCode < 200 || resp.StatusCode > 299 { return fmt.Errorf("bad http status: %s, headers: %#v", resp.Status, resp.Header) }