Skip to content

Commit b33c1f3

Browse files
authored
Add liveness sessions for API contract 2.0.0 (#8)
createLivenessSession (named in each language's style) starts a session and returns its ID, the challenges to show in order and the expiry. completeLivenessSession submits the frames with mirrored and returns isLive, the steps and, when the session passed, the liveness token used for enrolment. LIVENESS_SESSION_INVALID surfaces as the typed API error. Completion is retried only on 429: any other answer, 503 included, means the session was already used up, so a retry could only fail. BREAKING: activeLiveness results no longer carry the token fields, because the API stopped returning them. This is a new major version, pinned to contract 2.0.0, with migration notes in the CHANGELOG.
1 parent 441a67d commit b33c1f3

11 files changed

Lines changed: 849 additions & 78 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Changelog
2+
3+
All notable changes to this project are documented here. The format follows
4+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses
5+
[Semantic Versioning](https://semver.org/).
6+
7+
## [1.0.0]
8+
9+
Validated against API contract 2.0.0.
10+
11+
### BREAKING
12+
13+
- `faces.active_liveness` no longer returns a liveness token: the API's
14+
stateless check stopped issuing them. `ActiveLivenessResult.liveness_token`
15+
and `ActiveLivenessResult.liveness_token_expires_at` are removed.
16+
17+
Migration: create a session, show its challenges, complete it with the frames,
18+
then enrol with the token it returns.
19+
20+
```python
21+
session = client.faces.create_liveness_session(collection_id)
22+
# show session.challenges in order, capture frames while they are performed
23+
result = client.faces.complete_liveness_session(collection_id, session.session_id, frames, mirrored=True)
24+
if result.liveness_token:
25+
client.faces.register(collection_id, image, external_id, liveness_token=result.liveness_token)
26+
```
27+
28+
### Added
29+
30+
- `faces.create_liveness_session(collection_id)` returns a `LivenessSession`
31+
(`session_id`, `challenges`: `blink` / `turn_left` / `turn_right` in order,
32+
`expires_at`).
33+
- `faces.complete_liveness_session(collection_id, session_id, frames, mirrored=False)`
34+
returns a `LivenessSessionResult`: the active-liveness fields, `steps`
35+
(`LivenessStep`: `type`, `passed`) and, when it passed, `liveness_token` and
36+
`liveness_token_expires_at`. It is retried on a 429 only, never on a network
37+
error or a 5xx, because a session is judged once.
38+
- Error code `LIVENESS_SESSION_INVALID` (422), raised as `LiveXFaceApiError`,
39+
when a session is unknown, expired, already submitted or bound to another
40+
collection.
41+
42+
### Changed
43+
44+
- Pinned API contract 2.0.0 (`contract/openapi-2.0.0.json`,
45+
`livexface.CONTRACT_VERSION`).

‎CONTRACT_VERSION‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
2.0.0

‎README.md‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Official Python SDK for [LiveXFace](https://github.com/livexface/livexface-pytho
1515
pip install livexface
1616
```
1717

18-
Validated against API contract 1.0.0 (`/openapi.json` `info.version`). The test suite calls every client method against the contract committed in `contract/` and fails if a method, path or required field is not in it; to move to a new contract, copy the release asset `openapi-<version>.json` into `contract/`, then update `CONTRACT_VERSION` and `livexface.CONTRACT_VERSION`.
18+
Validated against API contract 2.0.0 (`/openapi.json` `info.version`). The test suite calls every client method against the contract committed in `contract/` and fails if a method, path or required field is not in it; to move to a new contract, copy the release asset `openapi-<version>.json` into `contract/`, then update `CONTRACT_VERSION` and `livexface.CONTRACT_VERSION`.
1919

2020
## Quick Start
2121

@@ -61,27 +61,53 @@ liveness = client.faces.liveness(
6161
print(f"Live: {liveness.is_live}, Score: {liveness.liveness_score:.3f}")
6262
```
6363

64-
## Active Liveness and Enrolment
64+
## Liveness Sessions and Enrolment
6565

66-
A collection can require a liveness check before a face is enrolled. Run an
67-
active check over 5 to 50 frames captured in order (the user blinks and turns
68-
their head), then pass the token it returns when you register. The token is
69-
single-use, valid for 5 minutes, and bound to the collection.
66+
A collection can require a liveness check before a face is enrolled. The
67+
token for that comes from a liveness session: the server picks the steps the
68+
person must perform, in order, and the frames must show them.
69+
70+
1. Create a session. `challenges` lists the steps: `blink`, `turn_left` or
71+
`turn_right` (the person's own left and right). Submit before `expires_at`
72+
(60 seconds by default).
73+
2. Show each prompt and capture 5 to 50 frames, in order, while the person
74+
performs them.
75+
3. Complete the session with the frames. Pass `mirrored=True` when they are
76+
horizontally mirrored, as a selfie preview is.
77+
4. If it passed, register with its `liveness_token`: single-use, valid for 5
78+
minutes, and bound to the collection.
7079

7180
```python
72-
frames = [open(f"frame_{i}.jpg", "rb") for i in range(10)]
73-
check = client.faces.active_liveness("collection-uuid", frames)
74-
print(f"Live: {check.is_live}, Blink: {check.blink.passed}, Head turn: {check.head_turn.passed}")
81+
session = client.faces.create_liveness_session("collection-uuid")
82+
prompts = {"blink": "Blink", "turn_left": "Turn your head to your left", "turn_right": "Turn your head to your right"}
83+
for step in session.challenges:
84+
print(prompts[step]) # show the prompt, keep capturing frames
85+
86+
frames = [open(f"frame_{i}.jpg", "rb") for i in range(25)]
87+
result = client.faces.complete_liveness_session("collection-uuid", session.session_id, frames, mirrored=True)
88+
print(f"Live: {result.is_live}, Steps: {[(s.type, s.passed) for s in result.steps]}")
7589

76-
if check.liveness_token:
90+
if result.liveness_token:
7791
face = client.faces.register(
7892
collection_id="collection-uuid",
7993
image=open("photo.jpg", "rb"),
8094
external_id="user_123",
81-
liveness_token=check.liveness_token,
95+
liveness_token=result.liveness_token,
8296
)
8397
```
8498

99+
A session is judged once. Any submission except one with fewer than 5 frames
100+
(400 `IMAGE_REQUIRED`, the session is kept) uses it up, so
101+
`complete_liveness_session` is never retried on a network error or a 5xx;
102+
create a new session instead. Completing fails with `LIVENESS_SESSION_INVALID`
103+
(422) when the session is unknown, expired, already submitted or bound to
104+
another collection, and with `SERVICE_BUSY` (503) when the engine is
105+
saturated; create a new session for either.
106+
107+
`active_liveness(collection_id, frames)` still checks frames for a blink, a
108+
head turn and passive anti-spoofing, but it returns a verdict only and no
109+
token.
110+
85111
Batch items accept a `liveness_token` key too. Enrolment fails with
86112
`LIVENESS_TOKEN_REQUIRED` (400) when the collection requires a token and none
87113
was sent, `LIVENESS_TOKEN_INVALID` (422) when it is expired, used or for
@@ -149,6 +175,8 @@ after the first): a 429 or 503 is retried after its `Retry-After`, capped at
149175
none. A network error or another 5xx is retried only for reads, deletions and
150176
calls that carry an idempotency key; other 4xx are never retried. Enrolment and
151177
batch calls send one key on every attempt, generating it when you give none.
178+
`complete_liveness_session` is retried on a 429 only, since a session is
179+
judged once.
152180

153181
```python
154182
from livexface import LiveXFace, LiveXFaceApiError, new_idempotency_key

0 commit comments

Comments
 (0)