User API Tutorial
Run the example
- Install and run the gateway
- Download the Python client library
- Copy the root certificate of the gateway to your working directory. As default, the certificate(ca.crt) resides in cert of the installation directory.
-
Change the server and the device information in example/user/test/test.py as needed.
# the path of the root certificate GATEWAY_CA_FILE = '../../../../cert/gateway/ca.crt' # the ip address of the gateway GATEWAY_IP = '192.168.0.2' GATEWAY_PORT = 4000 # the ip address of the target device DEVICE_IP = '192.168.0.110' DEVICE_PORT = 51211 USE_SSL = False
-
Run.
cd example/user/test python test.py
1. Connect to the gateway and the device
The example assumes you use the device gateway. For the master gateway or other connection options, refer to the Connect or ConnectMaster examples.
client = GatewayClient(GATEWAY_IP, GATEWAY_PORT, GATEWAY_CA_FILE)
channel = client.getChannel()
connectSvc = ConnectSvc(channel)
connInfo = connect_pb2.ConnectInfo(IPAddr=DEVICE_IP, port=DEVICE_PORT, useSSL=USE_SSL)
devID = connectSvc.connect(connInfo)
2. Change the auth configuration
To test the authentication modes of the test user, AuthConfig.usePrivateAuth will be enabled.
config = authSvc.getConfig(deviceID)
# Backup the original configuration
origConfig = auth_pb2.AuthConfig()
origConfig.CopyFrom(config)
# Enable private authentication for test
config.usePrivateAuth = True
authSvc.setConfig(deviceID, config)
3. Enroll users
Enroll a test user and set the authentication modes. For brevity, the example sets only the simple authentication modes. You can test all the modes by changing these values.
newUserID = "%d" % int(time.time())
newUserHdr = user_pb2.UserHdr(ID=newUserID)
newUser = user_pb2.UserInfo(hdr=newUserHdr, setting=user_pb2.UserSetting())
if deviceType == device_pb2.FACESTATION_F2 or deviceType == device_pb2.FACESTATION_F2_FP:
newUser.setting.cardAuthExtMode = auth_pb2.AUTH_EXT_MODE_CARD_ONLY
newUser.setting.fingerAuthExtMode = auth_pb2.AUTH_EXT_MODE_FINGERPRINT_ONLY
newUser.setting.faceAuthExtMode = auth_pb2.AUTH_EXT_MODE_FACE_ONLY
else:
newUser.setting.cardAuthMode = auth_pb2.AUTH_MODE_CARD_ONLY
newUser.setting.biometricAuthMode = auth_pb2.AUTH_MODE_BIOMETRIC_ONLY
userSvc.enroll(deviceID, [newUser], True)
4. Add credentials to users
Add cards, fingerprints, or faces to the test user. To know the supported credentials of each device, you can use Device.GetCapabilityInfo. For added credentials, check whether you can authenticate them on the device.
capInfo = deviceSvc.getCapInfo(devID)
if capInfo.cardSupported:
cardSvc = CardSvc(channel)
TestCard(cardSvc, userSvc).test(devID, testUserID)
if capInfo.fingerSupported:
fingerSvc = FingerSvc(channel)
TestFinger(fingerSvc, userSvc).test(devID, testUserID)
if capInfo.faceSupported:
faceSvc = FaceSvc(channel)
TestFace(faceSvc, userSvc).test(devID, testUserID)
cardData = cardSvc.scan(deviceID)
userCard = user_pb2.UserCard(userID=userID, cards=[cardData.CSNCardData])
userSvc.setCard(deviceID, [userCard])
pressEnter('>> Try to authenticate the enrolled card. And, press ENTER to end the test.\n')
fingerData = finger_pb2.FingerData()
fingerData.templates.append(fingerSvc.scan(deviceID, TEMPLATE_FORMAT, QUALITY_THRESHOLD))
fingerData.templates.append(fingerSvc.scan(deviceID, TEMPLATE_FORMAT, QUALITY_THRESHOLD))
userFinger = user_pb2.UserFinger(userID=userID, fingers=[fingerData])
userSvc.setFinger(deviceID, [userFinger])
pressEnter('>> Try to authenticate the enrolled finger. And, press ENTER to end the test.\n')
faceData = faceSvc.scan(deviceID, ENROLL_THRESHOLD)
userFace = user_pb2.UserFace(userID=userID, faces=[faceData])
userSvc.setFace(deviceID, [userFace])
pressEnter('>> Try to authenticate the enrolled face. And, press ENTER to end the test.\n')
5. Test the authentication modes
You can set the authentication modes of a device using AuthConfig.
At this step, AuthConfig.usePrivateAuth is set to false. In other words, these authentication modes will be applied to all the users.
config = auth_pb2.AuthConfig(matchTimeout=10, authTimeout=15, usePrivateAuth=False)
if deviceType == device_pb2.FACESTATION_F2 or deviceType == device_pb2.FACESTATION_F2_FP:
config.authSchedules.add(mode=auth_pb2.AUTH_EXT_MODE_CARD_ONLY, scheduleID=1) # Card Only, Always
config.authSchedules.add(mode=auth_pb2.AUTH_EXT_MODE_FACE_ONLY, scheduleID=1) # Face Only, Always
config.authSchedules.add(mode=auth_pb2.AUTH_EXT_MODE_FINGERPRINT_ONLY, scheduleID=1) # Fingerprint Only, Always
else:
config.authSchedules.add(mode=auth_pb2.AUTH_MODE_CARD_ONLY, scheduleID=1) # Card Only, Always
config.authSchedules.add(mode=auth_pb2.AUTH_MODE_BIOMETRIC_ONLY, scheduleID=1) # Biometric Only, Always
authSvc.setConfig(deviceID, config)
pressEnter('>> Try to authenticate card or fingerprint or face. And, press ENTER for the next test.\n')
del config.authSchedules[:]
if deviceType == device_pb2.FACESTATION_F2 or deviceType == device_pb2.FACESTATION_F2_FP:
config.authSchedules.add(mode=auth_pb2.AUTH_EXT_MODE_CARD_FACE, scheduleID=1) # Card + Face, Always
config.authSchedules.add(mode=auth_pb2.AUTH_EXT_MODE_CARD_FINGERPRINT, scheduleID=1) # Card + Fingerprint, Always
else:
config.authSchedules.add(mode=auth_pb2.AUTH_MODE_CARD_BIOMETRIC, scheduleID=1) # Card + Biometric, Always
authSvc.setConfig(deviceID, config)
pressEnter('>> Try to authenticate (card + fingerprint) or (card + face). And, press ENTER for the next test.\n')
6. Get the event logs
You can also subscribe to an event stream for receiving realtime events.
eventSvc.enableMonitoring(deviceID)
eventCh = eventSvc.subscribe(EVENT_QUEUE_SIZE)
statusThread = threading.Thread(target=handleEvent)
statusThread.start()
# ..
def handleEvent():
for event in eventCh:
# do something with the event