Skip to content

Commit

Permalink
Add Account Deletion and Change Password to CLI Commands (#983)
Browse files Browse the repository at this point in the history
Added functionality allowing users to delete accounts and change passwords through the CLI to support recent development of admin ChangePassword and DeleteAccount APIs on the server side.
  • Loading branch information
sigmaith authored Sep 3, 2024
1 parent 2f93922 commit 7a23cb1
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 17 deletions.
27 changes: 27 additions & 0 deletions admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,30 @@ func withShardKey[T any](conn *connect.Request[T], keys ...string) *connect.Requ

return conn
}

// DeleteAccount deletes the user's account.
func (c *Client) DeleteAccount(ctx context.Context, username, password string) error {
_, err := c.client.DeleteAccount(ctx, connect.NewRequest(&api.DeleteAccountRequest{
Username: username,
Password: password,
}))
if err != nil {
return err
}

return nil
}

// ChangePassword changes the user's password.
func (c *Client) ChangePassword(ctx context.Context, username, password, newPassword string) error {
_, err := c.client.ChangePassword(ctx, connect.NewRequest(&api.ChangePasswordRequest{
Username: username,
CurrentPassword: password,
NewPassword: newPassword,
}))
if err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions cmd/yorkie/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/yorkie-team/yorkie/cmd/yorkie/context"
"github.com/yorkie-team/yorkie/cmd/yorkie/document"
"github.com/yorkie-team/yorkie/cmd/yorkie/project"
"github.com/yorkie-team/yorkie/cmd/yorkie/user"
)

var rootCmd = &cobra.Command{
Expand All @@ -49,6 +50,7 @@ func init() {
rootCmd.AddCommand(project.SubCmd)
rootCmd.AddCommand(document.SubCmd)
rootCmd.AddCommand(context.SubCmd)
rootCmd.AddCommand(user.SubCmd)
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath(path.Join(os.Getenv("HOME"), ".yorkie"))
Expand Down
130 changes: 130 additions & 0 deletions cmd/yorkie/user/change_password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package user

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"

"github.com/yorkie-team/yorkie/admin"
"github.com/yorkie-team/yorkie/cmd/yorkie/config"
)

var (
newPassword string
)

func changePasswordCmd() *cobra.Command {
return &cobra.Command{
Use: "change-password",
Short: "Change user password",
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
password, newPassword, err := getPasswords()
if err != nil {
return err
}

if rpcAddr == "" {
rpcAddr = viper.GetString("rpcAddr")
}

cli, err := admin.Dial(rpcAddr, admin.WithInsecure(insecure))
if err != nil {
return fmt.Errorf("failed to dial admin: %w", err)
}
defer func() {
cli.Close()
}()

ctx := context.Background()
if err := cli.ChangePassword(ctx, username, password, newPassword); err != nil {
return err
}

if err := deleteAuthSession(rpcAddr); err != nil {
return err
}

return nil
},
}
}

func getPasswords() (string, string, error) {
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", "", fmt.Errorf("failed to read password: %w", err)
}
password := string(bytePassword)
fmt.Println()

fmt.Print("Enter New Password: ")
bytePassword, err = term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", "", fmt.Errorf("failed to read password: %w", err)
}
newPassword := string(bytePassword)
fmt.Println()

return password, newPassword, nil
}

func deleteAuthSession(rpcAddr string) error {
conf, err := config.Load()
if err != nil {
return err
}

delete(conf.Auths, rpcAddr)
if err := config.Save(conf); err != nil {
return err
}

return nil
}

func init() {
cmd := changePasswordCmd()
cmd.Flags().StringVarP(
&username,
"username",
"u",
"",
"Username (required)",
)
cmd.Flags().StringVar(
&rpcAddr,
"rpc-addr",
"",
"Address of the RPC server",
)
cmd.Flags().BoolVar(
&insecure,
"insecure",
false,
"Skip the TLS connection of the client",
)
_ = cmd.MarkFlagRequired("username")
SubCmd.AddCommand(cmd)
}
157 changes: 157 additions & 0 deletions cmd/yorkie/user/delete_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package user

import (
"context"
"fmt"
"os"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"

"github.com/yorkie-team/yorkie/admin"
"github.com/yorkie-team/yorkie/cmd/yorkie/config"
)

