seclient: clean login

This commit is contained in:
Vladislav Yarmak
2021-04-02 01:56:17 +03:00
parent 1deeb512cf
commit 51933dcffe
+31 -13
View File
@@ -73,6 +73,8 @@ type SEClient struct {
rng *rand.Rand rng *rand.Rand
} }
type StrKV map[string]string
// Instantiates SurfEasy client with default settings and given API keys. // Instantiates SurfEasy client with default settings and given API keys.
// Optional `transport` parameter allows to override HTTP transport used // Optional `transport` parameter allows to override HTTP transport used
// for HTTP calls // for HTTP calls
@@ -81,13 +83,6 @@ func NewSEClient(apiUsername, apiSecret string, transport http.RoundTripper) (*S
transport = http.DefaultTransport transport = http.DefaultTransport
} }
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
return nil, err
}
rng := rand.New(RandomSource) rng := rand.New(RandomSource)
device_id, err := randomCapitalHexString(rng, DEVICE_ID_BYTES) device_id, err := randomCapitalHexString(rng, DEVICE_ID_BYTES)
@@ -95,18 +90,36 @@ func NewSEClient(apiUsername, apiSecret string, transport http.RoundTripper) (*S
return nil, err return nil, err
} }
return &SEClient{ res := &SEClient{
HttpClient: &http.Client{ HttpClient: &http.Client{
Transport: dac.NewDigestTransport(apiUsername, apiSecret, transport), Transport: dac.NewDigestTransport(apiUsername, apiSecret, transport),
Jar: jar,
}, },
Settings: DefaultSESettings, Settings: DefaultSESettings,
rng: rng, rng: rng,
DeviceID: device_id, DeviceID: device_id,
}, nil }
err = res.ResetCookies()
if err != nil {
return nil, err
}
return res, nil
} }
type StrKV map[string]string func (c *SEClient) ResetCookies() error {
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
return err
}
c.StateMux.Lock()
c.HttpClient.Jar = jar
c.StateMux.Unlock()
return nil
}
func (c *SEClient) AnonRegister(ctx context.Context) error { func (c *SEClient) AnonRegister(ctx context.Context) error {
localPart, err := randomEmailLocalPart(c.rng) localPart, err := randomEmailLocalPart(c.rng)
@@ -202,8 +215,13 @@ func (c *SEClient) Discover(ctx context.Context, requestedGeo string) ([]SEIPEnt
} }
func (c *SEClient) Login(ctx context.Context) error { func (c *SEClient) Login(ctx context.Context) error {
err := c.ResetCookies()
if err != nil {
return err
}
var loginRes SESubscriberLoginResponse var loginRes SESubscriberLoginResponse
err := c.RpcCall(ctx, c.Settings.Endpoints.SubscriberLogin, StrKV{ err = c.RpcCall(ctx, c.Settings.Endpoints.SubscriberLogin, StrKV{
"login": c.SubscriberEmail, "login": c.SubscriberEmail,
"password": c.SubscriberPassword, "password": c.SubscriberPassword,
"client_type": c.Settings.ClientType, "client_type": c.Settings.ClientType,
@@ -277,7 +295,7 @@ func (c *SEClient) RpcCall(ctx context.Context, endpoint string, params map[stri
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad http status: %s", resp.Status) return fmt.Errorf("bad http status: %s, headers: %#v", resp.Status, resp.Header)
} }
decoder := json.NewDecoder(resp.Body) decoder := json.NewDecoder(resp.Body)