Reusing oauth client information

This commit is contained in:
farhan 2020-12-13 07:38:03 +00:00
parent 9e2af34c15
commit 69d940f64b
3 changed files with 57 additions and 7 deletions

View File

@ -4,8 +4,11 @@ import (
"net/http" "net/http"
"encoding/json" "encoding/json"
"bytes" "bytes"
"bufio"
"log" "log"
"io/ioutil" "io/ioutil"
"io"
"os"
) )
type OAuth struct { type OAuth struct {
@ -52,8 +55,58 @@ func register_client(endpoint string, http_client *http.Client) (string, string,
return "", "", err 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 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) { func oauth_login(endpoint string, username string, password string, client_id string, client_secret string) (OAuth, error) {
authMap, err := json.Marshal(map[string]string{ authMap, err := json.Marshal(map[string]string{
@ -90,7 +143,6 @@ func oauth_login(endpoint string, username string, password string, client_id st
log.Print("Unable to authenticate") log.Print("Unable to authenticate")
return OAuth{}, &authError{"Authentication error"} return OAuth{}, &authError{"Authentication error"}
} }
// log.Fatal(resp.StatusCode)
oauthData := OAuth{} oauthData := OAuth{}
err = json.Unmarshal(body, &oauthData) err = json.Unmarshal(body, &oauthData)

View File

@ -31,9 +31,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
for _, extaccount := range settings.Externalaccounts { for _, extaccount := range settings.Externalaccounts {
if extaccount.Endpoint == endpoint { if extaccount.Endpoint == endpoint {
use_auth = true use_auth = true
register_client(endpoint, &http_client) client_id, client_secret, err = get_client(endpoint, &http_client);
client_id, client_secret, err = register_client(endpoint, &http_client);
if err != nil { if err != nil {
log.Fatal("Unable to register client: ", err) log.Fatal("Unable to register client: ", err)
} }
@ -188,7 +186,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
} }
// Only done if we are crawling // 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:] newinstance := newpost.Account.Acct[at_sign+1:]
ri_mutex.Lock() ri_mutex.Lock()
_, exists := runninginstances[newinstance] _, exists := runninginstances[newinstance]

View File

@ -32,9 +32,9 @@ func StreamMastodon(endpoint string, reportPostChan chan ReportPost) {
for _, extaccount := range settings.Externalaccounts { for _, extaccount := range settings.Externalaccounts {
if extaccount.Endpoint == endpoint { if extaccount.Endpoint == endpoint {
// use_auth = true // 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 { if err != nil {
log.Fatal("Unable to register client: ", err) log.Fatal("Unable to register client: ", err)
} }