Skip to content

Commit

Permalink
Merge pull request #5 from aripalo/is-available
Browse files Browse the repository at this point in the history
feat: IsAvailable
  • Loading branch information
aripalo authored Apr 1, 2022
2 parents d7a48e9 + 3f861dc commit 343daf6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
18 changes: 15 additions & 3 deletions oath.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ func (oa *OathAccounts) GetSerial() string {
return oa.serial
}

// IsAvailable checks whether the Yubikey device is connected & available
func (oa *OathAccounts) IsAvailable() bool {
queryOptions := ykmanOptions{
serial: oa.serial,
password: "",
args: []string{"info"},
}

_, err := executeYkman(oa.ctx, queryOptions)
return err == ErrOathAccountPasswordProtected
}

// IsPasswordProtected checks whether the OATH application is password protected
func (oa *OathAccounts) IsPasswordProtected() bool {
queryOptions := ykmanOptions{
serial: oa.serial,
password: "",
args: []string{"list"},
args: []string{"oath", "accounts", "list"},
}

_, err := executeYkman(oa.ctx, queryOptions)
Expand Down Expand Up @@ -80,7 +92,7 @@ func (oa *OathAccounts) List() ([]string, error) {
queryOptions := ykmanOptions{
serial: oa.serial,
password: oa.password,
args: []string{"list"},
args: []string{"oath", "accounts", "list"},
}

oa.ensurePrompt()
Expand All @@ -99,7 +111,7 @@ func (oa *OathAccounts) Code(account string) (string, error) {
queryOptions := ykmanOptions{
serial: oa.serial,
password: oa.password,
args: []string{"code", "--single", account},
args: []string{"oath", "accounts", "code", "--single", account},
}

oa.ensurePrompt()
Expand Down
5 changes: 3 additions & 2 deletions ykman.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func defineYkmanArgs(options ykmanOptions) []string {
args = append(args, "--device", options.serial)
}

// setup oath application arguments
args = append(args, "oath", "accounts")
args = append(args, options.args...)

return args
Expand Down Expand Up @@ -103,6 +101,9 @@ func processYkmanErrors(err error, outputStderr string, password string) error {
if strings.Contains(outputStderr, "Failed connecting to the YubiKey") {
return ErrDeviceNotFound
}
if strings.Contains(outputStderr, "Failed to open device for communication") {
return ErrDeviceNotFound
}

// check for yubikey device removal
if strings.Contains(outputStderr, "Failed to transmit with protocol") {
Expand Down
24 changes: 18 additions & 6 deletions ykman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ func TestDefineYkmanArgs(t *testing.T) {
received []string
}{
{
name: "empty options",
input: ykmanOptions{},
name: "defaults",
input: ykmanOptions{args: []string{"oath", "accounts"}},
received: []string{"oath", "accounts"},
},
{
name: "info",
input: ykmanOptions{args: []string{"info"}},
received: []string{"info"},
},
{
name: "with serial",
input: ykmanOptions{serial: "12345678"},
input: ykmanOptions{serial: "12345678", args: []string{"oath", "accounts"}},
received: []string{"--device", "12345678", "oath", "accounts"},
},
{
name: "list accounts",
input: ykmanOptions{args: []string{"list"}},
input: ykmanOptions{args: []string{"oath", "accounts", "list"}},
received: []string{"oath", "accounts", "list"},
},
{
name: "code for account",
input: ykmanOptions{args: []string{"code", "--single", "Amazon Web Services:john.doe@example"}},
input: ykmanOptions{args: []string{"oath", "accounts", "code", "--single", "Amazon Web Services:john.doe@example"}},
received: []string{"oath", "accounts", "code", "--single", "Amazon Web Services:john.doe@example"},
},
{
name: "code for account with all options",
input: ykmanOptions{serial: "12345678", args: []string{"code", "--single", "Amazon Web Services:john.doe@example"}},
input: ykmanOptions{serial: "12345678", args: []string{"oath", "accounts", "code", "--single", "Amazon Web Services:john.doe@example"}},
received: []string{"--device", "12345678", "oath", "accounts", "code", "--single", "Amazon Web Services:john.doe@example"},
},
}
Expand Down Expand Up @@ -83,6 +88,13 @@ func TestProcessYkmanErrors(t *testing.T) {
password: "",
received: ErrDeviceNotFound,
},
{
name: "yubikey not found",
err: genericErr,
outputStderr: "Failed to open device for communication",
password: "",
received: ErrDeviceNotFound,
},
{
name: "yubikey removed while in-use",
err: genericErr,
Expand Down

0 comments on commit 343daf6

Please sign in to comment.