diff --git a/auth/rod.go b/auth/rod.go index 596cdea2..31e7c255 100644 --- a/auth/rod.go +++ b/auth/rod.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "time" "github.com/rusq/slackauth" "github.com/rusq/slackdump/v2/auth/auth_ui" @@ -31,8 +30,6 @@ type browserAuthUIExt interface { ConfirmationCode(email string) (code int, err error) } -const expectedLoginDuration = 16 * time.Second - func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) { r := RodAuth{ opts: options{ @@ -74,30 +71,10 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) { return r, err } case auth_ui.LoginEmail: - username, password, err := r.opts.ui.RequestCreds(os.Stdout, r.opts.workspace) + sp, err = headlessFlow(ctx, r.opts.workspace, r.opts.ui) if err != nil { return r, err } - if username == "" { - return r, fmt.Errorf("email cannot be empty") - } - if password == "" { - return r, fmt.Errorf("password cannot be empty") - } - fmt.Println("Logging in to Slack, depending on your connection speed, it usually takes 10-20 seconds...") - - var loginErr error - sp.Token, sp.Cookie, loginErr = slackauth.Headless( - ctx, - r.opts.workspace, - username, - password, - slackauth.WithChallengeFunc(r.opts.ui.ConfirmationCode), - ) - if loginErr != nil { - return r, loginErr - } - fmt.Fprintln(os.Stderr, "authenticated.") case auth_ui.LoginCancel: return r, ErrCancelled @@ -107,3 +84,32 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) { simpleProvider: sp, }, nil } + +func headlessFlow(ctx context.Context, workspace string, ui browserAuthUIExt) (sp simpleProvider, err error) { + username, password, err := ui.RequestCreds(os.Stdout, workspace) + if err != nil { + return sp, err + } + if username == "" { + return sp, fmt.Errorf("email cannot be empty") + } + if password == "" { + return sp, fmt.Errorf("password cannot be empty") + } + fmt.Println("Logging in to Slack, depending on your connection speed, it will take 15-30 seconds...") + + var loginErr error + sp.Token, sp.Cookie, loginErr = slackauth.Headless( + ctx, + workspace, + username, + password, + slackauth.WithChallengeFunc(ui.ConfirmationCode), + ) + if loginErr != nil { + return sp, loginErr + } + + fmt.Fprintln(os.Stderr, "authenticated.") + return +}