make pairing work

This commit is contained in:
Michele Balistreri 2021-10-04 17:57:01 +03:00 committed by Iuri Matias
parent 101e77cc14
commit 3016fd4ec1
3 changed files with 67 additions and 68 deletions

View File

@ -24,60 +24,7 @@ proc delete*(self: KeycardController) =
proc reset*(self: KeycardController) =
discard
proc attemptOpenSecureChannel(self: KeycardController) : bool =
let pairing = self.view.pairings.getPairing(self.view.appInfo.instanceUID)
if pairing == "":
return false
discard """let err = self.status.keycard.openSecureChannel(pairing)
if err == Ok:
return true
self.view.pairings.removePairing(self.view.appInfo.instanceUID)
""""
return false
proc getCardState(self: KeycardController) =
var appInfo: KeycardApplicationInfo
try:
appInfo = self.status.keycard.select()
except KeycardSelectException as ex:
self.view.cardUnhandledError(ex.error)
return
self.view.appInfo = appInfo
if not appInfo.installed:
self.view.cardState = NotKeycard
self.view.cardNotKeycard()
elif not appInfo.initialized:
self.view.cardState = PreInit
self.view.cardPreInit()
elif self.attemptOpenSecureChannel():
discard """
self.view.appStatus = self.status.keycard.getStatusApplication()
if self.view.appStatus.pukRetryCounter == 0:
self.view.cardState = Blocked
self.view.cardBlocked()
elif self.view.appStatus.pinRetryCounter == 0:
self.view.cardState = Frozen
self.view.cardFrozen()
else:
"""
self.view.cardState = Paired
self.view.cardPaired()
elif appInfo.availableSlots > 0:
self.view.cardState = Unpaired
self.view.cardUnpaired()
else:
self.view.cardState = NoFreeSlots
self.view.cardNoFreeSlots()
proc init*(self: KeycardController) =
self.status.events.on(SignalType.KeycardConnected.event) do(e:Args):
self.getCardState()
self.view.getCardState()
self.view.cardConnected()

View File

@ -1,4 +1,5 @@
import json, os, std/wrapnils
import json, os
import types/keycard
import ../../constants
@ -17,7 +18,7 @@ proc newPairingController*(): KeycardPairingController =
proc save(self: KeycardPairingController) =
writeFile(PAIRINGSTORE, $self.store)
proc addPairing*(self: KeycardPairingController, instanceUID: string, pairing: string) =
proc addPairing*(self: KeycardPairingController, instanceUID: string, pairing: KeycardPairingInfo) =
self.store[instanceUID] = %* pairing
self.save()
@ -25,6 +26,7 @@ proc removePairing*(self: KeycardPairingController, instanceUID: string) =
self.store.delete(instanceUID)
self.save()
proc getPairing*(self: KeycardPairingController, instanceUID: string): string =
proc getPairing*(self: KeycardPairingController, instanceUID: string): KeycardPairingInfo =
let node = self.store{instanceUID}
return ?.node.getStr()
if node != nil:
result = to(node, KeycardPairingInfo)

View File

@ -55,7 +55,6 @@ QtObject:
except KeycardStartException as ex:
self.cardUnhandledError(ex.error)
proc stopConnection*(self: KeycardView) {.slot.} =
self.cardState = Disconnected
try:
@ -63,17 +62,42 @@ QtObject:
except KeycardStopException as ex:
self.cardUnhandledError(ex.error)
proc pair*(self: KeycardView, password: string) {.slot.} =
proc attemptOpenSecureChannel(self: KeycardView): bool =
let pairing = self.pairings.getPairing(self.appInfo.instanceUID)
if pairing == nil:
return false
try:
self.status.keycard.openSecureChannel(int(pairing.index), pairing.key)
except KeycardOpenSecureChannelException:
self.pairings.removePairing(self.appInfo.instanceUID)
return false
return true
proc onSecureChannelOpened(self: KeycardView) =
discard """
let pairing = self.status.keycard.pair(password)
if pairing == "":
error
self.pairings.addPairing(self.appInfo.instanceUID, pairing)
self.appStatus = self.status.keycard.getStatusApplication()
if self.appStatus.pukRetryCounter == 0:
self.cardState = Blocked
self.cardBlocked()
elif self.appStatus.pinRetryCounter == 0:
self.cardState = Frozen
self.cardFrozen()
else:
"""
self.cardState = Paired
self.cardPaired()
"""
proc pair*(self: KeycardView, password: string) {.slot.} =
try:
let pairing = self.status.keycard.pair(password)
self.pairings.addPairing(self.appInfo.instanceUID, pairing)
if self.attemptOpenSecureChannel():
self.onSecureChannelOpened()
except KeycardPairException:
discard #display wrong pairing password message
proc authenticate*(self: KeycardView, pin: string) {.slot.} =
discard """
@ -91,3 +115,29 @@ QtObject:
proc recoverAccount*(self: KeycardView) {.slot.} =
discard """
"""
proc getCardState*(self: KeycardView) =
var appInfo: KeycardApplicationInfo
try:
appInfo = self.status.keycard.select()
except KeycardSelectException as ex:
self.cardUnhandledError(ex.error)
return
self.appInfo = appInfo
if not appInfo.installed:
self.cardState = NotKeycard
self.cardNotKeycard()
elif not appInfo.initialized:
self.cardState = PreInit
self.cardPreInit()
elif self.attemptOpenSecureChannel():
self.onSecureChannelOpened()
elif appInfo.availableSlots > 0:
self.cardState = Unpaired
self.cardUnpaired()
else:
self.cardState = NoFreeSlots
self.cardNoFreeSlots()