func deleteAccountCmd() *cobra.Command {
return &cobra.Command{
Use: "delete-account",
Short: "Delete user account",
PreRunE: config.Preload,
RunE: func(_ *cobra.Command, args []string) error {
password, err := getPassword()
if err != nil {
return err
}

if confirmation, err := makeConfirmation(); !confirmation || err != nil {
if err != nil {
return err
}
return nil
}

conf, err := config.Load()
if err != nil {
return err
}

if rpcAddr == "" {
rpcAddr = viper.GetString("rpcAddr")
}

if err := deleteAccountFromServer(conf, rpcAddr, insecure, username, password); err != nil {
fmt.Println("Failed to delete your account." +
"The account may not exist or the password might be incorrect. Please try again.")
} else {
fmt.Println("Your account has been successfully deleted.")
}

return nil
},
}
}

func getPassword() (string, error) {
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", fmt.Errorf("failed to read password: %w", err)
}
password = string(bytePassword)
fmt.Println()

return password, nil
}

func makeConfirmation() (bool, error) {
fmt.Println(
"WARNING: This action is irreversible. Your account and all associated data will be permanently deleted.",
)

fmt.Print("Are you absolutely sure? Type 'DELETE' to confirm: ")
var confirmation string
if _, err := fmt.Scanln(&confirmation); err != nil {
return false, fmt.Errorf("failed to read confirmation from user: %w", err)
}

if confirmation != "DELETE" {
return false, fmt.Errorf("account deletion aborted")
}

return true, nil
}

func deleteAccountFromServer(conf *config.Config, rpcAddr string, insecureFlag bool, username, password string) error {
cli, err := admin.Dial(rpcAddr,
admin.WithInsecure(insecureFlag),
admin.WithToken(conf.Auths[rpcAddr].Token),
)
if err != nil {
return fmt.Errorf("failed to dial admin: %w", err)
}
defer cli.Close()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if err := cli.DeleteAccount(ctx, username, password); err != nil {
return fmt.Errorf("server failed to delete account: %w", err)
}

delete(conf.Auths, rpcAddr)
if conf.RPCAddr == rpcAddr {
for addr := range conf.Auths {
conf.RPCAddr = addr
break
}
}

if err := config.Save(conf); err != nil {
return err
}

return nil
}

func init() {
cmd := deleteAccountCmd()
cmd.Flags().StringVarP(
&username,
"username",
"u",
"",
"Username (required)",
)
cmd.Flags().StringVar(
&rpcAddr,
"rpc-addr",
"",
"Address of the RPC server",
)
cmd.Flags().BoolVar(
&insecure,
"insecure",
false,
"Skip the TLS connection of the client",
)
_ = cmd.MarkFlagRequired("username")
SubCmd.AddCommand(cmd)
}
27 changes: 16 additions & 11 deletions cmd/yorkie/login.go → cmd/yorkie/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
* limitations under the License.
*/

package main
// Package user provides the user command.
package user

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"golang.org/x/term"

"github.com/yorkie-team/yorkie/admin"
"github.com/yorkie-team/yorkie/cmd/yorkie/config"
Expand All @@ -38,6 +42,14 @@ func newLoginCmd() *cobra.Command {
Short: "Log in to Yorkie server",
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return fmt.Errorf("failed to read password: %w", err)
}
password = string(bytePassword)
fmt.Println()

cli, err := admin.Dial(rpcAddr, admin.WithInsecure(insecure))
if err != nil {
return err
Expand Down Expand Up @@ -81,14 +93,7 @@ func init() {
"username",
"u",
"",
"Username (required if password is set)",
)
cmd.Flags().StringVarP(
&password,
"password",
"p",
"",
"Password (required if username is set)",
"Username (required)",
)
cmd.Flags().StringVar(
&rpcAddr,
Expand All @@ -102,6 +107,6 @@ func init() {
false,
"Skip the TLS connection of the client",
)
cmd.MarkFlagsRequiredTogether("username", "password")
rootCmd.AddCommand(cmd)
_ = cmd.MarkFlagRequired("username")
SubCmd.AddCommand(cmd)
}
Loading

0 comments on commit 7a23cb1

Please sign in to comment.