-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.go
56 lines (52 loc) · 1.24 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package steps
import (
"context"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/pkg/errors"
"net/http"
"time"
)
type User interface {
signIn(username string) error
isSignedIn() bool
signOut() error
resetUser(fakeApi *FakeApi, ctx context.Context)
setChromeCtx(ctx context.Context)
setFakeApi(fakeApi *FakeApi)
setAuthCookies()
}
func GenerateCookie(name, value, domain, path string, httpOnly bool) *http.Cookie {
if domain == "" {
domain = "localhost"
}
return &http.Cookie{
Name: name,
Value: value,
Path: path,
Domain: domain,
HttpOnly: httpOnly,
Secure: true,
MaxAge: 0,
SameSite: http.SameSiteStrictMode,
}
}
func SetCookies(cookies []*http.Cookie) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) error {
expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
for _, cookie := range cookies {
err := network.SetCookie(cookie.Name, cookie.Value).
WithExpires(&expr).
WithDomain(cookie.Domain).
WithPath(cookie.Path).
WithHTTPOnly(cookie.HttpOnly).
WithSecure(cookie.Secure).
Do(ctx)
if err != nil {
return errors.New("set cookie error")
}
}
return nil
})
}