Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding otp flag set with get for speeding up things #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
adding otp flag set with get for speedup things
  • Loading branch information
Samuel Archambault committed Jul 5, 2022
commit b57ddf6ea72d3bc0e5cfe66ae8089e037188dfba
30 changes: 29 additions & 1 deletion internal/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ type ZAuthArgsEntryInput interface {
const usage = `COMMANDS:
entry zauth entry operations (add/edit/delete/list) (see zauth entry --help)
import import file(s) to zauth (see zauth import --help)
export export zauth entries to file (see zauth export --help)`
export export zauth entries to file (see zauth export --help)
otp zauth otp get code from entry name`

func ParseArgs(zc common.ZAuthCommonComp) error {
var msg string
Expand All @@ -89,6 +90,10 @@ func ParseArgs(zc common.ZAuthCommonComp) error {
entryDelete := entryCmd.Bool("delete", false, "Delete existing entry")
entryList := entryCmd.Bool("list", false, "List all entries")

// otp cmd
otpCmd := flag.NewFlagSet("otp", flag.ExitOnError)
issuerLabel := otpCmd.String("get", "", "get otp code from issuer label")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [COMMAND]\n\n%v\n", os.Args[0], usage)
}
Expand All @@ -99,6 +104,29 @@ func ParseArgs(zc common.ZAuthCommonComp) error {
return printZAuthOtpTable()
} else {
switch os.Args[1] {
case "otp":

otpCmd.Parse(os.Args[2:])
lst, err := common.ReadZAuthJson()
if err != nil {
msg = fmt.Sprintf("An error occured while listing entries to get entry %s: %v", *issuerLabel, err)
fmt.Fprintf(flag.CommandLine.Output(), "%s\n", msg)
return fmt.Errorf(msg)
}
for _, z := range lst {

if z.Issuer == *issuerLabel {
otp, err := otp.GenerateOTP(&z)
if err != nil {
return err
}
fmt.Println(otp.Otp)
return nil
}
}
msg = fmt.Sprintf("%s not found", *issuerLabel)
fmt.Fprintf(flag.CommandLine.Output(), "%s\n", msg)
return fmt.Errorf(msg)
case "import":
{
var pwd string
Expand Down
15 changes: 15 additions & 0 deletions internal/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ func TestParseEntryArgs(t *testing.T) {
t.Fatal("expected test to fail since functionality is not yet implemented")
}
}
func TestParseOtpArgs(t *testing.T) {
defer test.RemoveTestFiles()

// import sample file to create zauth json file. The file will then be exported
os.Args = []string{"zauth", "import", "-type=andotp", fmt.Sprintf("-file=%s", test.TestAndotpAccountsJson)}
ParseArgs(zc)
//otp get
zauth.ZAuthJson = test.TestZAuthJson
os.Args = []string{"zauth", "otp", "-get", "Some Org"}
err := ParseArgs(zc)
if err != nil {
t.Fatal(err)
}

}

func (*zauthCommonTest) ReadPassword() (string, error) {
if isEmptyPassword {
Expand Down