Xác thực thiết bị (Device Credentials)

Ba phương thức xác thực thiết bị trong VieLang IoT: Access Token, Basic MQTT, X.509 Certificate.

Xác thực thiết bị (Device Credentials)

VieLang IoT hỗ trợ ba phương thức xác thực thiết bị, với mức độ bảo mật và độ phức tạp khác nhau.

So sánh nhanh

Phương thứcBảo mậtĐộ phức tạpPhù hợp
Access TokenTrung bìnhThấpDev, thiết bị đơn giản
Basic MQTTTrung bìnhThấpMQTT-only devices
X.509 CertificateCaoCaoProduction, IoT lớn

1. Access Token

Phương thức đơn giản nhất. Thiết bị dùng token như username khi kết nối.

Ưu điểm:

  • Dễ triển khai, không cần library phức tạp
  • Hỗ trợ tất cả giao thức: MQTT, HTTP, CoAP

Nhược điểm:

  • Dễ bị đánh cắp nếu không dùng TLS
  • Token bị lộ → phải thu hồi thủ công

Dùng với MQTT

mosquitto_pub \ -h localhost -p 1883 \ -u "YOUR_ACCESS_TOKEN" \ -t "v1/devices/me/telemetry" \ -m '{"temperature": 25}'

Dùng với HTTP

POST http://localhost:8080/api/v1/YOUR_ACCESS_TOKEN/telemetry

Dùng với CoAP

coap-client -m post \ coap://localhost:5683/api/v1/YOUR_ACCESS_TOKEN/telemetry \ -e '{"temperature": 25}'

Lấy / Đặt Access Token qua API

# Lấy credentials hiện tại GET /api/device/{deviceId}/credentials Authorization: Bearer {JWT} # Response: { "id": { "id": "cred-uuid", "entityType": "DEVICE_CREDENTIALS" }, "deviceId": { "id": "device-uuid", "entityType": "DEVICE" }, "credentialsType": "ACCESS_TOKEN", "credentialsId": "YOUR_ACCESS_TOKEN" } # Đặt token mới POST /api/device/credentials Authorization: Bearer {JWT} Content-Type: application/json { "deviceId": { "id": "device-uuid", "entityType": "DEVICE" }, "credentialsType": "ACCESS_TOKEN", "credentialsId": "MY_CUSTOM_TOKEN_123" }

2. Basic MQTT Credentials

Xác thực bằng Client ID + Username + Password theo chuẩn MQTT.

Ưu điểm: Tích hợp tốt với hệ thống MQTT hiện có, hỗ trợ client ID tùy chỉnh.

Nhược điểm: Chỉ dùng với MQTT, không dùng được với HTTP/CoAP.

mosquitto_pub \ -h localhost -p 1883 \ --id "my-device-client-id" \ -u "my-username" \ -P "my-password" \ -t "v1/devices/me/telemetry" \ -m '{"temperature": 25}'

Đặt Basic MQTT Credentials qua API

POST /api/device/credentials Authorization: Bearer {JWT} Content-Type: application/json { "deviceId": { "id": "device-uuid", "entityType": "DEVICE" }, "credentialsType": "MQTT_BASIC", "credentialsValue": "{\"clientId\":\"my-client\",\"userName\":\"my-user\",\"password\":\"my-pass\"}" }

3. X.509 Certificate

Xác thực bằng certificate PKI. Thiết bị và server đều xác thực lẫn nhau (mutual TLS).

Ưu điểm:

  • Bảo mật cao nhất — private key không bao giờ rời khỏi thiết bị
  • Tiêu chuẩn công nghiệp cho IoT

Nhược điểm:

  • Cần phần cứng đủ mạnh (CPU cho TLS handshake)
  • Phức tạp hơn trong triển khai

Tạo certificate (dev/test)

# Tạo CA openssl genrsa -out ca.key 4096 openssl req -new -x509 -days 3650 -key ca.key -out ca.crt # Tạo device certificate openssl genrsa -out device.key 2048 openssl req -new -key device.key -out device.csr openssl x509 -req -days 365 -in device.csr -CA ca.crt -CAkey ca.key \ -CAcreateserial -out device.crt

Đăng ký certificate với VieLang IoT

POST /api/device/credentials Authorization: Bearer {JWT} Content-Type: application/json { "deviceId": { "id": "device-uuid", "entityType": "DEVICE" }, "credentialsType": "X509_CERTIFICATE", "credentialsValue": "-----BEGIN CERTIFICATE-----\n...PEM content...\n-----END CERTIFICATE-----" }

Kết nối MQTT với certificate

mosquitto_pub \ -h localhost -p 8883 \ --cert device.crt \ --key device.key \ --cafile ca.crt \ -t "v1/devices/me/telemetry" \ -m '{"temperature": 25}'

Cách cấp phát credentials

Thủ công qua UI (Development)

Dashboard → Devices → Chọn thiết bị → Tab Credentials → Edit.

Qua REST API (Manufacturing)

Phù hợp khi cần cấp phát hàng loạt trong quy trình sản xuất:

import requests BASE_URL = "http://localhost:8080" headers = {"Authorization": f"Bearer {jwt_token}"} # Tạo thiết bị + set token device = requests.post(f"{BASE_URL}/api/device", json={"name": f"Sensor-{serial}"}, headers=headers).json() requests.post(f"{BASE_URL}/api/device/credentials", json={ "deviceId": device["id"], "credentialsType": "ACCESS_TOKEN", "credentialsId": f"TOKEN_{serial}" }, headers=headers)

Tự động qua Device Provisioning

Thiết bị tự đăng ký khi lần đầu kết nối mà không cần cấu hình thủ công. Xem Device Provisioning.