Skip to content

Commit

Permalink
LoginCancel
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 5, 2024
1 parent dd24baf commit e8b7d6e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ var (
ErrNoToken = errors.New("no token")
ErrNoCookies = errors.New("no cookies")
ErrNotSupported = errors.New("not supported")
// ErrCancelled may be returned by auth providers, if the authentication
// process was cancelled.
ErrCancelled = errors.New("authentication cancelled")
)

type simpleProvider struct {
Expand Down
5 changes: 3 additions & 2 deletions auth/auth_ui/auth_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

const (
LoginEmail = 0
LoginSSO = 1
LoginEmail = 0
LoginSSO = 1
LoginCancel = 2
)

func Sanitize(workspace string) (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions auth/auth_ui/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func (cl *CLI) RequestLoginType(w io.Writer) (int, error) {
{"Google", LoginSSO},
{"Apple", LoginSSO},
{"Login with Single-Sign-On (SSO)", LoginSSO},
{"Other", LoginSSO},
{"Other/Manual", LoginSSO},
{"Cancel", LoginCancel},
}

var idx int = -1
Expand All @@ -60,7 +61,7 @@ func (cl *CLI) RequestLoginType(w io.Writer) (int, error) {
for i, t := range types {
fmt.Fprintf(w, "\t%d. %s\n", i+1, t.name)
}
fmt.Fprintf(w, "Enter number: ")
fmt.Fprintf(w, "Enter number, and press Enter: ")

_, err := fmt.Fscanf(os.Stdin, "%d", &idx)
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions auth/auth_ui/huh.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth_ui

import (
"fmt"
"io"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -29,8 +30,13 @@ func (*Huh) Stop() {}
func (*Huh) RequestCreds(w io.Writer, workspace string) (email string, passwd string, err error) {
f := huh.NewForm(
huh.NewGroup(
huh.NewInput().Title("Slack email").Inline(true).Value(&email).Validate(valAND(valEmail, valRequired)),
huh.NewInput().Title("Slack password").Inline(true).Value(&passwd).Password(true).Validate(valRequired).Password(true),
huh.NewInput().
Title("You Slack Login Email").Value(&email).
Description(fmt.Sprintf("This is the email that you log into %s with.", workspace)).
Validate(valAND(valEmail, valRequired)),
huh.NewInput().
Title("Password").Value(&passwd).
Validate(valRequired).Password(true),
),
)
err = f.Run()
Expand All @@ -45,7 +51,9 @@ func (*Huh) RequestLoginType(w io.Writer) (int, error) {
huh.NewOption("Google", LoginSSO),
huh.NewOption("Apple", LoginSSO),
huh.NewOption("Login with Single-Sign-On (SSO)", LoginSSO),
huh.NewOption("Other", LoginSSO),
huh.NewOption("Other/Manual", LoginSSO),
huh.NewOption("------", LoginCancel),
huh.NewOption("Cancel", LoginCancel),
).
Value(&loginType).
Description("If you are not sure, select 'Other'.").
Expand Down
12 changes: 9 additions & 3 deletions auth/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
} else {
r.opts.workspace = wsp
}

resp, err := r.opts.ui.RequestLoginType(os.Stdout)
if err != nil {
return r, err
}

var sp simpleProvider
if resp == auth_ui.LoginSSO {
switch resp {
case auth_ui.LoginSSO:
var err error
sp.Token, sp.Cookie, err = slackauth.Browser(ctx, r.opts.workspace)
if err != nil {
return r, err
}
} else {
case auth_ui.LoginEmail:
var err error
username, password, err := r.opts.ui.RequestCreds(os.Stdout, r.opts.workspace)
if err != nil {
Expand All @@ -83,13 +86,16 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
return r, fmt.Errorf("password cannot be empty")
}
sctx, cancel := context.WithCancel(ctx)
go spinner.New().Title("Logging in...").Context(sctx).Run()
spin := spinner.New().Title("Logging in...").Context(sctx)
go spin.Run()
sp.Token, sp.Cookie, err = slackauth.Headless(ctx, r.opts.workspace, username, password)
cancel()
if err != nil {
return r, err
}
fmt.Fprintln(os.Stderr, "authenticated.")
case auth_ui.LoginCancel:
return r, ErrCancelled
}

return RodAuth{
Expand Down
5 changes: 5 additions & 0 deletions cmd/slackdump/internal/auth/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"context"
"errors"
"fmt"

"github.com/rusq/slackdump/v2/auth"
Expand Down Expand Up @@ -60,6 +61,10 @@ func runWspNew(ctx context.Context, cmd *base.Command, args []string) error {
prov, err := m.Auth(ctx, wsp, creds)
if err != nil {
base.SetExitStatus(base.SAuthError)
if errors.Is(err, auth.ErrCancelled) {
lg.Println(auth.ErrCancelled)
return nil
}
return err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.4

require (
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403
github.com/charmbracelet/huh v0.2.3
github.com/charmbracelet/huh v0.2.4-0.20240104183627-a92fbe913dd7
github.com/charmbracelet/huh/spinner v0.0.0-20231222231237-4bd4657a36ac
github.com/fatih/color v1.16.0
github.com/go-playground/locales v0.14.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/charmbracelet/glamour v0.6.0 h1:wi8fse3Y7nfcabbbDuwolqTqMQPMnVPeZhDM2
github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8om2538k9ITBxOc=
github.com/charmbracelet/huh v0.2.3 h1:fZaqnd/fiO7jlfcLqhP2iwpLt670IaHQfL/7Qu+fBm0=
github.com/charmbracelet/huh v0.2.3/go.mod h1:XmADLRnJs/Jqw7zIbi9BTss5gXbOkR6feyVoNAp19rA=
github.com/charmbracelet/huh v0.2.4-0.20240104183627-a92fbe913dd7 h1:RMo0SwJr+Nw+0rePd09AtOuvo75eSfMPgzJIIk8Rhvc=
github.com/charmbracelet/huh v0.2.4-0.20240104183627-a92fbe913dd7/go.mod h1:NlYSEnyQxG6pAy5MfrUXJdJOYQv691EwTLbjo8Nw3us=
github.com/charmbracelet/huh/spinner v0.0.0-20231222231237-4bd4657a36ac h1:sl0PfzeQCpHWWNxs9iimnTivB5WK+ln85mBBNFxLFOg=
github.com/charmbracelet/huh/spinner v0.0.0-20231222231237-4bd4657a36ac/go.mod h1:CM9bYw6ky86T3ZCniTp4Mj4n3TXwwp8GCHxeUsKiITo=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
Expand Down

0 comments on commit e8b7d6e

Please sign in to comment.