Skip to content

Commit

Permalink
Merge branch 'v3-enterprise' into v3-viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 28, 2024
2 parents 8a392a4 + 02c69b5 commit b1e9351
Show file tree
Hide file tree
Showing 42 changed files with 4,221 additions and 217 deletions.
13 changes: 7 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Provider interface {
// retrieved.
Validate() error
// Test tests if credentials are valid.
Test(ctx context.Context) error
Test(ctx context.Context) (*slack.AuthTestResponse, error)
// Client returns an authenticated HTTP client
HTTPClient() (*http.Client, error)
}
Expand Down Expand Up @@ -107,22 +107,23 @@ func IsClientToken(tok string) bool {

// TestAuth attempts to authenticate with the given provider. It will return
// AuthError if failed.
func (s simpleProvider) Test(ctx context.Context) error {
func (s simpleProvider) Test(ctx context.Context) (*slack.AuthTestResponse, error) {
ctx, task := trace.NewTask(ctx, "TestAuth")
defer task.End()

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

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

func (s simpleProvider) HTTPClient() (*http.Client, error) {
Expand Down
20 changes: 20 additions & 0 deletions auth/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ func NewValueAuth(token string, cookie string) (ValueAuth, error) {
return c, nil
}

func NewValueCookiesAuth(token string, cookies []*http.Cookie) (ValueAuth, error) {
if token == "" {
return ValueAuth{}, ErrNoToken
}
var found bool
for _, c := range cookies {
if c.Name == "d" {
found = true
break
}
}
if !found {
return ValueAuth{}, ErrNoCookies
}
return ValueAuth{simpleProvider{
Token: token,
Cookie: cookies,
}}, nil
}

var timeFunc = time.Now

func makeCookie(key, val string) *http.Cookie {
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/authcmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func userInfo(ctx context.Context, m manager, name string) (*slack.AuthTestRespo
if err != nil {
return nil, err
}
sess, err := slackdump.New(ctx, prov, slackdump.WithLogger(logger.Silent))
sess, err := slackdump.New(ctx, prov, slackdump.WithLogger(logger.Silent), slackdump.WithForceEnterprise(cfg.ForceEnterprise))
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ var (
LoginTimeout time.Duration = browser.DefLoginTimeout
Limits = slackdump.DefLimits
// playwright stuff
Browser browser.Browser
LegacyBrowser bool
Browser browser.Browser
LegacyBrowser bool
ForceEnterprise bool

DownloadFiles bool
NoChunkCache bool
Expand Down Expand Up @@ -92,6 +93,7 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.Var(&Browser, "browser", "browser to use for EZ-Login 3000 (default: firefox)")
fs.DurationVar(&LoginTimeout, "browser-timeout", LoginTimeout, "Browser login `timeout`")
fs.BoolVar(&LegacyBrowser, "legacy-browser", false, "use legacy browser automation (playwright) for EZ-Login 3000")
fs.BoolVar(&ForceEnterprise, "enterprise", false, "enable Enteprise workarounds")
}
if mask&OmitDownloadFlag == 0 {
fs.BoolVar(&DownloadFiles, "files", true, "enables file attachments (to disable, specify: -files=false)")
Expand Down
34 changes: 34 additions & 0 deletions cmd/slackdump/internal/cfg/slackdump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cfg

import (
"context"

"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/logger"
)

// SlackdumpSession returns the Slackdump Session initialised with the provider
// from context and a standard set of options initialised from the
// configuration. One can provide additional options to override the
// defaults.
func SlackdumpSession(ctx context.Context, opts ...slackdump.Option) (*slackdump.Session, error) {
lg := logger.FromContext(ctx)
prov, err := auth.FromContext(ctx)
if err != nil {
return nil, err
}

var stdOpts = []slackdump.Option{
slackdump.WithLogger(lg),
slackdump.WithForceEnterprise(ForceEnterprise),
slackdump.WithLimits(Limits),
}

stdOpts = append(stdOpts, opts...)
return slackdump.New(
ctx,
prov,
stdOpts...,
)
}
137 changes: 137 additions & 0 deletions cmd/slackdump/internal/diag/edge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package diag

import (
"context"
"encoding/json"
"log/slog"
"os"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/internal/edge"
"github.com/rusq/slackdump/v3/logger"
)

var CmdEdge = &base.Command{
Run: runEdge,
Wizard: func(ctx context.Context, cmd *base.Command, args []string) error { panic("not implemented") },
UsageLine: "slack tools edge",
Short: "Edge test",
RequireAuth: true,
Long: `
# Slack Edge API test tool
Edge test attempts to call the Edge API with the provided credentials.
`,
}

var edgeParams = struct {
channel string
}{}

func init() {
CmdEdge.Flag.StringVar(&edgeParams.channel, "channel", "CHY5HUESG", "channel to get users from")
}

func runEdge(ctx context.Context, cmd *base.Command, args []string) error {
if cfg.Verbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
lg := logger.FromContext(ctx)

prov, err := auth.FromContext(ctx)
if err != nil {
base.SetExitStatus(base.SAuthError)
return err
}

cl, err := edge.New(ctx, prov)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer cl.Close()
lg.Print("connected")

lg.Printf("*** Search for Channels test ***")
channels, err := cl.SearchChannels(ctx, "")
if err != nil {
return err
}
if err := save("channels.json", channels); err != nil {
return err
}

// lg.Printf("*** IMs test ***")
// ims, err := cl.IMList(ctx)
// if err != nil {
// return err
// }
// if err := save("ims.json", ims); err != nil {
// return err
// }

// lg.Printf("*** Counts ***")
// counts, err := cl.ClientCounts(ctx)
// if err != nil {
// return err
// }
// if err := save("counts.json", counts); err != nil {
// return err
// }

// lg.Print("*** GetConversationsContext ***")
// gcc, _, err := cl.GetConversationsContext(ctx, nil)
// if err != nil {
// return err
// }
// if err := save("get_conversations_context.json", gcc); err != nil {
// return err
// }

// lg.Print("*** GetUsersInConversationContext ***")
// if len(gcc) > 0 {
// lg.Printf("using: %s", gcc[0].Name)
// guic, _, err := cl.GetUsersInConversationContext(ctx, &slack.GetUsersInConversationParameters{ChannelID: gcc[0].ID})
// if err != nil {
// return err
// }
// if err := save("get_users_in_conversation_context.json", guic); err != nil {
// return err
// }
// if len(guic) > 0 {
// lg.Print("*** GetUsers ***")
// users, err := cl.GetUsers(ctx, guic...)
// if err != nil {
// return err
// }
// if err := save("get_users.json", users); err != nil {
// return err
// }
// }
// lg.Print("*** Conversations Generic Info ***")
// ci, err := cl.ConversationsGenericInfo(ctx, gcc[0].ID)
// if err != nil {
// return err
// }
// if err := save("conversations_generic_info.json", ci); err != nil {
// return err
// }
// }

lg.Print("OK")
return nil
}

func save(filename string, r any) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
enc.Encode(r)
return err
}
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/eztest.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ezResult struct {

func init() {
CmdEzTest.Flag.Usage = func() {
fmt.Fprint(os.Stdout, "usage: slackdump diag eztest [flags]\n\nFlags:\n")
fmt.Fprint(os.Stdout, "usage: slackdump tools eztest [flags]\n\nFlags:\n")
CmdEzTest.Flag.PrintDefaults()
}
}
Expand Down
23 changes: 8 additions & 15 deletions cmd/slackdump/internal/diag/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"io"
"os"

"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/internal/chunk"
Expand All @@ -18,10 +16,10 @@ import (
var CmdRecord = &base.Command{
UsageLine: "slackdump tools record",
Short: "chunk record commands",
Commands: []*base.Command{CmdRecordStream, CmdRecordState},
Commands: []*base.Command{cmdRecordStream, cmdRecordState},
}

var CmdRecordStream = &base.Command{
var cmdRecordStream = &base.Command{
UsageLine: "slackdump tools record stream [options] <channel>",
Short: "dump slack data in a chunk record format",
Long: `
Expand All @@ -36,7 +34,7 @@ See also: slackdump tool obfuscate
RequireAuth: true,
}

var CmdRecordState = &base.Command{
var cmdRecordState = &base.Command{
UsageLine: "slackdump tools record state [options] <record_file.jsonl>",
Short: "print state of the record",
FlagMask: cfg.OmitAll,
Expand All @@ -46,25 +44,20 @@ var CmdRecordState = &base.Command{

func init() {
// break init cycle
CmdRecordStream.Run = runRecord
cmdRecordStream.Run = runRecord
}

var output = CmdRecordStream.Flag.String("output", "", "output file")
var output = cmdRecordStream.Flag.String("output", "", "output file")

func runRecord(ctx context.Context, _ *base.Command, args []string) error {
if len(args) == 0 {
base.SetExitStatus(base.SInvalidParameters)
return errors.New("missing channel argument")
}

prov, err := auth.FromContext(ctx)
sess, err := cfg.SlackdumpSession(ctx)
if err != nil {
base.SetExitStatus(base.SAuthError)
return err
}
sess, err := slackdump.New(ctx, prov)
if err != nil {
base.SetExitStatus(base.SWorkspaceError)
base.SetExitStatus(base.SInitializationError)
return err
}

Expand Down Expand Up @@ -110,7 +103,7 @@ func runRecord(ctx context.Context, _ *base.Command, args []string) error {

func init() {
// break init cycle
CmdRecordState.Run = runRecordState
cmdRecordState.Run = runRecordState
}

func runRecordState(ctx context.Context, _ *base.Command, args []string) error {
Expand Down
Loading

0 comments on commit b1e9351

Please sign in to comment.