Skip to content

Commit

Permalink
implement rod auth
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Dec 29, 2023
1 parent c9bcadf commit dd0eae9
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 3 deletions.
30 changes: 30 additions & 0 deletions auth/auth_ui/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/fatih/color"
"golang.org/x/term"
)

type CLI struct{}
Expand All @@ -32,6 +33,35 @@ func (cl *CLI) RequestWorkspace(w io.Writer) (string, error) {
return workspace, nil
}

func (cl *CLI) RequestEmail(w io.Writer) (string, error) {
fmt.Fprint(w, "Enter Email: ")
username, err := readln(os.Stdin)
if err != nil {
return "", err
}
return username, nil
}

func (cl *CLI) RequestPassword(w io.Writer) (string, error) {
fmt.Fprint(w, "Enter Password (won't be visible): ")
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
fmt.Fprintln(w)
return string(password), nil
}

func (cl *CLI) YesNo(w io.Writer, message string) (bool, error) {
fmt.Fprintf(w, "%s [y/N]: ", message)
answer, err := readln(os.Stdin)
if err != nil {
return false, err
}
answer = strings.ToLower(answer)
return answer == "y" || answer == "yes", nil
}

func (*CLI) Stop() {}

func readln(r io.Reader) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions auth/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type options struct {
*browserOpts
*rodOpts
}

type Option func(*options)
Expand Down
87 changes: 87 additions & 0 deletions auth/rod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package auth

import (
"context"
"fmt"
"os"

"github.com/rusq/slackauth"
"github.com/rusq/slackdump/v2/auth/auth_ui"
)

type RodAuth struct {
simpleProvider
opts rodOpts
}

func (p RodAuth) Type() Type {
return TypeRod
}

type rodOpts struct {
ui BrowserAuthUIExt
workspace string
}

func RodWithWorkspace(name string) Option {
return func(o *options) {
o.rodOpts.workspace = name
}
}

func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
r := RodAuth{
opts: rodOpts{
ui: &auth_ui.CLI{},
},
}
for _, opt := range opts {
opt(&options{
rodOpts: &r.opts,
})
}
if r.opts.workspace == "" {
var err error
r.opts.workspace, err = r.opts.ui.RequestWorkspace(os.Stdout)
if err != nil {
return r, err
}
}
if wsp, err := sanitize(r.opts.workspace); err != nil {
return r, err
} else {
r.opts.workspace = wsp
}
yes, err := r.opts.ui.YesNo(os.Stdout, "Does your Slack Workspace use Single Sign On (i.e. Google, Okta, Auth0 etc.)?\n\tIf unsure, say yes")
if err != nil {
return r, err
}
var sp simpleProvider
if yes {
var err error
sp.Token, sp.Cookie, err = slackauth.Browser(ctx, r.opts.workspace)
if err != nil {
return r, err
}
} else {
var err error
username, err := r.opts.ui.RequestEmail(os.Stdout)
if err != nil {
return r, err
}
password, err := r.opts.ui.RequestPassword(os.Stdout)
if err != nil {
return r, err
}
fmt.Fprintln(os.Stderr, "Please wait while slackdump authenticates with Slack...")
sp.Token, sp.Cookie, err = slackauth.Headless(ctx, r.opts.workspace, username, password)
if err != nil {
return r, err
}
fmt.Fprintln(os.Stderr, "authenticated.")
}

return RodAuth{
simpleProvider: sp,
}, nil
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rusq/slackdump/v2

go 1.18
go 1.21.4

