Authentication
Example for how to authenticate a user, after they have been enrolled.
The authentication verifies the validity of the face features captured by the selected camera device and produces a User Secret. This object should match the original User Secret obtained during a previous enrollment.
To authenticate a user we must first perform the enrollment (see Enrollment).

Synchronous authentication

We will perform authentication using a simple blocking call.
1
/// USER SESSION HAS BEEN INITIALIZED
2
/// USER HAS BEEN ENROLLED
3
4
// These are enrollment outputs, load them here before authentication
5
std::vector<unsigned char> user_key = loadUserKey();
6
std::vector<unsigned char> user_secret = loadUserSecret();
7
8
try
9
{
10
// IAuthenticationobject will hold the authentication result
11
keyless::IAuthentication::Ptr auth;
12
13
// Perform authentication using previously selected and configured camera device
14
auth = session->authenticate(*device_selector, user_key, nullptr);
15
16
// Verify matching User Secret objects from enrollment and authentication
17
// in the simplest way here
18
bool ok = (user_secret.size() == auth->getUserSecret().getDataSize() &&
19
memcmp(user_secret.data(),
20
auth->getUserSecret().getData(),
21
user_secret.size()) == 0);
22
23
if (ok) {
24
/// USER SUCCESFULLY AUTHENTICATED
25
}
26
else {
27
/// USER AUTHENTICATION FAILED
28
}
29
}
30
catch (const keyless::IException& e)
31
{
32
std::cerr << e.message() << std::endl;
33
}
34
Copied!

Asynchronous authentication

To perform the authentication we call the authenticateAsync()method with our implementation of IAuthenticationCallbacks interface which will report the execution end.
1
class KeylessAuthenticationCallbacks : public keyless::IAuthenticationCallbacks
2
{
3
private:
4
void KEYLESS_SDK_CALL onFailure(const keyless::IException& exception) noexcept override
5
{
6
// Authentication has ended with failure here, save details from `exception`
7
}
8
9
void KEYLESS_SDK_CALL onSuccess(keyless::IAuthentication* authentication) noexcept override
10
{
11
// Authentication has ended with success here, save results from `authentication`
12
}
13
14
keyless::IStatusCallbacks* KEYLESS_SDK_CALL getStatusCallbacks() noexcept override
15
{
16
return nullptr; // see "Status Updates" below
17
}
18
};
Copied!
We will also retrieve the IControl interface allowing us to stop the running execution.
1
/// USER SESSION HAS BEEN INITIALIZED
2
/// USER HAS BEEN ENROLLED
3
4
try
5
{
6
// declare our asynchronous callbacks
7
KeylessAuthenticationCallbacks callbacks;
8
9
// Perform enrollment
10
keyless::IControl& control = session->authenticateAsync(callbacks);
11
12
/// AUTHENTICATION IN PROGRESS, WAIT FOR RESULT VIA CALLBACKS
13
14
// we can optionally interrupt the running execution with this call
15
control.stop();
16
}
17
catch (const keyless::IException& e)
18
{
19
std::cerr << e.message() << std::endl;
20
}
Copied!

Status updates

We may provide both authenticate() and authenticateAsync() methods with our implementation of IStatusCallbacks interface which will report enrollment progress.
1
class KeylessWndStatusCallbacks : public keyless::IStatusCallbacks
2
{
3
private:
4
void KEYLESS_SDK_CALL onFrameCapture(const keyless::IFrame& frame) noexcept override
5
{
6
// New frame capture from the camera, save it to be displayed to user
7
}
8
void KEYLESS_SDK_CALL onSessionStatus(keyless::SessionStatus status, const keyless::IException& e) noexcept override
9
{
10
// Overall execution status has changed
11
}
12
void KEYLESS_SDK_CALL onLivenessStatus(keyless::LivenessStatus status) noexcept override
13
{
14
// Liveness detection status has changed
15
}
16
void KEYLESS_SDK_CALL onRecognitionStatus(keyless::FaceRecognitionStatus status) noexcept override
17
{
18
// Face recognition status has changed
19
}
20
}
Copied!