Bỏ qua, đến nội dung

@digiforce-nc/plugin-field-encryption ​

Plugin cung cấp kiểu field mã hóa — tự động encrypt dữ liệu trước khi lưu vào database (AES) và decrypt khi đọc ra, hỗ trợ per-field encryption key, custom operators cho query, và CLI key-rotation.

Plugin này làm gì? ​

Hãy hình dung: khác với mask (chỉ che trên UI), plugin này mã hóa thực sự dữ liệu trong database. Cột lưu trữ là TEXT chứa ciphertext — không ai đọc được dữ liệu gốc khi truy cập trực tiếp DB. Server tự động encrypt khi ghi và decrypt khi đọc, mỗi field có thể dùng encryption key riêng.

Bốn nhiệm vụ chính ​

#Nhiệm vụChi tiết
1Encrypt/DecryptAES mã hóa khi ghi, giải mã khi đọc — transparent với application
2Per-field keyMỗi field có thể cấu hình encryption key riêng trong metadata
3Custom operators$encryptionEq, $encryptionNe — so sánh giá trị mã hóa trong query
4Key rotation CLICommand line để xoay vòng encryption key mà không mất dữ liệu

Kiến trúc ​

Tổng quan 3 tầng ​

TầngVai tròThành phần
ClientNhập/hiển thị plaintext bình thườngInputEncryptionFieldWidget, DisplayEncryptionFieldWidget
ServerEncrypt khi ghi, decrypt khi đọcField type encryption, hooks, operators
DatabaseLưu ciphertext dạng TEXTCột với giá trị mã hóa AES

Luồng Encrypt khi ghi ​

Field type encryption ​

Thuộc tínhGiá trị
DB column typeTEXT
Encrypt algorithmAES
Key storageField metadata (options)
HooksbeforeCreate, beforeUpdate (encrypt), afterFind (decrypt)

Custom operators ​

OperatorMô tả
$encryptionEqSo sánh bằng — encrypt giá trị cần tìm rồi so với ciphertext trong DB
$encryptionNeSo sánh khác — ngược lại $encryptionEq

Key management & CLI ​

Mỗi field encryption có thể có key riêng, lưu trong field metadata. Hooks beforeCreate và beforeUpdate tự động tạo/cập nhật key khi cần. Plugin cung cấp CLI command key-rotation để xoay vòng key (decrypt bằng key cũ → re-encrypt bằng key mới) mà không mất dữ liệu.

Error handling ​

Plugin đăng ký với plugin-error-handler để bắt EncryptionError (decrypt thất bại do key sai hoặc dữ liệu corrupt).


Ví dụ sử dụng ​

Cấu hình field encryption ​

typescript
const ssnField = {
  name: 'ssn',
  type: 'encryption',
  interface: 'encryption',
  uiSchema: {
    type: 'string',
    title: 'Số CCCD',
    'x-component': 'InputEncryption',
  },
};

Query với custom operator ​

typescript
const result = await repository.find({
  filter: {
    ssn: { $encryptionEq: '001234567890' },
  },
});

Thành phần client ​

Thành phầnMô tả
EncryptionFieldInterfaceĐịnh nghĩa interface cho field encryption
InputEncryptionFieldWidgetWidget nhập liệu (hiển thị plaintext)
DisplayEncryptionFieldWidgetWidget hiển thị (giải mã từ server)

Dependencies ​

PackageVai trò
@digiforce-nc/serverServer framework — hook encrypt/decrypt
@digiforce-nc/databaseDatabase ORM — đăng ký field type
@digiforce-nc/clientClient framework — đăng ký component
@digiforce-nc/plugin-error-handlerXử lý EncryptionError

Mục lục chi tiết ​