From: Adam Shamblin Date: Sat, 17 Dec 2022 00:25:12 +0000 (-0700) Subject: Serializes auth token on login X-Git-Url: https://git.vexinglabs.com/?a=commitdiff_plain;h=refs%2Fheads%2Ffeature%2Fcobra-command;p=dead-tooter.git Serializes auth token on login --- diff --git a/cmd/login.go b/cmd/login.go index da35918..1842764 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -47,7 +47,12 @@ func login() { var account mastodon.Account code := account.Authorize(host, app) - token, err := account.RequestToken(host, app, code) + token, err = account.RequestToken(host, app, code) + if err != nil { + panic(err.Error()) + } + + err = token.Save() if err != nil { panic(err.Error()) } diff --git a/cmd/root.go b/cmd/root.go index 96aeedf..bb129bf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,18 +4,36 @@ import ( "fmt" "os" + "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon" "github.com/spf13/cobra" ) +var token mastodon.Token + var rootCmd = &cobra.Command{ Use: "dead-tooter", Short: "A CLI for Mastodon hate scripts", Long: `Provides a collection of capabilities that may or may not be present in the Mastodon web UI`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + var err error + token, err = mastodon.LoadToken() + if err != nil { + fmt.Println("No authentication token found.\nYou must login before performing futher commands.") + } + }, + Run: func(cmd *cobra.Command, args []string) { fmt.Println("d34d-t00t3r") }, + + PersistentPostRun: func(cmd *cobra.Command, args []string) { + err := token.Save() + if err != nil { + panic(err.Error()) + } + }, } func Execute() { diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index 690ef32..130fc9e 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -103,9 +103,8 @@ func (a *Account) RequestToken( v.Set("grant_type", "authorization_code") u := url.URL{ - Host: host, - Path: "oauth/token", - RawQuery: v.Encode(), + Host: host, + Path: "oauth/token", } u.Scheme = "https" diff --git a/pkg/mastodon/application.go b/pkg/mastodon/application.go index e7a5ece..6d4e2bb 100644 --- a/pkg/mastodon/application.go +++ b/pkg/mastodon/application.go @@ -9,20 +9,12 @@ import ( "os" ) -const configdir = "foodir/" +const ConfigDir = "foodir/" // RedirectUris when passed to the redirect_uris parameter, will // show return the authorization code instead of redirecting the client. const RedirectUris = "urn:ietf:wg:oauth:2.0:oob" -// Token struct contains the data returned by the Application login request. -type Token struct { - AccessToken string `json:"access_token"` - TokenType string `json:"token_type"` - Scope string `json:"scope"` - CreatedAt int `json:"created_at"` -} - // APIError contains the application specific error message returned // by the Mastodon API. type APIError struct { @@ -87,7 +79,7 @@ func Create(host string, client Client) (app Application, err error) { // Load will hydrate an Application instance based upon data stored in // a file. func Load(name string) (app Application, err error) { - data, err := os.ReadFile(configdir + name) + data, err := os.ReadFile(ConfigDir + name) if err != nil && os.IsNotExist(err) { return } @@ -102,7 +94,7 @@ func Load(name string) (app Application, err error) { // Save will store a serialized version of the Application struct to a file. func (a *Application) Save() (err error) { - err = os.Mkdir(configdir, 0750) + err = os.Mkdir(ConfigDir, 0750) if err != nil && !os.IsExist(err) { return } @@ -112,7 +104,7 @@ func (a *Application) Save() (err error) { return } - err = os.WriteFile(configdir+a.Name, data, 0666) + err = os.WriteFile(ConfigDir+a.Name, data, 0666) if err != nil { return } diff --git a/pkg/mastodon/oauth.go b/pkg/mastodon/oauth.go new file mode 100644 index 0000000..6f12a81 --- /dev/null +++ b/pkg/mastodon/oauth.go @@ -0,0 +1,45 @@ +package mastodon + +import ( + "encoding/json" + "os" +) + +// Token struct contains the data returned by the Application login request. +type Token struct { + AccessToken string `json:"access_token"` + TokenType string `json:"token_type"` + Scope string `json:"scope"` + CreatedAt int `json:"created_at"` +} + +// LoadToken deserializes the application authentication token from disc, if it +// exists. +func LoadToken() (token Token, err error) { + data, err := os.ReadFile(ConfigDir + "/token") + if err != nil && os.IsNotExist(err) { + return + } + + err = json.Unmarshal(data, &token) + if err != nil { + return + } + + return +} + +// Save serializes the application token to disk. +func (t *Token) Save() (err error) { + data, err := json.Marshal(t) + if err != nil { + return + } + + err = os.WriteFile(ConfigDir+"/token", data, 0666) + if err != nil { + return + } + + return +}