diff options
| author | Shulhan <ms@kilabit.info> | 2023-05-16 00:06:39 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-05-16 00:06:39 +0700 |
| commit | 6a9e476354d23da316a94d0d5a7f8b14a273910e (patch) | |
| tree | 2b3cd9df782fa41edde4b3282b7375825684be40 /cli.go | |
| parent | 6fd1ef57498aad5b73808f2b2260ccfe76ae649c (diff) | |
| download | gotp-6a9e476354d23da316a94d0d5a7f8b14a273910e.tar.xz | |
all: add command to set private key
The set-private-key command encrypt the issuer's value (hash:secret...)
in the file using private key.
The supported private key is RSA.
Diffstat (limited to 'cli.go')
| -rw-r--r-- | cli.go | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -306,6 +306,52 @@ func (cli *Cli) Rename(label, newLabel string) (err error) { return nil } +// SetPrivateKey encrypt all the OTP secret using the private key. +// The only accepted private key is RSA. +func (cli *Cli) SetPrivateKey(privateKeyFile string) (err error) { + var ( + logp = `SetPrivateKey` + oldIssuers = cli.cfg.Issuers + oldPrivateKey = cli.cfg.privateKey + ) + + cli.cfg.privateKey, err = loadPrivateKey(privateKeyFile, nil) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + var ( + issuer *Issuer + label string + raw string + ) + + cli.cfg.Issuers = map[string]string{} + + for label, raw = range oldIssuers { + // Decrypt the old 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 using new private key. + err = cli.cfg.add(issuer) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + } + + cli.cfg.PrivateKey = privateKeyFile + + err = cli.cfg.save() + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + return nil +} + func (cli *Cli) add(issuer *Issuer) (err error) { err = issuer.validate() if err != nil { |
