| 1 |
//go:build !noserver |
| 2 |
|
| 3 |
package cmd |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"github.com/urfave/cli/v2" |
| 9 |
"heckel.io/ntfy/v2/user" |
| 10 |
"heckel.io/ntfy/v2/util" |
| 11 |
"net/netip" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
func init() { |
| 16 |
commands = append(commands, cmdToken) |
| 17 |
} |
| 18 |
|
| 19 |
var flagsToken = append([]cli.Flag{}, flagsUser...) |
| 20 |
|
| 21 |
var cmdToken = &cli.Command{ |
| 22 |
Name: "token", |
| 23 |
Usage: "Create, list or delete user tokens", |
| 24 |
UsageText: "ntfy token [list|add|remove] ...", |
| 25 |
Flags: flagsToken, |
| 26 |
Before: initConfigFileInputSourceFunc("config", flagsToken, initLogFunc), |
| 27 |
Category: categoryServer, |
| 28 |
Subcommands: []*cli.Command{ |
| 29 |
{ |
| 30 |
Name: "add", |
| 31 |
Aliases: []string{"a"}, |
| 32 |
Usage: "Create a new token", |
| 33 |
UsageText: "ntfy token add [--expires=<duration>] [--label=..] USERNAME", |
| 34 |
Action: execTokenAdd, |
| 35 |
Flags: []cli.Flag{ |
| 36 |
&cli.StringFlag{Name: "expires", Aliases: []string{"e"}, Value: "", Usage: "token expires after"}, |
| 37 |
&cli.StringFlag{Name: "label", Aliases: []string{"l"}, Value: "", Usage: "token label"}, |
| 38 |
}, |
| 39 |
Description: `Create a new user access token. |
| 40 |
|
| 41 |
User access tokens can be used to publish, subscribe, or perform any other user-specific tasks. |
| 42 |
Tokens have full access, and can perform any task a user can do. They are meant to be used to |
| 43 |
avoid spreading the password to various places. |
| 44 |
|
| 45 |
This is a server-only command. It directly reads from user.db as defined in the server config |
| 46 |
file server.yml. The command only works if 'auth-file' is properly defined. |
| 47 |
|
| 48 |
Examples: |
| 49 |
ntfy token add phil # Create token for user phil which never expires |
| 50 |
ntfy token add --expires=2d phil # Create token for user phil which expires in 2 days |
| 51 |
ntfy token add -e "tuesday, 8pm" phil # Create token for user phil which expires next Tuesday |
| 52 |
ntfy token add -l backups phil # Create token for user phil with label "backups"`, |
| 53 |
}, |
| 54 |
{ |
| 55 |
Name: "remove", |
| 56 |
Aliases: []string{"del", "rm"}, |
| 57 |
Usage: "Removes a token", |
| 58 |
UsageText: "ntfy token remove USERNAME TOKEN", |
| 59 |
Action: execTokenDel, |
| 60 |
Description: `Remove a token from the ntfy user database. |
| 61 |
|
| 62 |
Example: |
| 63 |
ntfy token del phil tk_th2srHVlxrANQHAso5t0HuQ1J1TjN`, |
| 64 |
}, |
| 65 |
{ |
| 66 |
Name: "list", |
| 67 |
Aliases: []string{"l"}, |
| 68 |
Usage: "Shows a list of tokens", |
| 69 |
Action: execTokenList, |
| 70 |
Description: `Shows a list of all tokens. |
| 71 |
|
| 72 |
This is a server-only command. It directly reads from user.db as defined in the server config |
| 73 |
file server.yml. The command only works if 'auth-file' is properly defined.`, |
| 74 |
}, |
| 75 |
{ |
| 76 |
Name: "generate", |
| 77 |
Usage: "Generates a random token", |
| 78 |
Action: execTokenGenerate, |
| 79 |
Description: `Randomly generate a token to be used in provisioned tokens. |
| 80 |
|
| 81 |
This command only generates the token value, but does not persist it anywhere. |
| 82 |
The output can be used in the 'auth-tokens' config option.`, |
| 83 |
}, |
| 84 |
}, |
| 85 |
Description: `Manage access tokens for individual users. |
| 86 |
|
| 87 |
User access tokens can be used to publish, subscribe, or perform any other user-specific tasks. |
| 88 |
Tokens have full access, and can perform any task a user can do. They are meant to be used to |
| 89 |
avoid spreading the password to various places. |
| 90 |
|
| 91 |
This is a server-only command. It directly manages the user.db as defined in the server config |
| 92 |
file server.yml. The command only works if 'auth-file' is properly defined. |
| 93 |
|
| 94 |
Examples: |
| 95 |
ntfy token list # Shows list of tokens for all users |
| 96 |
ntfy token list phil # Shows list of tokens for user phil |
| 97 |
ntfy token add phil # Create token for user phil which never expires |
| 98 |
ntfy token add --expires=2d phil # Create token for user phil which expires in 2 days |
| 99 |
ntfy token remove phil tk_th2srHVlxr... # Delete token`, |
| 100 |
} |
| 101 |
|
| 102 |
func execTokenAdd(c *cli.Context) error { |
| 103 |
username := c.Args().Get(0) |
| 104 |
expiresStr := c.String("expires") |
| 105 |
label := c.String("label") |
| 106 |
if username == "" { |
| 107 |
return errors.New("username expected, type 'ntfy token add --help' for help") |
| 108 |
} else if username == userEveryone || username == user.Everyone { |
| 109 |
return errors.New("username not allowed") |
| 110 |
} |
| 111 |
expires := time.Unix(0, 0) |
| 112 |
if expiresStr != "" { |
| 113 |
var err error |
| 114 |
expires, err = util.ParseFutureTime(expiresStr, time.Now()) |
| 115 |
if err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
} |
| 119 |
manager, err := createUserManager(c) |
| 120 |
if err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
u, err := manager.User(username) |
| 124 |
if errors.Is(err, user.ErrUserNotFound) { |
| 125 |
return fmt.Errorf("user %s does not exist", username) |
| 126 |
} else if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
token, err := manager.CreateToken(u.ID, label, expires, netip.IPv4Unspecified(), false) |
| 130 |
if err != nil { |
| 131 |
return err |
| 132 |
} |
| 133 |
if expires.Unix() == 0 { |
| 134 |
fmt.Fprintf(c.App.Writer, "token %s created for user %s, never expires\n", token.Value, u.Name) |
| 135 |
} else { |
| 136 |
fmt.Fprintf(c.App.Writer, "token %s created for user %s, expires %v\n", token.Value, u.Name, expires.Format(time.UnixDate)) |
| 137 |
} |
| 138 |
return nil |
| 139 |
} |
| 140 |
|
| 141 |
func execTokenDel(c *cli.Context) error { |
| 142 |
username, token := c.Args().Get(0), c.Args().Get(1) |
| 143 |
if username == "" || token == "" { |
| 144 |
return errors.New("username and token expected, type 'ntfy token remove --help' for help") |
| 145 |
} else if username == userEveryone || username == user.Everyone { |
| 146 |
return errors.New("username not allowed") |
| 147 |
} |
| 148 |
manager, err := createUserManager(c) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
u, err := manager.User(username) |
| 153 |
if errors.Is(err, user.ErrUserNotFound) { |
| 154 |
return fmt.Errorf("user %s does not exist", username) |
| 155 |
} else if err != nil { |
| 156 |
return err |
| 157 |
} |
| 158 |
if err := manager.RemoveToken(u.ID, token); err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
fmt.Fprintf(c.App.Writer, "token %s for user %s removed\n", token, username) |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
func execTokenList(c *cli.Context) error { |
| 166 |
username := c.Args().Get(0) |
| 167 |
if username == userEveryone || username == user.Everyone { |
| 168 |
return errors.New("username not allowed") |
| 169 |
} |
| 170 |
manager, err := createUserManager(c) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
var users []*user.User |
| 175 |
if username != "" { |
| 176 |
u, err := manager.User(username) |
| 177 |
if errors.Is(err, user.ErrUserNotFound) { |
| 178 |
return fmt.Errorf("user %s does not exist", username) |
| 179 |
} else if err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
users = append(users, u) |
| 183 |
} else { |
| 184 |
users, err = manager.Users() |
| 185 |
if err != nil { |
| 186 |
return err |
| 187 |
} |
| 188 |
} |
| 189 |
usersWithTokens := 0 |
| 190 |
for _, u := range users { |
| 191 |
tokens, err := manager.Tokens(u.ID) |
| 192 |
if err != nil { |
| 193 |
return err |
| 194 |
} else if len(tokens) == 0 && username != "" { |
| 195 |
fmt.Fprintf(c.App.Writer, "user %s has no access tokens\n", username) |
| 196 |
return nil |
| 197 |
} else if len(tokens) == 0 { |
| 198 |
continue |
| 199 |
} |
| 200 |
usersWithTokens++ |
| 201 |
fmt.Fprintf(c.App.Writer, "user %s\n", u.Name) |
| 202 |
for _, t := range tokens { |
| 203 |
var label, expires, provisioned string |
| 204 |
if t.Label != "" { |
| 205 |
label = fmt.Sprintf(" (%s)", t.Label) |
| 206 |
} |
| 207 |
if t.Expires.Unix() == 0 { |
| 208 |
expires = "never expires" |
| 209 |
} else { |
| 210 |
expires = fmt.Sprintf("expires %s", t.Expires.Format(time.RFC822)) |
| 211 |
} |
| 212 |
if t.Provisioned { |
| 213 |
provisioned = " (server config)" |
| 214 |
} |
| 215 |
fmt.Fprintf(c.App.Writer, "- %s%s, %s, accessed from %s at %s%s\n", t.Value, label, expires, t.LastOrigin.String(), t.LastAccess.Format(time.RFC822), provisioned) |
| 216 |
} |
| 217 |
} |
| 218 |
if usersWithTokens == 0 { |
| 219 |
fmt.Fprintf(c.App.Writer, "no users with tokens\n") |
| 220 |
} |
| 221 |
return nil |
| 222 |
} |
| 223 |
|
| 224 |
func execTokenGenerate(c *cli.Context) error { |
| 225 |
fmt.Fprintln(c.App.Writer, user.GenerateToken()) |
| 226 |
return nil |
| 227 |
} |