Reusing oauth client information
This commit is contained in:
parent
9e2af34c15
commit
69d940f64b
@ -4,8 +4,11 @@ import (
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"bytes"
|
||||
"bufio"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type OAuth struct {
|
||||
@ -52,8 +55,58 @@ 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{
|
||||
@ -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)
|
||||
|
@ -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]
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user