Skip to content

Commit

Permalink
Merge pull request #249 from rusq/lowercase-wsp
Browse files Browse the repository at this point in the history
Lowercase the user provided workspace name
  • Loading branch information
rusq authored Nov 29, 2023
2 parents c88ef91 + 504857c commit 4d28042
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
19 changes: 14 additions & 5 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/slack-go/slack"
)

const SlackURL = "https://slack.com"

// Type is the auth type.
//
//go:generate stringer -type Type -linecomment
Expand Down Expand Up @@ -41,11 +43,14 @@ type Provider interface {
Validate() error
// Test tests if credentials are valid.
Test(ctx context.Context) error
// Client returns an authenticated HTTP client
HTTPClient() (*http.Client, error)
}

var (
ErrNoToken = errors.New("no token")
ErrNoCookies = errors.New("no cookies")
ErrNoToken = errors.New("no token")
ErrNoCookies = errors.New("no cookies")
ErrNotSupported = errors.New("not supported")
)

type simpleProvider struct {
Expand Down Expand Up @@ -114,16 +119,20 @@ func (s simpleProvider) Test(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "TestAuth")
defer task.End()

httpCl, err := chttp.New("https://slack.com", s.Cookies())
httpCl, err := s.HTTPClient()
if err != nil {
return err
return &Error{Err: err}
}
cl := slack.New(s.Token, slack.OptionHTTPClient(httpCl))

region := trace.StartRegion(ctx, "AuthTestContext")
region := trace.StartRegion(ctx, "simpleProvider.Test")
defer region.End()
if _, err := cl.AuthTestContext(ctx); err != nil {
return &Error{Err: err}
}
return nil
}

func (s simpleProvider) HTTPClient() (*http.Client, error) {
return chttp.New(SlackURL, s.Cookies())
}
7 changes: 6 additions & 1 deletion auth/auth_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import "fmt"
// an API error returned by slack.AuthTest call.
type Error struct {
Err error
Msg string
}

func (ae *Error) Error() string {
return fmt.Sprintf("failed to authenticate: %s", ae.Err)
var msg string = ae.Msg
if msg == "" {
msg = ae.Err.Error()
}
return fmt.Sprintf("authentication error: %s", msg)
}

func (ae *Error) Unwrap() error {
Expand Down
9 changes: 8 additions & 1 deletion auth/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func NewBrowserAuth(ctx context.Context, opts ...Option) (BrowserAuth, error) {
for _, opt := range opts {
opt(&options{browserOpts: &br.opts})
}
if isDocker() {
return BrowserAuth{}, &Error{Err: ErrNotSupported, Msg: "browser auth is not supported in docker, use token/cookie auth instead"}
}

if br.opts.workspace == "" {
var err error
Expand Down Expand Up @@ -70,7 +73,6 @@ func NewBrowserAuth(ctx context.Context, opts ...Option) (BrowserAuth, error) {
Token: token,
Cookie: cookies,
}

return br, nil
}

Expand All @@ -93,3 +95,8 @@ func sanitize(workspace string) (string, error) {
parts := strings.Split(workspace, ".")
return parts[0], nil
}

func isDocker() bool {
_, err := os.Stat("/.dockerenv")
return err == nil
}
17 changes: 9 additions & 8 deletions auth/browser/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import (
"github.com/rusq/slackdump/v2/logger"
)

const slackDomain = ".slack.com"
const (
slackDomain = ".slack.com"
requestTimeout = 600 * time.Second
)

// Client is the client for Browser Auth Provider.
type Client struct {
Expand All @@ -32,11 +35,11 @@ var installFn = playwright.Install

// New create new browser based client.
func New(workspace string, opts ...Option) (*Client, error) {
if workspace == "" {
if strings.TrimSpace(workspace) == "" {
return nil, errors.New("workspace can't be empty")
}
cl := &Client{
workspace: workspace,
workspace: strings.ToLower(workspace),
pageClosed: make(chan bool, 1),
br: Bfirefox,
loginTimeout: float64(DefLoginTimeout.Milliseconds()),
Expand Down Expand Up @@ -142,15 +145,13 @@ func (cl *Client) Authenticate(ctx context.Context) (string, []*http.Cookie, err
return token, convertCookies(state.Cookies), nil
}

var errPageClosed = errors.New("browser closed or timed out")
var ErrBrowserClosed = errors.New("browser closed or timed out")

// withBrowserGuard starts the function fn in a goroutine, and waits for it to
// finish. If the context is canceled, or the page is closed, it returns
// the appropriate error.
func (cl *Client) withBrowserGuard(ctx context.Context, fn func() error) error {
var (
errC = make(chan error)
)
var errC = make(chan error, 1)
go func() {
defer close(errC)
errC <- fn()
Expand All @@ -159,7 +160,7 @@ func (cl *Client) withBrowserGuard(ctx context.Context, fn func() error) error {
case <-ctx.Done():
return ctx.Err()
case <-cl.pageClosed:
return errPageClosed
return ErrBrowserClosed
case err := <-errC:
return err
}
Expand Down
3 changes: 2 additions & 1 deletion auth/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"strings"
"time"

"github.com/rusq/slackdump/v2/auth/browser"
Expand All @@ -23,7 +24,7 @@ func BrowserWithAuthFlow(flow BrowserAuthUI) Option {

func BrowserWithWorkspace(name string) Option {
return func(o *options) {
o.browserOpts.workspace = name
o.browserOpts.workspace = strings.ToLower(name)
}
}

Expand Down

0 comments on commit 4d28042

Please sign in to comment.