User API Tutorial
Run the example
- Install and run the gateway
- Download the Go 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 gateway and the device information in src/example/user/test/test.go as needed.
// the path of the root certificate GATEWAY_CA_FILE = "../../../../cert/gateway/ca.crt" // the address of the device gateway GATEWAY_IP = "192.168.0.2" GATEWAY_PORT = 4000 // the ip address of the target device DEV_IP = "192.168.0.110" DEV_PORT = 51211 USE_SSL = false
-
Build.
cd src/example/user/test go build .
-
Run.
./test
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.
gatewayClient := &client.GatewayClient{}
gatewayClient.Connect(GATEWAY_CA_FILE, GATEWAY_IP, GATEWAY_PORT)
connectSvc = connect.NewConnectSvc(gatewayClient.GetConn())
deviceID, _ := connectSvc.Connect(DEV_IP, DEV_PORT, USE_SSL)
2. Change the auth configuration
To test the authentication modes of the test user, AuthConfig.usePrivateAuth will be enabled.
origConfig, _ := authSvc.GetConfig(deviceID)
newConfig := proto.Clone(origConfig).(*auth.AuthConfig)
newConfig.UsePrivateAuth = true
authSvc.SetConfig(deviceID, newConfig)
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 := fmt.Sprintf("%v", time.Now().Unix())
newUser := &user.UserInfo{
Hdr: &user.UserHdr{
ID: newUserID,
},
Setting: &user.UserSetting{
},
}
// Set authentication modes for test
if deviceType == device.Type_FACESTATION_F2 || deviceType == device.Type_FACESTATION_F2_FP {
newUser.Setting.CardAuthExtMode = uint32(auth.AuthMode_AUTH_EXT_MODE_CARD_ONLY)
newUser.Setting.FingerAuthExtMode = uint32(auth.AuthMode_AUTH_EXT_MODE_FINGERPRINT_ONLY)
newUser.Setting.FaceAuthExtMode = uint32(auth.AuthMode_AUTH_EXT_MODE_FACE_ONLY)
} else {
newUser.Setting.CardAuthMode = uint32(auth.AuthMode_AUTH_MODE_CARD_ONLY)
newUser.Setting.BiometricAuthMode = uint32(auth.AuthMode_AUTH_MODE_BIOMETRIC_ONLY)
}
userSvc.Enroll(deviceID, []*user.UserInfo{ newUser })
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.GetCapabilityInfo(deviceID)
if capInfo.CardSupported {
cardSvc = card.NewCardSvc(gatewayClient.GetConn())
testCard(deviceID, testUserID)
}
if capInfo.FingerSupported {
fingerSvc = finger.NewFingerSvc(gatewayClient.GetConn())
testFinger(deviceID, testUserID)
}
if capInfo.FaceSupported {
faceSvc = face.NewFaceSvc(gatewayClient.GetConn())
testFace(deviceID, testUserID)
}
cardData, _ := cardSvc.Scan(deviceID)
userCard := &user.UserCard{
UserID: userID,
Cards: []*card.CSNCardData{
cardData.CSNCardData,
},
}
userSvc.SetCard(deviceID, []*user.UserCard{ userCard })
cli.PressEnter(">> Try to authenticate the enrolled card. And, press ENTER to end the test.\n")
fingerData := &finger.FingerData{
Templates: [][]byte{},
}
for i := 0; i < NUM_OF_TEMPLATE; i++ {
templateData, _, _ := fingerSvc.Scan(deviceID, finger.TemplateFormat_TEMPLATE_FORMAT_SUPREMA, QUALITY_THRESHOLD)
fingerData.Templates = append(fingerData.Templates, templateData)
}
userFinger := &user.UserFinger{
UserID: userID,
Fingers: []*finger.FingerData{
fingerData,
},
}
userSvc.SetFinger(deviceID, []*user.UserFinger{ userFinger })
cli.PressEnter(">> Try to authenticate the enrolled finger. And press ENTER to end the test.\n")
faceData, _ := faceSvc.Scan(deviceID, ENROLL_THRESHOLD)
userFace := &user.UserFace{
UserID: userID,
Faces: []*face.FaceData{
faceData,
},
}
userSvc.SetFace(deviceID, []*user.UserFace{ userFace })
cli.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.
authConfig := &auth.AuthConfig{
MatchTimeout: 10,
AuthTimeout: 15,
}
if deviceType == device.Type_FACESTATION_F2 || deviceType == device.Type_FACESTATION_F2_FP {
authConfig.AuthSchedules = []*auth.AuthSchedule{
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_EXT_MODE_FACE_ONLY, ScheduleID: 1 }, // Face Only, Always
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_EXT_MODE_FINGERPRINT_ONLY, ScheduleID: 1 }, // Fingerprint Only, Always
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_EXT_MODE_CARD_ONLY, ScheduleID: 1 }, // Card Only, Always
}
} else {
authConfig.AuthSchedules = []*auth.AuthSchedule{
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_MODE_BIOMETRIC_ONLY, ScheduleID: 1 }, // Biometric Only, Always
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_MODE_CARD_ONLY, ScheduleID: 1 }, // Card Only, Always
}
}
authSvc.SetConfig(deviceID, authConfig)
cli.PressEnter(">> Try to authenticate card or fingerprint or face. And, press ENTER for the next test.\n")
if deviceType == device.Type_FACESTATION_F2 || deviceType == device.Type_FACESTATION_F2_FP {
authConfig.AuthSchedules = []*auth.AuthSchedule{
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_EXT_MODE_CARD_FACE, ScheduleID: 1 }, // Card + Face, Always
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_EXT_MODE_CARD_FINGERPRINT, ScheduleID: 1 }, // Card + Fingerprint, Always
}
} else {
authConfig.AuthSchedules = []*auth.AuthSchedule{
&auth.AuthSchedule{ Mode: auth.AuthMode_AUTH_MODE_CARD_BIOMETRIC, ScheduleID: 1 }, // Card + Biometric, Always
}
}
authSvc.SetConfig(deviceID, authConfig)
cli.PressEnter(">> Try to authenticate (card + fingerprint) or (card + face). And, press ENTER to end the test.\n")
6. Get the event logs
You can also subscribe to an event stream for receiving realtime events.
enableReq := &event.EnableMonitoringRequest{
DeviceID: deviceID,
}
s.client.EnableMonitoring(context.Background(), enableReq)
subReq := &event.SubscribeRealtimeLogRequest{
QueueSize: MONITORING_QUEUE_SIZE,
DeviceIDs: []uint32 { deviceID },
}
ctx, cancelFunc := context.WithCancel(context.Background())
eventStream, _ = s.client.SubscribeRealtimeLog(ctx, subReq)
go func() {
for {
eventLog, _ := eventStream.Recv()
eventCallback(eventLog)
}
} ()