aboutsummaryrefslogtreecommitdiff
path: root/cli.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-05-16 00:20:40 +0700
committerShulhan <ms@kilabit.info>2023-05-16 00:20:40 +0700
commitde4158b930a2afcfa59947ae2cd7127943751805 (patch)
tree3cd3763ed102e1ebf729036ec9ba4da7ca1d0a26 /cli.go
parent6a9e476354d23da316a94d0d5a7f8b14a273910e (diff)
downloadgotp-de4158b930a2afcfa59947ae2cd7127943751805.tar.xz
all: add command to remove the private key
The remove-private-key decrypt the issuer's value back using previous private key and store it back to file as plain text.
Diffstat (limited to 'cli.go')
-rw-r--r--cli.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/cli.go b/cli.go
index 46a6d7e..d858faa 100644
--- a/cli.go
+++ b/cli.go
@@ -271,6 +271,50 @@ func (cli *Cli) Remove(label string) (err error) {
return nil
}
+// RemovePrivateKey Decrypt the issuer's value (hash:secret...) using previous
+// private key and store it back to file as plain text.
+func (cli *Cli) RemovePrivateKey() (err error) {
+ if cli.cfg.privateKey == nil {
+ return nil
+ }
+
+ var (
+ logp = `RemovePrivateKey`
+ oldPrivateKey = cli.cfg.privateKey
+ oldIssuers = cli.cfg.Issuers
+
+ issuer *Issuer
+ label string
+ raw string
+ )
+
+ cli.cfg.privateKey = nil
+ cli.cfg.Issuers = map[string]string{}
+
+ for label, raw = range oldIssuers {
+ // Decrypt the issuer using old private key.
+ issuer, err = NewIssuer(label, raw, oldPrivateKey)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ // Add it to the config back as plain text.
+ err = cli.cfg.add(issuer)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+ }
+
+ cli.cfg.PrivateKey = ``
+
+ err = cli.cfg.save()
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+
+ return nil
+}
+
// Rename a label to newLabel.
// It will return an error if the label parameter is not exist or newLabel
// already exist.