2023-12-12 18:42:50 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-01-29 21:08:01 +01:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2023-12-12 18:42:50 +01:00
|
|
|
"github.com/manifoldco/promptui"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
|
|
"github.com/photoprism/photoprism/pkg/list"
|
|
|
|
"github.com/photoprism/photoprism/pkg/report"
|
2024-01-13 16:27:05 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2023-12-12 18:42:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientsAddCommand configures the command name, flags, and action.
|
|
|
|
var ClientsAddCommand = cli.Command{
|
2024-01-14 09:51:49 +01:00
|
|
|
Name: "add",
|
|
|
|
Usage: "Registers a new client application",
|
2024-01-29 13:54:50 +01:00
|
|
|
Description: "If you specify a username as argument, the new client will belong to this user and inherit its privileges.",
|
2024-01-14 09:51:49 +01:00
|
|
|
ArgsUsage: "[username]",
|
|
|
|
Flags: ClientAddFlags,
|
|
|
|
Action: clientsAddAction,
|
2023-12-12 18:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// clientsAddAction registers a new client application.
|
|
|
|
func clientsAddAction(ctx *cli.Context) error {
|
|
|
|
return CallWithDependencies(ctx, func(conf *config.Config) error {
|
|
|
|
conf.MigrateDb(false, nil)
|
|
|
|
|
2024-01-29 13:54:50 +01:00
|
|
|
frm := form.AddClientFromCli(ctx)
|
2023-12-12 18:42:50 +01:00
|
|
|
|
|
|
|
interactive := true
|
|
|
|
|
|
|
|
if frm.ClientName != "" && frm.AuthScope != "" {
|
|
|
|
log.Debugf("client will be added in non-interactive mode")
|
|
|
|
interactive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && frm.ClientName == "" {
|
|
|
|
prompt := promptui.Prompt{
|
2024-01-13 16:27:05 +01:00
|
|
|
Label: "Client Name",
|
|
|
|
Default: rnd.Name(),
|
2023-12-12 18:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := prompt.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
frm.ClientName = clean.Name(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if interactive && frm.AuthScope == "" {
|
|
|
|
prompt := promptui.Prompt{
|
|
|
|
Label: "Authorization Scope",
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := prompt.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
frm.AuthScope = clean.Scope(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a default client name if no specific name has been provided.
|
|
|
|
if frm.AuthScope == "" {
|
|
|
|
frm.AuthScope = list.All
|
|
|
|
}
|
|
|
|
|
|
|
|
client, addErr := entity.AddClient(frm)
|
|
|
|
|
|
|
|
if addErr != nil {
|
2024-01-29 13:54:50 +01:00
|
|
|
return addErr
|
2023-12-12 18:42:50 +01:00
|
|
|
} else {
|
|
|
|
log.Infof("successfully registered new client %s", clean.LogQuote(client.ClientName))
|
|
|
|
|
|
|
|
// Display client details.
|
2024-01-29 17:32:53 +01:00
|
|
|
cols := []string{"Client ID", "Name", "Authentication Method", "User", "Role", "Scope", "Enabled", "Access Token Lifetime", "Created At"}
|
2023-12-12 18:42:50 +01:00
|
|
|
rows := make([][]string, 1)
|
|
|
|
|
|
|
|
var authExpires string
|
2024-01-29 13:54:50 +01:00
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
if client.AuthExpires > 0 {
|
|
|
|
authExpires = client.Expires().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.AuthTokens > 0 {
|
2024-01-29 13:54:50 +01:00
|
|
|
if authExpires != "" {
|
2024-01-29 21:08:01 +01:00
|
|
|
authExpires = fmt.Sprintf("%s; up to %s", authExpires, english.Plural(int(client.Tokens()), "token", "tokens"))
|
2024-01-29 13:54:50 +01:00
|
|
|
} else {
|
|
|
|
authExpires = fmt.Sprintf("up to %d tokens", client.AuthTokens)
|
|
|
|
}
|
2023-12-12 18:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rows[0] = []string{
|
|
|
|
client.UID(),
|
2024-01-18 16:53:05 +01:00
|
|
|
client.Name(),
|
2024-01-19 13:45:30 +01:00
|
|
|
client.AuthInfo(),
|
2024-01-19 18:10:01 +01:00
|
|
|
client.UserInfo(),
|
2024-01-18 16:53:05 +01:00
|
|
|
client.AclRole().String(),
|
|
|
|
client.Scope(),
|
2023-12-12 18:42:50 +01:00
|
|
|
report.Bool(client.AuthEnabled, report.Yes, report.No),
|
|
|
|
authExpires,
|
|
|
|
client.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err := report.RenderFormat(rows, cols, report.CliFormat(ctx)); err == nil {
|
|
|
|
fmt.Printf("\n%s", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 13:54:50 +01:00
|
|
|
// Se a random secret or the secret specified in the command flags, if any.
|
|
|
|
var secret, message string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if secret = frm.Secret(); secret == "" {
|
|
|
|
secret, err = client.NewSecret()
|
|
|
|
message = fmt.Sprintf(ClientSecretInfo, "FOLLOWING RANDOMLY GENERATED")
|
2023-12-12 18:42:50 +01:00
|
|
|
} else {
|
2024-01-29 13:54:50 +01:00
|
|
|
err = client.SetSecret(secret)
|
|
|
|
message = fmt.Sprintf(ClientSecretInfo, "SPECIFIED")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the secret has been saved successfully or return an error otherwise.
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set client secret: %s", err)
|
2023-12-12 18:42:50 +01:00
|
|
|
}
|
|
|
|
|
2024-01-29 13:54:50 +01:00
|
|
|
// Show client authentication credentials.
|
|
|
|
fmt.Printf(message)
|
|
|
|
result := report.Credentials("Client ID", client.ClientUID, "Client Secret", secret)
|
|
|
|
fmt.Printf("\n%s\n", result)
|
|
|
|
|
2023-12-12 18:42:50 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|