Skip to content

Commit

Permalink
extract email flow
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 7, 2024
1 parent cde920c commit 0deb05b
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions auth/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"time"

"github.com/rusq/slackauth"
"github.com/rusq/slackdump/v2/auth/auth_ui"
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 0deb05b

Please sign in to comment.