Máy chủ Passkey (WebAuthn) API
Thêm passkey cho mọi backend: tạo options đăng ký và đăng nhập WebAuthn, xác minh attestation và chữ ký; ứng dụng chỉ cần lưu credential.
Máy chủ passkey không lưu trạng thái, xây trên SimpleWebAuthn. POST /v1/webauthn/register/options với rp_id (tên miền của bạn) và user_name trả về options cho navigator.credentials.create() (hoặc startRegistration() của @simplewebauthn/browser), challenge và challenge_token: token ký HMAC chứa challenge, RP ID và hạn 5 phút (timeout_ms, tối đa 10 phút), nên bạn không cần nơi lưu phiên. Sau đó POST /register/verify với response từ trình duyệt, challenge_token (hoặc expected_challenge bạn tự lưu + rp_id) và expected_origin: kiểm tra attestation none, packed, TPM, Android Key / SafetyNet, Apple và FIDO U2F, trả về credential.id, credential.public_key (COSE base64url, kèm JWK và PEM), counter, transports, AAGUID kèm tên trình quản lý passkey phổ biến, user_verified, device_type và backed_up.
Khi đăng nhập, gọi POST /login/options (có thể kèm allow_credentials) rồi POST /login/verify với response, token và credential đã lưu {id, public_key, counter}: kiểm tra chữ ký, origin, mã băm RP ID, sự có mặt / xác minh người dùng (require_user_verification) và bộ đếm chữ ký; hãy lưu new_counter. Thuật toán mặc định [-8, -7, -257] (EdDSA, ES256, RS256).
Phía chúng tôi không lưu gì: bạn giữ credential (một người dùng có thể có nhiều passkey) và tự quyết định đăng nhập thành công thì làm gì. Token không lưu trạng thái và còn hiệu lực đến khi hết hạn: hãy đánh dấu token đã dùng nếu cần challenge dùng một lần tuyệt đối. Không kèm FIDO Metadata Service (điều khoản cấm phân phối lại), nên không đối chiếu chứng nhận của mẫu thiết bị.
Markdown · OpenAPI · Postman · llms.txt
Tham số
| Tên | Vị trí | Kiểu | Bắt buộc | Mặc định | |
|---|---|---|---|---|---|
operation | path | string | có | register/options, register/verify, login/options hoặc login/verify (hoặc gửi op trong body). | |
rp_id | body | string | có | RP ID: tên miền của bạn, không có scheme (options; verify khi dùng expected_challenge). | |
rp_name | body | string | không | Tên hiển thị trên trình duyệt (register/options). | |
user_name | body | string | có | Tên tài khoản, ví dụ e-mail (register/options). | |
user_id | body | string | không | Id người dùng dạng mờ, tối đa 64 byte (ngẫu nhiên nếu bỏ trống; trả về base64url). | |
user_display_name | body | string | không | Tên hiển thị (register/options). | |
attestation | body | string | không | none | none, direct hoặc enterprise. |
authenticator_attachment | body | string | không | platform (passkey trên thiết bị) hoặc cross-platform (khoá bảo mật). | |
resident_key | body | string | không | preferred | required, preferred hoặc discouraged (credential tự phát hiện). |
user_verification | body | string | không | preferred | required, preferred hoặc discouraged. |
exclude_credentials | body | array | không | Các id credential người dùng đã có (hoặc {id, transports}) (register/options). | |
allow_credentials | body | array | không | Các credential được phép đăng nhập (login/options); để trống = passkey tự phát hiện. | |
timeout_ms | body | number | không | 300000 | Thời gian chờ và hạn token, 30 000-600 000 ms. |
response | body | object | có | JSON credential từ trình duyệt (verify). | |
challenge_token | body | string | không | Token từ lệnh options (verify). | |
expected_challenge | body | string | không | Challenge bạn tự lưu, thay cho token (verify, kèm rp_id). | |
expected_origin | body | string | không | Origin được phép, ví dụ https://example.com (mặc định https://<rp_id>). | |
credential | body | object | không | Credential đã lưu {id, public_key, counter, transports} (login/verify). | |
require_user_verification | body | boolean | không | false | Báo lỗi nếu authenticator không xác minh người dùng (PIN / sinh trắc). |
Ví dụ
curl -X POST "https://apibox.io.vn/v1/webauthn/register/options" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"rp_id":"example.com","rp_name":"Example","user_name":"an@example.vn","user_id":"user-42"}'const res = await fetch('https://apibox.io.vn/v1/webauthn/register/options', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"rp_id": "example.com",
"rp_name": "Example",
"user_name": "an@example.vn",
"user_id": "user-42"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/webauthn/register/options",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"rp_id": "example.com", "rp_name": "Example", "user_name": "an@example.vn", "user_id": "user-42"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/webauthn/register/options');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"rp_id":"example.com","rp_name":"Example","user_name":"an@example.vn","user_id":"user-42"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Kết quả trả về
{
"options": {
"challenge": "Wbjgnz1u4oqN_OSWuvCfxO0QJ_E08LK0EM5t58J69w0",
"rp": {
"name": "Example",
"id": "example.com"
},
"user": {
"id": "dXNlci00Mg",
"name": "an@example.vn",
"displayName": "an@example.vn"
},
"pubKeyCredParams": [
{
"alg": -8,
"type": "public-key"
},
{
"alg": -7,
"type": "public-key"
},
{
"alg": -257,
"type": "public-key"
}
],
"timeout": 300000,
"attestation": "none",
"excludeCredentials": [],
"authenticatorSelection": {
"residentKey": "preferred",
"userVerification": "preferred",
"requireResidentKey": false
},
"extensions": {
"credProps": true
},
"hints": []
},
"challenge": "Wbjgnz1u4oqN_OSWuvCfxO0QJ_E08LK0EM5t58J69w0",
"challenge_token": "eyJrIjoicmVnIiwiYyI6IldiamduejF1NG9xTl9PU1d1dkNmeE8wUUpfRTA4TEswRU01dDU4SjY5dzAiLCJyIjoiZXhhbXBsZS5jb20iLCJlIjoxNzk0Mzk0MzYyLCJvIjoiZGVtbyJ9.tT-u26UHsnaeEQj8K_fcWoIEi20XctaQPWXjveP0q7k",
"expires_at": "2026-11-11T10:52:42.000Z",
"user_id": "dXNlci00Mg",
"next": "Pass `options` to navigator.credentials.create() (or startRegistration() of @simplewebauthn/browser), then POST the result as `response` with `challenge_token` to /v1/webauthn/register/verify."
}
Ứng dụng
- Thêm đăng nhập passkey cho backend PHP, WordPress, Laravel, Apps Script mà không cần thư viện WebAuthn
- Thay OTP SMS bằng passkey chống lừa đảo cho tài khoản khách hàng
- Đăng nhập không mật khẩu cho công cụ nội bộ, trang quản trị
- Thử nghiệm luồng WebAuthn và xem dữ liệu thật từ authenticator
Hỏi đáp
Ứng dụng của tôi phải lưu gì?
Id, khoá công khai, bộ đếm và transports của từng passkey, gắn với người dùng. Có thể lưu thêm challenge (thay cho challenge_token) và các token đã dùng.
Có dùng được với passkey của iPhone, Android, Windows Hello không?
Có. Passkey đồng bộ thường dùng attestation "none" và giữ bộ đếm bằng 0, vẫn được chấp nhận.
Gửi origin nào?
Đúng origin của trang gọi WebAuthn, ví dụ https://example.com (cho phép nhiều giá trị). rp_id phải là tên miền đó hoặc tên miền cha.
API liên quan
Tạo mã hash
Tính MD5, SHA-1, SHA-256, SHA-384, SHA-512 và CRC32 của chuỗi hoặc file trong một lần gọi, xuất hex hoặc base64, hỗ trợ ký HMAC.
B64Mã hoá / Giải mã
Mã hoá hoặc giải mã chuỗi sang Base64, Base64URL, hex, URL encode, HTML entity, Unicode escape, ROT13 hay nhị phân chỉ với một lần gọi API.
***Tạo mật khẩu
Tạo mật khẩu ngẫu nhiên an toàn bằng CSPRNG (4–256 ký tự, tối đa 100 cái mỗi lần) hoặc kiểm tra độ mạnh, entropy và thời gian bẻ khoá của mật khẩu.