diff options
| author | Shulhan <ms@kilabit.info> | 2023-10-06 16:35:12 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-10-06 16:35:12 +0700 |
| commit | aedc3c206dd997c6dd96ce53a807b0b6ae635b3c (patch) | |
| tree | bc152d29ee0c864437cd8204e8bdfb0cd474b7a6 | |
| parent | 3ca05089eccb6c2e738c0a86725bd22695f5b7a7 (diff) | |
| download | gotp-aedc3c206dd997c6dd96ce53a807b0b6ae635b3c.tar.xz | |
all: implement command "get"
The "get" command get and print the issuer by its LABEL.
This will print the issuer secret, unencrypted.
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | _sys/etc/bash_completion.d/gotp | 8 | ||||
| -rw-r--r-- | cli.go | 19 | ||||
| -rw-r--r-- | cli_test.go | 45 | ||||
| -rw-r--r-- | cmd/gotp/main.go | 24 |
5 files changed, 100 insertions, 1 deletions
@@ -30,6 +30,11 @@ gen <LABEL> [N] Generate N number passwords using the secret identified by LABEL. +get <LABEL> + + Get and print the issuer by its LABEL. + This will print the issuer secret, unencrypted. + import <PROVIDER> <FILE> Import the TOTP configuration from other provider. diff --git a/_sys/etc/bash_completion.d/gotp b/_sys/etc/bash_completion.d/gotp index 807147c..6262172 100644 --- a/_sys/etc/bash_completion.d/gotp +++ b/_sys/etc/bash_completion.d/gotp @@ -15,7 +15,8 @@ suggest_key() { _gotp_completions() { - commands=("add" "gen" "import" "list" "remove" "remove-private-key" "rename" "set-private-key") + commands=("add" "gen" "get" "import" "list" "remove" + "remove-private-key" "rename" "set-private-key") local len=${#COMP_WORDS[@]} local cmd=${COMP_WORDS[1]} @@ -29,6 +30,11 @@ _gotp_completions() suggest_key "$key" fi ;; + get) + if [[ $len == 3 ]]; then + suggest_key "$key" + fi + ;; import) ;; list) @@ -198,6 +198,25 @@ func (cli *Cli) Generate(label string, n int) (listOtp []string, err error) { return listOtp, nil } +// Get the stored Issuer by its label. +func (cli *Cli) Get(label string) (issuer *Issuer, err error) { + var logp = `Get` + + if cli.cfg.privateKey == nil { + cli.cfg.privateKey, err = loadPrivateKey(cli.cfg.PrivateKey, nil) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + } + + issuer, err = cli.cfg.get(label) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + return issuer, nil +} + // Import the TOTP configuration from file format based on provider. func (cli *Cli) Import(providerName, file string) (n int, err error) { var ( diff --git a/cli_test.go b/cli_test.go index 8d0218c..bf15eec 100644 --- a/cli_test.go +++ b/cli_test.go @@ -7,6 +7,7 @@ import ( "bytes" "fmt" "os" + "path/filepath" "testing" "github.com/shuLhan/share/lib/test" @@ -233,3 +234,47 @@ func TestCli_SetPrivateKey(t *testing.T) { rawConfig = tdata.Input[`config.ini`] test.Assert(t, `RemovePrivateKey`, string(rawConfig), string(gotConfig)) } + +func TestCli_ViewEncrypted(t *testing.T) { + var ( + configDir = t.TempDir() + + cli *Cli + err error + ) + + cli, err = NewCli(configDir) + if err != nil { + t.Fatal(err) + } + + var privateKeyFile = filepath.Join(`testdata`, `keys`, `rsa-openssh.pem`) + + err = cli.SetPrivateKey(privateKeyFile) + if err != nil { + t.Fatal(err) + } + + var issA *Issuer + + issA, err = NewIssuer(`testA`, `SHA1:TESTA`, nil) + if err != nil { + t.Fatal(err) + } + + err = cli.Add(issA) + if err != nil { + t.Fatal(err) + } + + var gotIssA *Issuer + + gotIssA, err = cli.Get(`testA`) + if err != nil { + t.Fatal(err) + } + + // Reset the raw issuer value for comparison. + issA.raw = nil + test.Assert(t, `Get: testA`, issA, gotIssA) +} diff --git a/cmd/gotp/main.go b/cmd/gotp/main.go index 886fdbf..24e94e3 100644 --- a/cmd/gotp/main.go +++ b/cmd/gotp/main.go @@ -21,6 +21,7 @@ const ( cmdName = `gotp` cmdAdd = `add` cmdGenerate = `gen` + cmdGet = `get` cmdImport = `import` cmdList = `list` cmdRemove = `remove` @@ -68,6 +69,12 @@ func main() { log.Printf(`%s %s: missing parameters`, cmdName, cmd) os.Exit(1) } + case cmdGet: + args = args[1:] + if len(args) == 0 { + log.Fatalf(`%s %s: missing parameters`, cmdName, cmd) + } + case cmdImport: if len(args) <= 2 { log.Printf(`%s %s: missing parameters`, cmdName, cmd) @@ -123,6 +130,8 @@ func main() { doAdd(cli, args) case cmdGenerate: doGenerate(cli, args) + case cmdGet: + doGet(cli, args[0]) case cmdImport: doImport(cli, args) case cmdList: @@ -188,6 +197,21 @@ func doGenerate(cli *gotp.Cli, args []string) { } } +// doGet execute the "get" command to print the issuer by "label". +func doGet(cli *gotp.Cli, label string) { + var ( + issuer *gotp.Issuer + err error + ) + + issuer, err = cli.Get(label) + if err != nil { + log.Fatalf(`%s: %s`, cmdName, err) + } + + fmt.Println(issuer.String()) +} + func doImport(cli *gotp.Cli, args []string) { var ( providerName = args[1] |
