Skip to content

Commit

Permalink
Merge pull request #257 from rusq/rod
Browse files Browse the repository at this point in the history
ROD
  • Loading branch information
rusq authored Dec 30, 2023
2 parents 39fe92c + 607fa8e commit 6338249
Show file tree
Hide file tree
Showing 45 changed files with 664 additions and 549 deletions.
70 changes: 0 additions & 70 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.20"
go-version: "1.21"

- name: Build
run: go build -v ./...
Expand Down
1 change: 1 addition & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
TypeValue // Value
TypeCookieFile // Cookie File
TypeBrowser // EZ-Login 3000
TypeRod // EZ-Login 3001
)

// Provider is the Slack Authentication provider.
Expand Down
27 changes: 27 additions & 0 deletions auth/auth_ui/auth_ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package auth_ui

import (
"net/url"
"strings"
)

const (
LoginEmail = 0
LoginSSO = 1
)

func Sanitize(workspace string) (string, error) {
if !strings.Contains(workspace, ".slack.com") {
return workspace, nil
}
if strings.HasPrefix(workspace, "https://") {
uri, err := url.Parse(workspace)
if err != nil {
return "", err
}
workspace = uri.Host
}
// parse
parts := strings.Split(workspace, ".")
return parts[0], nil
}
6 changes: 3 additions & 3 deletions auth/browser_test.go → auth/auth_ui/auth_ui_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package auth
package auth_ui

import "testing"

func Test_sanitize(t *testing.T) {
func TestSanitize(t *testing.T) {
type args struct {
workspace string
}
Expand All @@ -20,7 +20,7 @@ func Test_sanitize(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := sanitize(tt.args.workspace)
got, err := Sanitize(tt.args.workspace)
if (err != nil) != tt.wantErr {
t.Errorf("sanitize() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
75 changes: 71 additions & 4 deletions auth/auth_ui/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"strings"

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

// CLI is the archaic fallback UI for auth.
type CLI struct{}

func (*CLI) instructions(w io.Writer) {
Expand All @@ -24,20 +26,85 @@ func (*CLI) instructions(w io.Writer) {

func (cl *CLI) RequestWorkspace(w io.Writer) (string, error) {
cl.instructions(w)
fmt.Fprint(w, "Enter Slack Workspace Name: ")
workspace, err := readln(os.Stdin)
workspace, err := prompt(w, "Enter Slack Workspace Name or URL: ", readln)
if err != nil {
return "", err
}
return workspace, nil
return Sanitize(workspace)
}

func (cl *CLI) RequestEmail(w io.Writer) (string, error) {
return prompt(w, "Enter Email: ", readln)
}

func (cl *CLI) RequestPassword(w io.Writer, account string) (string, error) {
defer fmt.Fprintln(w)
return prompt(w, fmt.Sprintf("Enter Password for %s (won't be visible): ", account), readpwd)
}

func (cl *CLI) RequestLoginType(w io.Writer) (int, error) {
var types = []struct {
name string
value int
}{
{"Email", LoginEmail},
{"Google", LoginSSO},
{"Apple", LoginSSO},
{"Login with Single-Sign-On (SSO)", LoginSSO},
{"Other", LoginSSO},
}

var idx int = -1
for idx < 0 || idx >= len(types) {
fmt.Fprintf(w, "Select login type:\n")
for i, t := range types {
fmt.Fprintf(w, "\t%d. %s\n", i+1, t.name)
}
fmt.Fprintf(w, "Enter number: ")

_, err := fmt.Fscanf(os.Stdin, "%d", &idx)
if err != nil {
fmt.Fprintln(w, err)
continue
}

idx -= 1 // adjusting for 0-index

if idx < 0 || idx >= len(types) {
fmt.Fprintln(w, "invalid login type")
}
}
return types[idx].value, nil
}

func (*CLI) Stop() {}

func readln(r io.Reader) (string, error) {
func readln(r *os.File) (string, error) {
line, err := bufio.NewReader(r).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(line), nil
}

func readpwd(f *os.File) (string, error) {
pwd, err := term.ReadPassword(int(f.Fd()))
if err != nil {
return "", err
}
return string(pwd), nil
}

func prompt(w io.Writer, prompt string, readlnFn func(*os.File) (string, error)) (string, error) {
for {
fmt.Fprint(w, prompt)
v, err := readlnFn(os.Stdin)
if err != nil {
return "", err
}
if v != "" {
return v, nil
}
fmt.Fprintln(w, "input cannot be empty")
}
}
61 changes: 61 additions & 0 deletions auth/auth_ui/huh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package auth_ui

import (
"io"

"github.com/charmbracelet/huh"
)

type Huh struct {
theme huh.Theme
}

func (*Huh) RequestWorkspace(w io.Writer) (string, error) {
var workspace string
huh.NewInput().
Title("Enter Slack workspace name").
Value(&workspace).
Validate(valRequired).
Description("The workspace name is the part of the URL that comes before `.slack.com' in\nhttps://<workspace>.slack.com/. Both workspace name or URL are acceptable.").
Run()
return Sanitize(workspace)
}

func (*Huh) Stop() {}

func (*Huh) RequestEmail(w io.Writer) (string, error) {
var email string
huh.NewInput().Title("Enter Slack login email").
Value(&email).
Description("The email that you use to login to Slack.").
Validate(valAND(valEmail, valRequired)).
Run()
return email, nil
}

func (*Huh) RequestPassword(w io.Writer, account string) (string, error) {
var password string
huh.NewInput().Title("Enter password for " + account).
Value(&password).
Password(true).
Description("This is your Slack password, it will not be saved.").
Validate(valRequired).
Run()
return password, nil
}

func (*Huh) RequestLoginType(w io.Writer) (int, error) {
var loginType int
err := huh.NewSelect[int]().Title("Select login type").
Options(
huh.NewOption("Email", LoginEmail),
huh.NewOption("Google", LoginSSO),
huh.NewOption("Apple", LoginSSO),
huh.NewOption("Login with Single-Sign-On (SSO)", LoginSSO),
huh.NewOption("Other", LoginSSO),
).
Value(&loginType).
Description("If you are not sure, select 'Other'.").
Run()
return loginType, err
}
29 changes: 0 additions & 29 deletions auth/auth_ui/survey.go

This file was deleted.

Loading

0 comments on commit 6338249

Please sign in to comment.