forked from koltyakov/gosip
/
helpers.go
139 lines (113 loc) · 3.3 KB
/
helpers.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package tmg
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/patrickmn/go-cache"
)
var (
storage = cache.New(5*time.Minute, 10*time.Minute)
)
// GetAuth gets authentication
func GetAuth(ctx context.Context, c *AuthCnfg) (string, int64, error) {
if c.client == nil {
c.client = &http.Client{}
}
parsedURL, err := url.Parse(c.SiteURL)
if err != nil {
return "", 0, err
}
cacheKey := parsedURL.Host + "@" + c.GetStrategy() + "@" + c.Username + "@" + c.Password
if accessToken, exp, found := storage.GetWithExpiration(cacheKey); found {
return accessToken.(string), exp.Unix(), nil
}
redirect, err := detectCookieAuthURL(ctx, c, c.SiteURL)
if err != nil {
return "", 0, err
}
endpoint := fmt.Sprintf("%s://%s/CookieAuth.dll?Logon", parsedURL.Scheme, parsedURL.Host)
params := url.Values{}
querystr := strings.Replace(redirect.RawQuery, "GetLogon?", "", 1)
for _, part := range strings.Split(querystr, "&") {
p := strings.Split(part, "=")
if len(p) == 2 {
params.Set(p[0], p[1])
}
}
params.Set("username", c.Username)
params.Set("password", c.Password)
// TODO: keepalive agent for https
// client := &http.Client{
// CheckRedirect: func(req *http.Request, via []*http.Request) error {
// return http.ErrUseLastResponse
// },
// }
c.client.CheckRedirect = doNotCheckRedirect
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(params.Encode()))
if err != nil {
return "", 0, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.client.Do(req)
if err != nil {
return "", 0, err
}
defer func() {
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
}()
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
return "", 0, err
}
// fmt.Println(resp.StatusCode)
authCookie := resp.Header.Get("Set-Cookie") // TODO: parse TMG cookie only (?)
// TODO: ttl detection
expiry := time.Hour
exp := time.Now().Add(expiry).Unix()
storage.Set(cacheKey, authCookie, expiry)
return authCookie, exp, nil
}
func detectCookieAuthURL(ctx context.Context, c *AuthCnfg, siteURL string) (*url.URL, error) {
if c.client == nil {
c.client = &http.Client{}
}
// client := &http.Client{
// CheckRedirect: func(req *http.Request, via []*http.Request) error {
// return http.ErrUseLastResponse
// },
// }
c.client.CheckRedirect = doNotCheckRedirect
req, err := http.NewRequestWithContext(ctx, "GET", siteURL, nil)
if err != nil {
return nil, err
}
// req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
// req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer func() {
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
}()
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
return nil, err
}
redirect, err := resp.Location()
if err != nil {
return nil, err
}
return redirect, nil
}
// doNotCheckRedirect *http.Client CheckRedirect callback to ignore redirects
func doNotCheckRedirect(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}