From a59b34198085468f522f46020f62f3482cc2a8ac Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Fri, 22 Oct 2021 09:55:00 +0300 Subject: [PATCH] implement init --- flow.go | 10 ++++------ flow_commands.go | 36 ++++++++++++++++++++++++++++++++---- flow_types.go | 3 +++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/flow.go b/flow.go index 8219ba5..7885832 100644 --- a/flow.go +++ b/flow.go @@ -107,9 +107,7 @@ func (f *KeycardFlow) runFlow() { func (f *KeycardFlow) pause(action string, errMsg string) { status := FlowParams{} - if errMsg != "" { - status[ErrorKey] = errMsg - } + status[ErrorKey] = errMsg if f.cardInfo.freeSlots != -1 { status[InstanceUID] = f.cardInfo.instanceUID @@ -128,7 +126,7 @@ func (f *KeycardFlow) pause(action string, errMsg string) { func (f *KeycardFlow) pauseAndWait(action string, errMsg string) error { if f.state == Cancelling { - return errors.New("cancel") + return giveupErr() } f.pause(action, errMsg) @@ -138,7 +136,7 @@ func (f *KeycardFlow) pauseAndWait(action string, errMsg string) error { f.state = Running return nil } else { - return errors.New("cancel") + return giveupErr() } } @@ -173,7 +171,7 @@ func (f *KeycardFlow) connect() *keycardContext { return nil } - f.pause(InsertCard, "") + f.pause(InsertCard, ErrorConnection) select { case <-f.wakeUp: if f.state != Cancelling { diff --git a/flow_commands.go b/flow_commands.go index 6b10798..c19b32f 100644 --- a/flow_commands.go +++ b/flow_commands.go @@ -56,7 +56,7 @@ func (f *KeycardFlow) pair(kc *keycardContext) error { delete(f.params, PairingPass) } - err := f.pauseAndWait(EnterPairing, "") + err := f.pauseAndWait(EnterPairing, ErrorPairing) if err != nil { return err @@ -66,8 +66,36 @@ func (f *KeycardFlow) pair(kc *keycardContext) error { } func (f *KeycardFlow) initCard(kc *keycardContext) error { - //NOTE: after init a restart of the flow is always needed - return errors.New("not implemented") + newPIN, pinOK := f.params[NewPIN] + + if !pinOK { + f.pauseAndWait(EnterNewPIN, ErrorRequireInit) + return f.initCard(kc) + } + + newPUK, pukOK := f.params[NewPUK] + if !pukOK { + f.pauseAndWait(EnterNewPUK, ErrorRequireInit) + return f.initCard(kc) + } + + newPairing, pairingOK := f.params[NewPairing] + if !pairingOK { + f.pauseAndWait(EnterNewPair, ErrorRequireInit) + return f.initCard(kc) + } + + err := kc.init(newPIN.(string), newPUK.(string), newPairing.(string)) + + if err == nil { + f.params[PIN] = newPIN + f.params[PairingPass] = newPairing + delete(f.params, NewPIN) + delete(f.params, NewPUK) + delete(f.params, NewPairing) + } + + return err } func (f *KeycardFlow) openSC(kc *keycardContext, giveup bool) error { @@ -145,7 +173,7 @@ func (f *KeycardFlow) unblockPIN(kc *keycardContext) error { if !pukOK { err = f.pauseAndWait(EnterPUK, pukError) } else if !pinOK { - err = f.pauseAndWait(EnterNewPIN, "") + err = f.pauseAndWait(EnterNewPIN, ErrorUnblocking) } if err != nil { diff --git a/flow_types.go b/flow_types.go index cc82e0d..a34cf5f 100644 --- a/flow_types.go +++ b/flow_types.go @@ -65,6 +65,9 @@ const ( ErrorUnknownFlow = "unknown-flow" ErrorNotAKeycard = "not-a-keycard" ErrorNoKeys = "no-keys" + ErrorRequireInit = "require-init" + ErrorPairing = "pairing" + ErrorUnblocking = "unblocking" ) const (