Allow clients to change password

This commit is contained in:
2021-12-06 06:53:49 +01:00
parent 67b3214276
commit 6f91ac3d2d
8 changed files with 284 additions and 70 deletions

View File

@@ -338,3 +338,47 @@ func ActionClientLogin(c *cli.Context) error {
return nil
}
func ActionClientChangePassword(c *cli.Context) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
addr := cfg.Client.DefaultServer
if c.IsSet("addr") {
addr = c.String("addr")
}
clientCreds, err := cfg.Client.Creds()
if err != nil {
return err
}
conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds))
if err != nil {
return err
}
defer conn.Close()
fmt.Printf("current password: ")
oldPasswordBytes, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return cli.Exit(fmt.Sprintf("unable to read password: %s", err), 1)
}
fmt.Println()
oldPassword := string(oldPasswordBytes)
fmt.Printf("new password: ")
newPasswordBytes, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return cli.Exit(fmt.Sprintf("unable to read password: %s", err), 1)
}
fmt.Println()
newPassword := string(newPasswordBytes)
client := pb.NewUserServiceClient(conn)
if _, err := client.ChangePassword(c.Context, &pb.ChangePasswordRequest{OldPassword: oldPassword, NewPassword: newPassword}); err != nil {
return cli.Exit(fmt.Sprintf("unable to change password: %s", err), 1)
}
return nil
}

View File

@@ -130,7 +130,7 @@ func ActionServe(c *cli.Context) error {
grpcServer := grpc.NewServer(
grpc.Creds(creds),
grpc.ChainUnaryInterceptor(interceptors.NewAuthInterceptor(&store.MemoryStore{})),
grpc.ChainUnaryInterceptor(interceptors.NewAuthInterceptor(userStore)),
)
pb.RegisterFileServiceServer(grpcServer, grpcFileServer)
pb.RegisterUserServiceServer(grpcServer, grpcUserServer)
@@ -216,7 +216,7 @@ func initializeUsers(us store.UserStore) error {
if err := us.StoreUser(admin); err != nil {
return err
}
log.Printf("user created %s:%s", admin.Username, password)
log.Printf("user created with id %s:%s", admin.Username, password)
return nil
}