]> Vexing Labs - dead-tooter.git/commitdiff
Serializes auth token on login feature/cobra-command
authorAdam Shamblin <adam@vexingworkshop.com>
Sat, 17 Dec 2022 00:25:12 +0000 (17:25 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Sat, 17 Dec 2022 00:25:12 +0000 (17:25 -0700)
cmd/login.go
cmd/root.go
pkg/mastodon/account.go
pkg/mastodon/application.go
pkg/mastodon/oauth.go [new file with mode: 0644]

index da3591891b377aed4cf66994ff9819f9dc06ab29..18427645304d1123786dc465a3e45de55234ef01 100644 (file)
@@ -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())
        }
index 96aeedffe5d7852fdae062108827f07afb2a3b3c..bb129bfec0d6d8516cee300dafe793278127e15a 100644 (file)
@@ -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() {
index 690ef32ffe34c3fc761f348d6f225b79086dd8dc..130fc9ed96948dc7ee38f16ffcf7f219e95662f4 100644 (file)
@@ -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"
 
index e7a5eced20b56c967a6aa297c1b1c2d87af1c335..6d4e2bb098b5aa27161353d67cf30d3942674f87 100644 (file)
@@ -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 (file)
index 0000000..6f12a81
--- /dev/null
@@ -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
+}