From 630a867a34bf6f1d84f71f8b71b2f4dfabd15164 Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Fri, 2 Aug 2019 10:46:55 -0700 Subject: [PATCH] update requireAuth helper, check logged in users this uses the usersLoggedIn helper to check for already logged in users, selecting the single user when only one present and returning and error when multiple are logged in. --- cmd/wf/commands.go | 18 +++++++++++++++++- cmd/wf/main.go | 8 ++++---- commands/commands.go | 6 +++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/wf/commands.go b/cmd/wf/commands.go index c252e6a..a725429 100644 --- a/cmd/wf/commands.go +++ b/cmd/wf/commands.go @@ -15,9 +15,25 @@ import ( func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc { return func(c *cli.Context) error { + // check for logged in users when host is provided without user + if c.GlobalIsSet("host") && !c.GlobalIsSet("user") { + // multiple users should display a list + if num, users, err := usersLoggedIn(c); num > 1 && err == nil { + return cli.NewExitError(fmt.Sprintf("Multiple logged in users, please use '-u' or '-user' to specify one of:\n%s", users), 1) + } else if num == 1 && err == nil { + // single user found for host should be set as user flag so LoadUser can + // succeed, and notify the client + if err := c.GlobalSet("user", users[0]); err != nil { + return cli.NewExitError(fmt.Sprintf("Failed to set user flag for only logged in user at host %s: %v", users[0], err), 1) + } + fmt.Printf("Host specified without user flag, using logged in user: %s\n", users[0]) + } else if err != nil { + return cli.NewExitError(fmt.Sprintf("Failed to check for logged in users: %v", err), 1) + } + } u, err := config.LoadUser(c) if err != nil { - return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1) + return cli.NewExitError(fmt.Sprintf("Couldn't load user: %v", err), 1) } if u == nil { return cli.NewExitError("You must be authenticated to "+action+".\nLog in first with: "+executable.Name()+" auth ", 1) diff --git a/cmd/wf/main.go b/cmd/wf/main.go index 46de71d..7cce4c9 100644 --- a/cmd/wf/main.go +++ b/cmd/wf/main.go @@ -148,7 +148,7 @@ func main() { Name: "posts", Usage: "List all of your posts", Description: "This will list only local posts.", - Action: commands.CmdListPosts, + Action: requireAuth(commands.CmdListPosts, "posts"), Flags: []cli.Flag{ cli.BoolFlag{ Name: "id", @@ -170,7 +170,7 @@ func main() { }, { Name: "blogs", Usage: "List blogs", - Action: commands.CmdCollections, + Action: requireAuth(commands.CmdCollections, "blogs"), Flags: []cli.Flag{ cli.BoolFlag{ Name: "tor, t", @@ -189,7 +189,7 @@ func main() { }, { Name: "claim", Usage: "Claim local unsynced posts", - Action: commands.CmdClaim, + Action: requireAuth(commands.CmdClaim, "claim"), Description: "This will claim any unsynced posts local to this machine. To see which posts these are run: wf posts.", Flags: []cli.Flag{ cli.BoolFlag{ @@ -229,7 +229,7 @@ func main() { { Name: "logout", Usage: "Log out of a WriteFreely instance", - Action: cmdLogOut, + Action: requireAuth(cmdLogOut, "logout"), Flags: []cli.Flag{ cli.BoolFlag{ Name: "tor, t", diff --git a/commands/commands.go b/commands/commands.go index 41b0662..19119b1 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -357,18 +357,18 @@ func CmdClaim(c *cli.Context) error { } func CmdAuth(c *cli.Context) error { + username := c.Args().Get(0) // Check configuration u, err := config.LoadUser(c) if err != nil { return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1) } - if u != nil && u.AccessToken != "" { - return cli.NewExitError("You're already authenticated as "+u.User.Username+". Log out with: "+executable.Name()+" logout", 1) + if u != nil && u.AccessToken != "" && username == u.User.Username { + return cli.NewExitError("You're already authenticated as "+u.User.Username, 1) } // Validate arguments and get password // TODO: after global config, check for default user - username := c.Args().Get(0) if username == "" { return cli.NewExitError("usage: "+executable.Name()+" auth ", 1) }