-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials.go
64 lines (56 loc) · 1.85 KB
/
credentials.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
package slack
import (
"fmt"
"github.com/browserutils/kooky"
_ "github.com/browserutils/kooky/browser/all" // register cookie store finders!
"io"
"net/http"
"regexp"
"sort"
)
// Slack _needs_ user agent pretending to be Chrome, otherwise it does not give back a token
const fakeUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
func GetDCookie() (*http.Cookie, error) {
cookies := kooky.ReadCookies(kooky.Valid, kooky.DomainHasSuffix(`slack.com`), kooky.Name("d"))
// Sort found cookies by expiry date, in reverse, to get the most "fresh" cookie
sort.Slice(cookies, func(i, j int) bool {
return cookies[i].Expires.After(cookies[j].Expires)
})
if len(cookies) > 0 {
return &cookies[0].Cookie, nil
} else {
return nil, fmt.Errorf("slack's 'd' cookie wasn't found")
}
}
func ScrapeToken(team string, dCookie *http.Cookie) (string, error) {
// Just a page where token is exposed as part of JSON in the page body itself
teamUrl := fmt.Sprintf("https://%s.slack.com/customize/emoji", team)
req, err := http.NewRequest("GET", teamUrl, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", fakeUserAgent)
req.AddCookie(dCookie)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
if (resp.StatusCode < 200) || (resp.StatusCode > 299) {
return "", fmt.Errorf("unexpected HTTP response status code: %d", resp.StatusCode)
}
// Make sure the body is closed
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
html, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Find the API token in the page body
re := regexp.MustCompile(`"api_token":"(xo[^"]+)"`)
match := re.FindSubmatch(html)
if match == nil {
return "", fmt.Errorf("token is not found in the HTML body")
}
return string(match[1]), nil
}