From c9b6d171f38b07289dea2402c542c1d6a0a23ff8 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Sun, 11 Dec 2022 23:21:08 -0700 Subject: [PATCH] wip, moved errors out of pkg code, still not autorizing accounts. --- main.go | 20 +++++++- pkg/mastodon/account.go | 98 +++++++++++++++++++++++++++++++++++-- pkg/mastodon/application.go | 66 ++++++++++++++++--------- 3 files changed, 153 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index 8c18a42..ce3a486 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,22 @@ func main() { panic(err.Error()) } - token := app.Login() - app.VerifyCredentials(token) + token, err := app.Login() + if err != nil { + panic(err.Error()) + } + + err = app.VerifyCredentials(token) + if err != nil { + panic(err.Error()) + } + + var account mastodon.Account + code := account.Authorize(app) + token, err = account.RequestToken(app, code) + if err != nil { + panic(err.Error()) + } + + account.VerifyCredentials(token) } diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index a9240cd..96da9d9 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -2,6 +2,7 @@ package mastodon import ( "encoding/json" + "errors" "fmt" "io" "net/http" @@ -10,6 +11,48 @@ import ( ) type Account struct { + ID string `json:"id"` + UserName string `json:"username"` + Acct string `json:"acct"` + DisplayName string `json:"display_name"` + Locked bool `json:"locked"` + Bot bool `json:"bot"` + CreatedAt string `json:"created_at"` + Note string `json:"note"` + URL string `json:"url"` + Avatar string `json:"avatar"` + AvatarStatic string `json:"avatar_static"` + Header string `json:"header"` + HeaderStatic string `json:"header_static"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + StatusesCount int `json:"statuses_count"` + LastStatusAt string `json:"last_status_at"` + Source Source `json:"source"` + Emojis []Emoji `json:"emojis"` + Fields []Field `json:"fields"` +} + +type Source struct { + Privacy string `json:"privacy"` + Sensitive bool `json:"sensitive"` + Language string `json:"language"` + Note string `json:"note"` + Fields []Field `json:"fields"` + FollowRequestsCount int `json:"follow_requests_count"` +} + +type Field struct { + Name string `json:"name"` + Value string `json:"value"` + VerifiedAt string `json:"verified_at"` +} + +type Emoji struct { + ShortCode string `json:"shortcode"` + Url string `json:"url"` + StaticUrl string `json:"static_url"` + VisibleInPicker bool `json:"visible_in_picker"` } func (a *Account) Authorize(app Application) (code string) { @@ -31,12 +74,15 @@ func (a *Account) Authorize(app Application) (code string) { } fmt.Print("Enter returned code: ") - fmt.Scanln(&code) + _, err = fmt.Scanln(&code) + if err != nil { + panic(err.Error()) + } return } -func (a *Account) RequestToken(app Application, code string) (token Token) { +func (a *Account) RequestToken(app Application, code string) (token Token, err error) { v := url.Values{} v.Set("client_id", app.ClientID) v.Set("code", code) @@ -52,18 +98,60 @@ func (a *Account) RequestToken(app Application, code string) (token Token) { resp, err := http.PostForm(u.String(), v) if err != nil { - panic(err.Error()) + return + } + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + return } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - panic(err.Error()) + return } err = json.Unmarshal(body, &token) if err != nil { - panic(err.Error()) + return + } + + return +} + +func (a *Account) VerifyCredentials(token Token) (err error) { + client := &http.Client{} + + u := url.URL{ + Host: "hackers.town", + Path: "api/v1/accounts/verify_credentials", + } + u.Scheme = "https" + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return + } + req.Header.Add("Authorization", "Bearer "+token.AccessToken) + + resp, err := client.Do(req) + if err != nil { + return + } + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return + } + + err = json.Unmarshal(body, a) + if err != nil { + return } return diff --git a/pkg/mastodon/application.go b/pkg/mastodon/application.go index 1b4fe5d..a57c587 100644 --- a/pkg/mastodon/application.go +++ b/pkg/mastodon/application.go @@ -2,7 +2,7 @@ package mastodon import ( "encoding/json" - "fmt" + "errors" "io" "net/http" "net/url" @@ -67,27 +67,39 @@ func (a *Application) Load(name string) error { // Login authenticates the application to the Mastodon API, returning // a bearer token to be used with future requests. -func (a *Application) Login() (token Token) { - resp, err := http.PostForm("https://hackers.town/oauth/token", - url.Values{ - "client_id": {a.ClientID}, - "client_secret": {a.ClientSecret}, - "redirect_uri": {a.RedirectURI}, - "grant_type": {"client_credentials"}, - }) +func (a *Application) Login() (token Token, err error) { + v := url.Values{} + v.Set("client_id", a.ClientID) + v.Set("client_secret", a.ClientSecret) + v.Set("redirect_uri", a.RedirectURI) + v.Set("grant_type", "client_credentials") + + u := url.URL{ + Host: "hackers.town", + Path: "oauth/token", + RawQuery: v.Encode(), + } + u.Scheme = "https" + + resp, err := http.PostForm(u.String(), v) if err != nil { - panic(err.Error()) + return + } + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + return } + defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - panic(err.Error()) + return } err = json.Unmarshal(body, &token) if err != nil { - panic(err.Error()) + return } return @@ -95,34 +107,40 @@ func (a *Application) Login() (token Token) { // VerifyCredentials accepts a Token object and validates the contained // token against the Mastodon API. -func (a *Application) VerifyCredentials(token Token) { +func (a *Application) VerifyCredentials(token Token) (err error) { client := &http.Client{} - req, err := http.NewRequest("GET", - "https://hackers.town/api/v1/apps/verify_credentials", nil) + u := url.URL{ + Host: "hackers.town", + Path: "api/v1/apps/verify_credentials", + } + u.Scheme = "https" + + req, err := http.NewRequest("GET", u.String(), nil) if err != nil { - panic(err.Error()) + return } req.Header.Add("Authorization", "Bearer "+token.AccessToken) resp, err := client.Do(req) if err != nil { - panic(err.Error()) + return + } + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + return } defer resp.Body.Close() - fmt.Printf("%s", resp.Status) - body, err := io.ReadAll(resp.Body) if err != nil { - panic(err.Error()) + return } - var dummy Application - err = json.Unmarshal(body, &dummy) + err = json.Unmarshal(body, a) if err != nil { - panic(err.Error()) + return } - fmt.Printf("%+v", dummy) + return } -- 2.39.5