From 69d940f64b35053d5158d8360d5595b14e8f0fa6 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sun, 13 Dec 2020 07:38:03 +0000 Subject: [PATCH] Reusing oauth client information --- engine/oauth.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++- engine/poll.go | 6 ++---- engine/stream.go | 4 ++-- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/engine/oauth.go b/engine/oauth.go index c3f3553..7948582 100644 --- a/engine/oauth.go +++ b/engine/oauth.go @@ -4,8 +4,11 @@ import ( "net/http" "encoding/json" "bytes" + "bufio" "log" "io/ioutil" + "io" + "os" ) type OAuth struct { @@ -52,9 +55,59 @@ func register_client(endpoint string, http_client *http.Client) (string, string, return "", "", err } + client_file := "clients/" + endpoint + + f, err := os.Create("clients/" + endpoint) + if err != nil { + log.Print("Unable to create " + client_file + ": ", err) + return bodymap["client_id"], bodymap["client_secret"], nil + } + defer f.Close() + + _, err = io.WriteString(f, bodymap["client_id"] + "\n") + if err != nil { + log.Print("Unable to write client_id line: ", err) + return bodymap["client_id"], bodymap["client_secret"], nil + } + _, err = io.WriteString(f, bodymap["client_secret"] + "\n") + if err != nil { + log.Print("Unable to write client_secret line: ", err) + return bodymap["client_id"], bodymap["client_secret"], nil + } + return bodymap["client_id"], bodymap["client_secret"], nil } +func get_client(endpoint string, http_client *http.Client) (string, string, error) { + client_file := "clients/" + endpoint + _, err := os.Stat(client_file) + if os.IsNotExist(err) == false { // The file exists + f, err := os.Open(client_file) + if err != nil { + log.Print("Unable to open " + client_file + ", creating new client") + return register_client(endpoint, http_client) + } + defer f.Close() + + rd := bufio.NewReader(f) + + client_id, _, err := rd.ReadLine() + if err != nil { + log.Print("Unable to read client_id line of " + client_file + ", building new client") + return register_client(endpoint, http_client) + } + client_secret, _, err := rd.ReadLine() + if err != nil { + log.Print("Unable to read client_secret line of " + client_file + ", building new client") + return register_client(endpoint, http_client) + } + + return string(client_id), string(client_secret), nil + } else { + return register_client(endpoint, http_client) + } +} + func oauth_login(endpoint string, username string, password string, client_id string, client_secret string) (OAuth, error) { authMap, err := json.Marshal(map[string]string{ "username": username, @@ -90,7 +143,6 @@ func oauth_login(endpoint string, username string, password string, client_id st log.Print("Unable to authenticate") return OAuth{}, &authError{"Authentication error"} } -// log.Fatal(resp.StatusCode) oauthData := OAuth{} err = json.Unmarshal(body, &oauthData) diff --git a/engine/poll.go b/engine/poll.go index a514924..69c4af9 100644 --- a/engine/poll.go +++ b/engine/poll.go @@ -31,9 +31,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { for _, extaccount := range settings.Externalaccounts { if extaccount.Endpoint == endpoint { use_auth = true - register_client(endpoint, &http_client) - - client_id, client_secret, err = register_client(endpoint, &http_client); + client_id, client_secret, err = get_client(endpoint, &http_client); if err != nil { log.Fatal("Unable to register client: ", err) } @@ -188,7 +186,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { } // Only done if we are crawling - if settings.Crawl == true || StringExists(endpoint, settings.Banned) == false { + if settings.Crawl == true && StringExists(endpoint, settings.Banned) == false { newinstance := newpost.Account.Acct[at_sign+1:] ri_mutex.Lock() _, exists := runninginstances[newinstance] diff --git a/engine/stream.go b/engine/stream.go index d47eaac..60e31ab 100644 --- a/engine/stream.go +++ b/engine/stream.go @@ -32,9 +32,9 @@ func StreamMastodon(endpoint string, reportPostChan chan ReportPost) { for _, extaccount := range settings.Externalaccounts { if extaccount.Endpoint == endpoint { // use_auth = true - register_client(endpoint, &http_client) + get_client(endpoint, &http_client) - client_id, client_secret, err = register_client(endpoint, &http_client); + client_id, client_secret, err = get_client(endpoint, &http_client); if err != nil { log.Fatal("Unable to register client: ", err) }