Files
kopia/cli/command_user_hash_password.go
Julio López 9c5fc842a1 feat(cli): add server user set-password-hash command (#3974)
Objectives:
- Facilitate the generation of valid password hashes that can be used with
  the `server user --user-password` CLI command.
- Encapsulate implementation details of password hashing in
  the `user` package.

Adds a new `server user hash-password` CLI command to generate the
hash from a supplied password.

Modifies the `server user set/add --user-password-hash` CLI command
to accept the password hash generated using the `hash-password`
command.

Adds `GetNewProfile(ctx, rep, username)` helper to move implementation
details to the `user` package.

Includes CLI and unit tests.

Cleans up and removes unused functions.
2024-07-11 19:29:06 -07:00

53 lines
1.4 KiB
Go

package cli
import (
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/user"
"github.com/kopia/kopia/repo"
)
type commandServerUserHashPassword struct {
password string
out textOutput
}
func (c *commandServerUserHashPassword) setup(svc appServices, parent commandParent) {
cmd := parent.Command("hash-password", "Hash a user password that can be passed to the 'server user add/set' command").Alias("hash")
cmd.Flag("user-password", "Password").StringVar(&c.password)
cmd.Action(svc.repositoryWriterAction(c.runServerUserHashPassword))
c.out.setup(svc)
}
// The current implementation does not require a connected repository, thus the
// RepositoryWriter parameter is not used. Future implementations will need a
// connected repository. To avoid a future incompatible change where the
// 'hash-password' command stops working without a connected repository,
// a connected repository is required now.
func (c *commandServerUserHashPassword) runServerUserHashPassword(ctx context.Context, _ repo.RepositoryWriter) error {
if c.password == "" {
// when password hash is empty, ask for password
pwd, err := askConfirmPass(c.out.stdout(), "Enter password to hash: ")
if err != nil {
return errors.Wrap(err, "error getting password")
}
c.password = pwd
}
h, err := user.HashPassword(c.password)
if err != nil {
return errors.Wrap(err, "hashing password")
}
c.out.printStdout("%s\n", h)
return nil
}