diff --git a/backend/nalogru/client.go b/backend/nalogru/client.go index fa85eb2..dcbe7ca 100644 --- a/backend/nalogru/client.go +++ b/backend/nalogru/client.go @@ -80,6 +80,12 @@ type TicketIdResponse struct { Status int `json:"status"` } +type PhoneAuthRequest struct { + ClientSecret string `json:"client_secret"` + Os string `json:"os"` + Phone string `json:"phone"` +} + // GetTicketId - send ticket id request to nalog.ru API. func (nalogruClient *Client) GetTicketId(queryString string, device *device.Device) (string, error) { payload := TicketIdRequest{Qr: queryString} @@ -231,6 +237,33 @@ func (nalogruClient *Client) GetElectronicTickets(device *device.Device) ([]*Tic return tickets, nil } +func (nalogruClient *Client) AuthByPhone(device *device.Device) error { + payload := PhoneAuthRequest{ + ClientSecret: device.ClientSecret, + Os: "android", + Phone: device.Phone, + } + + req, err := json.Marshal(payload) + if err != nil { + log.Println("Unable to serialize request") + return err + } + reader := bytes.NewReader(req) + url := nalogruClient.BaseAddress + "/v2/auth/phone/request" + request, err := http.NewRequest(http.MethodPost, url, reader) + addHeaders(request, device.Id.Hex()) + client := createHttpClient() + _, err = sendRequest(request, client) + + if err != nil { + log.Printf("Can't POST %s\n", url) + return err + } + + return nil +} + func (nalogruClient *Client) sendAuthenticatedRequest(r *http.Request, device *device.Device) (*http.Response, error) { addHeaders(r, device.Id.Hex()) addAuth(r, device.SessionId) @@ -262,6 +295,7 @@ type RefreshResponse struct { RefreshToken string `json:"refresh_token"` } +// RefreshSession used to refresh session by RefreshToken. func (nalogruClient *Client) RefreshSession(device *device.Device) error { client := createHttpClient() diff --git a/backend/nalogru/client_test.go b/backend/nalogru/client_test.go index 2c02761..4845767 100644 --- a/backend/nalogru/client_test.go +++ b/backend/nalogru/client_test.go @@ -11,10 +11,11 @@ import ( ) var baseAddress = "https://irkkt-mobile.nalog.ru:8888" -var sessionId = "606c4bb41d8f48ff57bfd021:6ca239ba-0885-4cc9-a7d9-be801253ac7d" +var sessionId = "INSERT SESSION ID HERE" var deviceId = primitive.NewObjectID().Hex() -var secret = "mnALjKobrqT/sC9um4wXlamXnOo=" -var refreshToken = "c55bdf44-acf6-46b3-be9a-8422bda7032d" +var secret = "INSERT SECRET HERE" +var refreshToken = "INSERT REFRESH TOKEN HERE" +var phone = "INSERT PHONE HERE" func IgnoreTestClient_GetTicketId(t *testing.T) { d, err := createDevice() @@ -84,6 +85,7 @@ func createDevice() (*device.Device, error) { Update: func(string, string) error { return nil }, + Phone: phone, } return d, err } @@ -194,6 +196,20 @@ func IgnoreTestHttpClient_Error(t *testing.T) { } } +func IgnoreTest_AuthByPhone(t *testing.T) { + d, err := createDevice() + if err != nil { + log.Println(err) + t.Fail() + return + } + client := NewClient(baseAddress) + err = client.AuthByPhone(d) + if err != nil { + t.Fatalf("Error auth by phone %v", err) + } +} + func createServer(t *testing.T) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := w.Write([]byte("Hello"))