diff --git a/flow.go b/flow.go index 0686d9d..8d6e839 100644 --- a/flow.go +++ b/flow.go @@ -179,11 +179,11 @@ func (f *KeycardFlow) closeKeycard(kc *keycardContext) { } } -func (f *KeycardFlow) connect() *keycardContext { +func (f *KeycardFlow) connect() (*keycardContext, error) { kc, err := startKeycardContext() if err != nil { - return nil + return nil, restartErr() } f.pause(InsertCard, ErrorConnection, FlowParams{}) @@ -192,23 +192,24 @@ func (f *KeycardFlow) connect() *keycardContext { if f.state != Cancelling { panic("Resuming is not expected during connection") } - return nil + return nil, giveupErr() case <-kc.connected: if kc.runErr != nil { - return nil + return nil, restartErr() } + f.state = Running signal.Send(CardInserted, FlowStatus{}) - return kc + return kc, nil } } func (f *KeycardFlow) connectedFlow() (FlowStatus, error) { - kc := f.connect() + kc, err := f.connect() defer f.closeKeycard(kc) - if kc == nil { - return nil, f.pauseAndRestart(InsertCard, ErrorConnection) + if err != nil { + return nil, err } if factoryReset, ok := f.params[FactoryReset]; ok && factoryReset.(bool) { @@ -219,7 +220,7 @@ func (f *KeycardFlow) connectedFlow() (FlowStatus, error) { } } - err := f.selectKeycard(kc) + err = f.selectKeycard(kc) if err != nil { return nil, err diff --git a/flow_commands.go b/flow_commands.go index d073b40..6583701 100644 --- a/flow_commands.go +++ b/flow_commands.go @@ -11,7 +11,7 @@ import ( ) func (f *KeycardFlow) factoryReset(kc *keycardContext) error { - err := kc.factoryReset() + err := kc.factoryReset(true) if err == nil { delete(f.params, FactoryReset) diff --git a/keycard_context.go b/keycard_context.go index 47cce1e..1b9a085 100644 --- a/keycard_context.go +++ b/keycard_context.go @@ -57,6 +57,7 @@ func (kc *keycardContext) start() error { err = newKeycardError("cannot get readers") l(err.Error()) close(kc.connected) + _ = kc.cardCtx.Release() return err } @@ -67,6 +68,7 @@ func (kc *keycardContext) start() error { err = newKeycardError("no smartcard reader found") l(err.Error()) close(kc.connected) + _ = kc.cardCtx.Release() return err } @@ -95,6 +97,7 @@ func (kc *keycardContext) run() { l(err.Error()) kc.runErr = err close(kc.connected) + _ = kc.cardCtx.Release() return } @@ -106,8 +109,10 @@ func (kc *keycardContext) run() { card, err := kc.cardCtx.Connect(reader, scard.ShareShared, scard.ProtocolAny) if err != nil { // error connecting to card + l(err.Error()) kc.runErr = err close(kc.connected) + _ = kc.cardCtx.Release() return } @@ -115,6 +120,7 @@ func (kc *keycardContext) run() { if err != nil { kc.runErr = err close(kc.connected) + _ = kc.cardCtx.Release() return } @@ -465,7 +471,7 @@ func (kc *keycardContext) changePairingPassword(pairingPassword string) error { return nil } -func (kc *keycardContext) factoryReset() error { +func (kc *keycardContext) factoryReset(retry bool) error { <-kc.connected if kc.runErr != nil { return kc.runErr @@ -491,7 +497,12 @@ func (kc *keycardContext) factoryReset() error { if err := cmdSet.DeleteObject(aid); err != nil { l("error deleting keycard aid %+v", err) - return err + + if retry { + return kc.factoryReset(false) + } else { + return err + } } if err := cmdSet.InstallKeycardApplet(); err != nil {