Implement initial login functionality

This commit is contained in:
Tom Wiesing 2022-12-05 16:14:54 +01:00
parent a3bd0db78c
commit 3aa79b0d23
No known key found for this signature in database
36 changed files with 908 additions and 70 deletions

187
cmd/dis_user.go Normal file
View file

@ -0,0 +1,187 @@
package cmd
import (
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/cli"
"github.com/tkw1536/goprogram/exit"
)
// DisUser is the 'dis_user' command
var DisUser wisski_distillery.Command = disUser{}
type disUser struct {
CreateUser bool `short:"c" long:"create" description:"create a new user"`
DeleteUser bool `short:"d" long:"delete" description:"delete a user"`
InfoUser bool `short:"i" long:"info" description:"show information about a user"`
ListUsers bool `short:"l" long:"list" description:"list all users"`
SetPassword bool `short:"s" long:"set-password" description:"interactively set a user password"`
UnsetPassword bool `short:"u" long:"unset-password" description:"delete a users password and block the account"`
CheckPassword bool `short:"p" long:"check-password" description:"interactively check a user password"`
Positionals struct {
User string `positional-arg-name:"USER" description:"username to manage. May be omitted for some actions"`
} `positional-args:"true"`
}
func (disUser) Description() wisski_distillery.Description {
return wisski_distillery.Description{
Requirements: cli.Requirements{
NeedsDistillery: true,
},
Command: "dis_user",
Description: "manage distillery users",
}
}
var errUserRequired = exit.Error{
Message: "`USER` argument is required",
ExitCode: exit.ExitCommandArguments,
}
func (du disUser) AfterParse() error {
var counter int
for _, action := range []bool{
du.CreateUser,
du.InfoUser,
du.DeleteUser,
du.SetPassword,
du.UnsetPassword,
du.CheckPassword,
du.ListUsers,
} {
if action {
counter++
}
}
if counter != 1 {
return errNoActionSelected
}
if !du.ListUsers && du.Positionals.User == "" {
return errUserRequired
}
return nil
}
func (du disUser) Run(context wisski_distillery.Context) error {
switch {
case du.InfoUser:
return du.runInfo(context)
case du.CreateUser:
return du.runCreate(context)
case du.DeleteUser:
return du.runDelete(context)
case du.SetPassword:
return du.runSetPassword(context)
case du.UnsetPassword:
return du.runUnsetPassword(context)
case du.CheckPassword:
return du.runCheckPassword(context)
case du.ListUsers:
return du.runListUsers(context)
}
panic("never reached")
}
func (du disUser) runInfo(context wisski_distillery.Context) error {
user, err := context.Environment.Auth().User(context.Context, du.Positionals.User)
if err != nil {
return err
}
context.Println(user)
return nil
}
func (du disUser) runCreate(context wisski_distillery.Context) error {
user, err := context.Environment.Auth().CreateUser(context.Context, du.Positionals.User)
if err != nil {
return err
}
context.Println(user)
return nil
}
func (du disUser) runDelete(context wisski_distillery.Context) error {
user, err := context.Environment.Auth().User(context.Context, du.Positionals.User)
if err != nil {
return err
}
return user.Delete(context.Context)
}
func (du disUser) runSetPassword(context wisski_distillery.Context) error {
user, err := context.Environment.Auth().User(context.Context, du.Positionals.User)
if err != nil {
return err
}
var passwd string
{
context.Printf("Enter new password for user %s:", du.Positionals.User)
passwd1, err := context.IOStream.ReadPassword()
if err != nil {
return err
}
context.Println()
context.Printf("Enter the same password again:")
passwd, err = context.IOStream.ReadPassword()
if err != nil {
return err
}
context.Println()
if passwd != passwd1 {
return errPasswordsNotIdentical
}
if len(passwd) == 0 {
return errPasswordsNotIdentical
}
}
return user.SetPassword(context.Context, []byte(passwd))
}
func (du disUser) runUnsetPassword(context wisski_distillery.Context) error {
user, err := context.Environment.Auth().User(context.Context, du.Positionals.User)
if err != nil {
return err
}
return user.UnsetPassword(context.Context)
}
func (du disUser) runCheckPassword(context wisski_distillery.Context) error {
user, err := context.Environment.Auth().User(context.Context, du.Positionals.User)
if err != nil {
return err
}
context.Printf("Enter password for %s:", du.Positionals.User)
candidate, err := context.IOStream.ReadPassword()
if err != nil {
return err
}
context.Println()
return user.CheckPassword(context.Context, []byte(candidate))
}
func (du disUser) runListUsers(context wisski_distillery.Context) error {
users, err := context.Environment.Auth().Users(context.Context)
if err != nil {
return err
}
for _, user := range users {
context.Println(user)
}
return nil
}

