From b9de37eccceea26a01aac71cee8ca88d9807726c Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Tue, 27 Jul 2021 18:53:16 +0200 Subject: [PATCH] add secretes --- account.go | 20 ++++++++++++++++++++ util.go | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/account.go b/account.go index 00115df..ebdff3b 100644 --- a/account.go +++ b/account.go @@ -3,12 +3,25 @@ package main import ( "context" "io/ioutil" + "log" "os" + "strings" "github.com/mattn/go-mastodon" "github.com/pelletier/go-toml/v2" ) +func GetSecret(s string) string { + var err error + if strings.HasPrefix(s, "!CMD!") { + s, err = CmdToString(s) + if err != nil { + log.Fatalf("Couldn't run CMD on auth-file. Error; %v", err) + } + } + return s +} + func GetAccounts(filepath string) (*AccountData, error) { f, err := os.Open(filepath) if err != nil { @@ -21,6 +34,13 @@ func GetAccounts(filepath string) (*AccountData, error) { } accounts := &AccountData{} err = toml.Unmarshal(data, accounts) + + for i, acc := range accounts.Accounts { + accounts.Accounts[i].ClientID = GetSecret(acc.ClientID) + accounts.Accounts[i].ClientSecret = GetSecret(acc.ClientSecret) + accounts.Accounts[i].AccessToken = GetSecret(acc.AccessToken) + } + return accounts, err } diff --git a/util.go b/util.go index 9813612..4dacfef 100644 --- a/util.go +++ b/util.go @@ -29,6 +29,14 @@ type URL struct { Classes []string } +//Runs commands prefixed !CMD! +func CmdToString(cmd string) (string, error) { + cmd = strings.TrimPrefix(cmd, "!CMD!") + parts := strings.Split(cmd, " ") + s, err := exec.Command(parts[0], parts[1:]...).CombinedOutput() + return string(s), err +} + func getURLs(text string) []URL { urlReg := regexp.MustCompile(`(.+?)<\/a>`) attrReg := regexp.MustCompile(`(\w+?)="(.+?)"`)