Links

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.
/// USER SESSION HAS BEEN INITIALIZED
/// USER HAS BEEN ENROLLED
// These are enrollment outputs, load them here before authentication
std::vector<unsigned char> user_key = loadUserKey();
std::vector<unsigned char> user_secret = loadUserSecret();
try
{
// IAuthenticationobject will hold the authentication result
keyless::IAuthentication::Ptr auth;
// Perform authentication using previously selected and configured camera device
auth = session->authenticate(*device_selector, user_key, nullptr);
// Verify matching User Secret objects from enrollment and authentication
// in the simplest way here
bool ok = (user_secret.size() == auth->getUserSecret().getDataSize() &&
memcmp(user_secret.data(),
auth->getUserSecret().getData(),
user_secret.size()) == 0);
if (ok) {
/// USER SUCCESFULLY AUTHENTICATED
}
else {
/// USER AUTHENTICATION FAILED
}
}
catch (const keyless::IException& e)
{
std::cerr << e.message() << std::endl;
}

Asynchronous authentication

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

Status updates

We may provide both authenticate() and authenticateAsync() methods with our implementation of IStatusCallbacks interface which will report enrollment progress.
class KeylessWndStatusCallbacks : public keyless::IStatusCallbacks
{
private:
void KEYLESS_SDK_CALL onFrameCapture(const keyless::IFrame& frame) noexcept override
{
// New frame capture from the camera, save it to be displayed to user
}
void KEYLESS_SDK_CALL onSessionStatus(keyless::SessionStatus status, const keyless::IException& e) noexcept override
{
// Overall execution status has changed
}
void KEYLESS_SDK_CALL onLivenessStatus(keyless::LivenessStatus status) noexcept override
{
// Liveness detection status has changed
}
void KEYLESS_SDK_CALL onRecognitionStatus(keyless::FaceRecognitionStatus status) noexcept override
{
// Face recognition status has changed
}
}