View file

@ -13,9 +13,9 @@ import (
)
// DrupalUser is the 'drupal_user' setting
var DrupalUser wisski_distillery.Command = duser{}
var DrupalUser wisski_distillery.Command = drupalUser{}
type duser struct {
type drupalUser struct {
CheckCommonPasswords bool `short:"d" long:"check-common-passwords" description:"check for most common passwords. operates on all users concurrently."`
CheckPasswdInteractive bool `short:"c" long:"check-password" description:"interactively check user password"`
ResetPasswd bool `short:"r" long:"reset-password" description:"reset password for user"`
@ -26,7 +26,7 @@ type duser struct {
} `positional-args:"true"`
}
func (duser) Description() wisski_distillery.Description {
func (drupalUser) Description() wisski_distillery.Description {
return wisski_distillery.Description{
Requirements: cli.Requirements{
NeedsDistillery: true,
@ -46,7 +46,7 @@ var errUserParameter = exit.Error{
ExitCode: exit.ExitGeneric,
}
func (du duser) AfterParse() error {
func (du drupalUser) AfterParse() error {
var count int
for _, s := range []bool{
du.CheckCommonPasswords,
@ -74,7 +74,7 @@ var errPasswordsNotIdentical = exit.Error{
ExitCode: exit.ExitGeneric,
}
func (du duser) Run(context wisski_distillery.Context) error {
func (du drupalUser) Run(context wisski_distillery.Context) error {
instance, err := context.Environment.Instances().WissKI(context.Context, du.Positionals.Slug)
if err != nil {
return err
@ -93,7 +93,7 @@ func (du duser) Run(context wisski_distillery.Context) error {
panic("never reached")
}
func (du duser) login(context wisski_distillery.Context, instance *wisski.WissKI) error {
func (du drupalUser) login(context wisski_distillery.Context, instance *wisski.WissKI) error {
link, err := instance.Users().Login(context.Context, nil, du.Positionals.User)
if err != nil {
return err
@ -107,7 +107,7 @@ var errPasswordFound = exit.Error{
ExitCode: 5,
}
func (du duser) checkCommonPassword(context wisski_distillery.Context, instance *wisski.WissKI) error {
func (du drupalUser) checkCommonPassword(context wisski_distillery.Context, instance *wisski.WissKI) error {
users := instance.Users()
entities, err := users.All(context.Context, nil)
@ -132,7 +132,7 @@ func (du duser) checkCommonPassword(context wisski_distillery.Context, instance
}, entities)
}
func (du duser) checkPasswordInteractive(context wisski_distillery.Context, instance *wisski.WissKI) error {
func (du drupalUser) checkPasswordInteractive(context wisski_distillery.Context, instance *wisski.WissKI) error {
validator, err := instance.Users().GetPasswordValidator(context.Context, du.Positionals.User)
if err != nil {
return err
@ -161,7 +161,7 @@ func (du duser) checkPasswordInteractive(context wisski_distillery.Context, inst
return nil
}
func (du duser) resetPassword(context wisski_distillery.Context, instance *wisski.WissKI) error {
func (du drupalUser) resetPassword(context wisski_distillery.Context, instance *wisski.WissKI) error {
context.Printf("Enter new password for user %s:", du.Positionals.User)
passwd1, err := context.IOStream.ReadPassword()
if err != nil {

View file

@ -50,6 +50,9 @@ func init() {
wdcli.Register(cmd.DrupalSetting)
wdcli.Register(cmd.DrupalUser)
// distillery auth
wdcli.Register(cmd.DisUser)
// backup & cron
wdcli.Register(cmd.Snapshot)
wdcli.Register(cmd.Backup)