Browse Source

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.
pull/36/head
Rob Loranger 4 years ago
parent
commit
630a867a34
No known key found for this signature in database GPG Key ID: D6F1633A4F0903B8
3 changed files with 24 additions and 8 deletions
  1. +17
    -1
      cmd/wf/commands.go
  2. +4
    -4
      cmd/wf/main.go
  3. +3
    -3
      commands/commands.go

+ 17
- 1
cmd/wf/commands.go View File

@@ -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 <username>", 1)


+ 4
- 4
cmd/wf/main.go View File

@@ -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",


+ 3
- 3
commands/commands.go View File

@@ -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 <username>", 1)
}


Loading…
Cancel
Save