require (
github.com/AlecAivazis/survey/v2 v2.3.7
Expand All @@ -26,6 +26,7 @@ require (
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/go-rod/rod v0.114.5 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
Expand All @@ -37,6 +38,12 @@ require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rusq/slackauth v0.0.1 // indirect
github.com/ysmood/fetchup v0.2.4 // indirect
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/got v0.38.2 // indirect
github.com/ysmood/gson v0.7.3 // indirect
github.com/ysmood/leakless v0.8.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-rod/rod v0.114.5 h1:1x6oqnslwFVuXJbJifgxspJUd3O4ntaGhRLHt+4Er9c=
github.com/go-rod/rod v0.114.5/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
Expand Down Expand Up @@ -73,6 +75,8 @@ github.com/rusq/osenv/v2 v2.0.1 h1:1LtNt8VNV/W86wb38Hyu5W3Rwqt/F1JNRGE+8GRu09o=
github.com/rusq/osenv/v2 v2.0.1/go.mod h1:+wJBSisjNZpfoD961JzqjaM+PtaqSusO3b4oVJi7TFY=
github.com/rusq/secure v0.0.4 h1:svpiZHfHnx89eEDCCFI9OXG1Y8hL9kUWUG6fJbrWUOI=
github.com/rusq/secure v0.0.4/go.mod h1:F1QilMKreuFRjov0UY7DZSIXn77/8RqMVGu2zV0RtqY=
github.com/rusq/slackauth v0.0.1 h1:UYW/lMr+FMnKeax40L55VpY40Ekmsoc0MhScdL4fopc=
github.com/rusq/slackauth v0.0.1/go.mod h1:zb1PJY2+8uEqn0RiWuRjnd+ZFwwfnvA5xrGoooVUgNY=
github.com/rusq/tracer v1.0.1 h1:5u4PCV8NGO97VuAINQA4gOVRkPoqHimLE2jpezRVNMU=
github.com/rusq/tracer v1.0.1/go.mod h1:Rqu48C3/K8bA5NPmF20Hft73v431MQIdM+Co+113pME=
github.com/schollz/progressbar/v3 v3.13.0 h1:9TeeWRcjW2qd05I8Kf9knPkW4vLM/hYoa6z9ABvxje8=
Expand All @@ -85,6 +89,20 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ysmood/fetchup v0.2.3/go.mod h1:xhibcRKziSvol0H1/pj33dnKrYyI2ebIvz5cOOkYGns=
github.com/ysmood/fetchup v0.2.4 h1:2kfWr/UrdiHg4KYRrxL2Jcrqx4DZYD+OtWu7WPBZl5o=
github.com/ysmood/fetchup v0.2.4/go.mod h1:hbysoq65PXL0NQeNzUczNYIKpwpkwFL4LXMDEvIQq9A=
github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ=
github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18=
github.com/ysmood/gop v0.0.2/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
github.com/ysmood/got v0.34.1/go.mod h1:yddyjq/PmAf08RMLSwDjPyCvHvYed+WjHnQxpH851LM=
github.com/ysmood/got v0.38.2 h1:h2RYvAe5nIK+oBRMLzNIrkZaX5kjmkOBqfRMIsFzLyU=
github.com/ysmood/got v0.38.2/go.mod h1:W7DdpuX6skL3NszLmAsC5hT7JAhuLZhByVzHTq874Qg=
github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM=
github.com/ysmood/gson v0.7.3 h1:QFkWbTH8MxyUTKPkVWAENJhxqdBa4lYTQWqZCiLG6kE=
github.com/ysmood/gson v0.7.3/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg=
github.com/ysmood/leakless v0.8.0 h1:BzLrVoiwxikpgEQR0Lk8NyBN5Cit2b1z+u0mgL4ZJak=
github.com/ysmood/leakless v0.8.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
6 changes: 4 additions & 2 deletions internal/app/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func (c SlackCreds) Type(ctx context.Context) (auth.Type, error) {
return auth.TypeInvalid, ErrUnsupported
}
if !ezLoginTested() {
return auth.TypeBrowser, ErrNotTested
return auth.TypeRod, ErrNotTested
}
return auth.TypeBrowser, nil
return auth.TypeRod, nil

}

Expand All @@ -73,6 +73,8 @@ func (c SlackCreds) AuthProvider(ctx context.Context, workspace string, browser
return nil, err
}
switch authType {
case auth.TypeRod:
return auth.NewRODAuth(ctx, auth.RodWithWorkspace(workspace))
case auth.TypeBrowser:
return auth.NewBrowserAuth(ctx, auth.BrowserWithWorkspace(workspace), auth.BrowserWithBrowser(browser))
case auth.TypeCookieFile:
Expand Down

0 comments on commit dd0eae9

Please sign in to comment.