diff options
| author | Shulhan <ms@kilabit.info> | 2023-05-16 00:20:40 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-05-16 00:20:40 +0700 |
| commit | de4158b930a2afcfa59947ae2cd7127943751805 (patch) | |
| tree | 3cd3763ed102e1ebf729036ec9ba4da7ca1d0a26 /cli.go | |
| parent | 6a9e476354d23da316a94d0d5a7f8b14a273910e (diff) | |
| download | gotp-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.go | 44 |
1 files changed, 44 insertions, 0 deletions
@@ -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. |
