diff --git a/dfssc/cmd/root.go b/dfssc/cmd/root.go index ce4c0d083af54e7e321224ec9c2c0a437ee321c9..75fad7986e7d88ba565d96498c703beb3cdee678 100644 --- a/dfssc/cmd/root.go +++ b/dfssc/cmd/root.go @@ -58,6 +58,6 @@ func init() { _ = viper.BindPFlag("timeout", RootCmd.PersistentFlags().Lookup("timeout")) // Bind subcommands to root - RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd) + RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd, unregisterCmd) } diff --git a/dfssc/cmd/unregister.go b/dfssc/cmd/unregister.go new file mode 100644 index 0000000000000000000000000000000000000000..9a1f1d2587f882adcb458b7d1a4a87f972443d10 --- /dev/null +++ b/dfssc/cmd/unregister.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + "os" + + "dfss/dfssc/security" + "dfss/dfssc/user" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var unregisterCmd = &cobra.Command{ + Use: "unregister", + Short: "delete current client information on platform", + Run: func(cmd *cobra.Command, args []string) { + // Read info from provided certificate + cert, err := security.GetCertificate(viper.GetString("file_cert")) + if err != nil { + fmt.Fprintln(os.Stderr, "An error occurred:", err.Error()) + os.Exit(2) + } + + // Confirmation + var ready string + readStringParam("Do you REALLY want to delete "+cert.Subject.CommonName+"? Type 'yes' to confirm", "", &ready) + if ready != "yes" { + fmt.Println("Unregistering aborted!") + os.Exit(1) + } + + err = user.Unregister() + if err != nil { + fmt.Fprintln(os.Stderr, "Error: cannot unregister:", err.Error()) + os.Exit(2) + } + }, +} diff --git a/dfssc/user/unregister.go b/dfssc/user/unregister.go new file mode 100644 index 0000000000000000000000000000000000000000..76b59527e4e534388bb4d962bf9bad3ca361e51a --- /dev/null +++ b/dfssc/user/unregister.go @@ -0,0 +1,32 @@ +package user + +import ( + "errors" + + "golang.org/x/net/context" + "google.golang.org/grpc" + + pb "dfss/dfssp/api" + "dfss/net" +) + +// Unregister a user from the platform +func Unregister() error { + client, err := connect() + if err != nil { + return err + } + + // Stop the context if it takes too long for the platform to answer + ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout) + defer cancel() + response, err := client.Unregister(ctx, &pb.Empty{}) + if err != nil { + return errors.New(grpc.ErrorDesc(err)) + } + if response.Code != pb.ErrorCode_SUCCESS { + return errors.New(response.Message) + } + + return nil +}