-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackauth.go
194 lines (169 loc) · 4.95 KB
/
slackauth.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package slackauth
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
)
const domain = ".slack.com"
type Option func(*options)
type options struct {
cookies []*http.Cookie
// codeFn is the function that is called when slack does not recognise the browser and challenges the user with a code sent to email.
// it must return the user-entered code.
codeFn func(email string) (code int, err error)
debug bool
lg Logger
}
func (o *options) apply(opts []Option) {
for _, opt := range opts {
opt(o)
}
}
// WithNoConsentPrompt adds a cookie that disables the Cookie Consent banner.
func WithNoConsentPrompt() Option {
return func(o *options) {
cookie := &http.Cookie{
Domain: domain,
Path: "/",
Name: "OptanonAlertBoxClosed",
Value: time.Now().Add(-10 * time.Minute).Format(time.RFC3339),
Expires: time.Now().AddDate(0, 0, 30),
}
WithCookie(cookie)(o)
}
}
// WithCookie adds a cookie to the request.
func WithCookie(cookie ...*http.Cookie) Option {
return func(o *options) {
o.cookies = append(o.cookies, cookie...)
}
}
type Logger interface {
// Debug logs a debug message.
Debug(msg string, keyvals ...interface{})
}
func WithLogger(l Logger) Option {
return func(o *options) {
o.lg = l
}
}
// WithChallengeFunc sets the function that is called when slack does not
// recognise the browser and challenges the user with a code sent to email.
// All the function has to do is to accept the user input and return the code.
//
// See [SimpleChallengeFn](#SimpleChallengeFn) for an example.
func WithChallengeFunc(fn func(email string) (code int, err error)) Option {
return func(o *options) {
o.codeFn = fn
}
}
// WithDebug enables debug logging.
func WithDebug(b bool) Option {
return func(o *options) {
o.debug = b
}
}
var (
// ErrInvalidCredentials indicates that the credentials were invalid.
ErrInvalidCredentials = errors.New("invalid credentials")
// ErrLoginError indicates that some error of unknown nature occurred
// during login.
ErrLoginError = errors.New("slack reported an error during login")
// ErrWorkspaceNotFound indicates that the workspace name was invalid.
ErrWorkspaceNotFound = errors.New("workspace not found")
)
// ErrBadWorkspace is returned when the workspace name is invalid.
type ErrBadWorkspace struct {
Name string
}
func (e ErrBadWorkspace) Error() string {
return fmt.Sprintf("invalid workspace name: %q", e.Name)
}
// ErrBrowser indicates the error with browser interaction.
type ErrBrowser struct {
Err error
FailedTo string
}
func (e ErrBrowser) Error() string {
return fmt.Sprintf("browser automation error: failed to %s: %v", e.FailedTo, e.Err)
}
func isURLSafe(s string) bool {
for _, r := range s {
if !isURLSafeRune(r) {
return false
}
}
return true
}
func isURLSafeRune(r rune) bool {
return (r >= 'a' && r <= 'z') ||
(r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') ||
r == '-' || r == '_' || r == '.' || r == '~'
}
func workspaceURL(workspace string) (string, error) {
if !isURLSafe(workspace) {
return "", ErrBadWorkspace{Name: workspace}
}
return "https://" + workspace + domain + "/", nil
}
// withTabGuard creates a context that is cancelled when the target is
// destroyed.
func withTabGuard(parent context.Context, browser *rod.Browser, targetID proto.TargetTargetID, l Logger) (context.Context, context.CancelCauseFunc) {
ctx, cancel := context.WithCancelCause(parent)
go browser.EachEvent(func(e *proto.TargetTargetDestroyed) {
if e.TargetID != targetID {
// skipping unrelated target (user opened pages)
return
}
l.Debug("target destroyed", "target", e.TargetID)
cancel(errors.New("target page is closed"))
})()
return ctx, cancel
}
// checkWorkspaceURL checks if the workspace exists. Slack returns 200 on
// existing workspaces and 404 on non-existing ones.
func checkWorkspaceURL(uri string) error {
// quick status check
if resp, err := http.Head(uri); err != nil {
return ErrWorkspaceNotFound
} else if resp.StatusCode != http.StatusOK {
return ErrWorkspaceNotFound
}
return nil
}
// extractCookies extracts cookies from the browser and returns them as a
// slice of http.Cookie.
func extractCookies(browser *rod.Browser) ([]*http.Cookie, error) {
cook, err := browser.GetCookies()
if err != nil {
return nil, err
}
var cookies = make([]*http.Cookie, 0, len(cook))
for _, c := range cook {
sameSite, ok := sameSiteMap[c.SameSite]
if !ok {
sameSite = http.SameSiteNoneMode
}
cookies = append(cookies, &http.Cookie{
Name: c.Name,
Value: c.Value,
Domain: c.Domain,
Path: c.Path,
Expires: c.Expires.Time(),
Secure: c.Secure,
HttpOnly: c.HTTPOnly,
SameSite: sameSite,
})
}
return cookies, nil
}
var sameSiteMap = map[proto.NetworkCookieSameSite]http.SameSite{
proto.NetworkCookieSameSiteNone: http.SameSiteNoneMode,
proto.NetworkCookieSameSiteLax: http.SameSiteLaxMode,
proto.NetworkCookieSameSiteStrict: http.SameSiteStrictMode,
}