From 3f50dfcab27e299334e814e9f2989e63c85cc8e2 Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Thu, 14 Nov 2024 09:31:52 +1000 Subject: [PATCH] dropped into exclusions --- .../workspace/workspaceui/filesecrets.go | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 cmd/slackdump/internal/workspace/workspaceui/filesecrets.go diff --git a/cmd/slackdump/internal/workspace/workspaceui/filesecrets.go b/cmd/slackdump/internal/workspace/workspaceui/filesecrets.go new file mode 100644 index 00000000..401e97f2 --- /dev/null +++ b/cmd/slackdump/internal/workspace/workspaceui/filesecrets.go @@ -0,0 +1,84 @@ +package workspaceui + +import ( + "context" + "errors" + "os" + "strings" + + "github.com/charmbracelet/huh" + "github.com/joho/godotenv" + + "github.com/rusq/slackdump/v3/auth" + "github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui" + "github.com/rusq/slackdump/v3/internal/structures" +) + +func fileWithSecrets(ctx context.Context, mgr manager) error { + var filename string + + form := huh.NewForm(huh.NewGroup( + huh.NewFilePicker(). + Title("Choose a file with secrets"). + Description("The one with SLACK_TOKEN and SLACK_COOKIE environment variables"). + ShowHidden(true). + ShowSize(true). + ShowPermissions(true). + Value(&filename). + Validate(validateSecrets), + )).WithTheme(ui.HuhTheme()).WithHeight(10) + if err := form.RunWithContext(ctx); err != nil { + if errors.Is(err, huh.ErrUserAborted) { + return nil + } + } + tok, cookie, err := parseSecretsTxt(filename) + if err != nil { + return err + } + prov, err := auth.NewValueAuth(tok, cookie) + if err != nil { + return err + } + wsp, err := createAndSelect(ctx, mgr, prov) + if err != nil { + return err + } + + return success(ctx, wsp) +} + +func validateSecrets(filename string) error { + _, _, err := parseSecretsTxt(filename) + return err +} + +func parseSecretsTxt(filename string) (string, string, error) { + f, err := os.Open(filename) + if err != nil { + return "", "", err + } + defer f.Close() + secrets, err := godotenv.Parse(f) + if err != nil { + return "", "", errors.New("not a secrets file") + } + token, ok := secrets["SLACK_TOKEN"] + if !ok { + return "", "", errors.New("no SLACK_TOKEN found") + } + if err := structures.ValidateToken(token); err != nil { + return "", "", err + } + if !strings.HasPrefix(token, "xoxc-") { + return token, "", nil + } + cook, ok := secrets["SLACK_COOKIE"] + if !ok { + return "", "", errors.New("no SLACK_COOKIE found") + } + if !strings.HasPrefix(cook, "xoxd-") { + return "", "", errors.New("invalid cookie") + } + return token, cook, nil +}