Tạo chữ ký cho API V2 (Signature)

Mục lục

Bắt đầu từ các API thuộc mục LMS, giao diện API của ClassIn sẽ sử dụng tiêu đề để xác thực thay vì tham số SafeKey.

  • Giá trị chữ ký được mã hóa sẽ được đặt trong tiêu đề yêu cầu với tham số “X-EEO-SIGN”, và quy tắc mã hóa như sau.sign = md5(’stringkey&key=SECRET’)
  • Mã số định danh trường học (sid) và dấu thời gian (timeStamp) sẽ được đặt trong phần tiêu đề, tương ứng với các tham số “X-EEO-UID” và “X-EEO-TS”. Các tham số trong phần thân không được chứa sid và timeStamp.
  • “X-EEO-TS” phải đảm bảo trong vòng 5 phút gần nhất cho lệnh gọi API hiện tại.
  • Tên tham số “key” không được sử dụng.

Ví dụ tiêu đề

{
    "X-EEO-SIGN": "sign",  # chữ ký sign
    "X-EEO-UID": "sid",  # SID
    "X-EEO-TS": "timeStamp",  # Unix Epoch
    "Content-Type": "application/json"
}

Cách tạo chữ ký

Bước 1: Loại bỏ các tham số yêu cầu không liên quan

Dữ liệu được lưu trữ ở định dạng JSON. Hãy trích xuất các cặp khóa/giá trị được sử dụng để ký từ dữ liệu theo các quy tắc sau.

  1. Các tham số dạng array hoặc dictionary không được bao gồm trong chữ ký và phải được loại bỏ.
  2. Các tham số có độ dài byte lớn hơn 1024 sẽ không được bao gồm trong chữ ký và phải được loại bỏ.

Bước 2: Soạn chuỗi cần ký

  1. Thêm sid=XXXXXX và timeStamp=XXXXXXXX;
  2. Sắp xếp các tham số theo mã ASCII của từ nhỏ nhất đến lớn nhất (thứ tự từ điển) và nối chúng lại thành một chuỗi stringA bằng cách sử dụng định dạng cặp khóa-giá trị URL (ví dụ: key1=value1&key2=value2…).
  3. Thêm ‘&key=SECRET’ vào cuối chuỗi A.

Bước 3: Tính giá trị MD5 32bit chữ thường của chuỗi cần ký.

Tính toán mã MD5 (32bit chữ thường) của toàn bộ chuỗi rồi truyền cho trường chữ ký là sign;

Ví dụ

Giả sử yêu cầu được gửi với SECRET là:Mb7SR6H

curl --location --request POST '/lms/unit/test' \
--header 'X-EEO-SIGN: 4f97f55addf4921a05c2395617cd8a7b' \
--header 'X-EEO-UID: 1000082' \
--header 'X-EEO-TS: 1721095405' \
--header 'Content-Type: application/json' \
--data-raw '{
    "courseId": 132323,
    "unitJson": [
        {
            "name": "string",
            "content": "string",
            "publishFlag": 0
        }
    ],
}'
  1. Nối chuỗi yêu cầu

    Loại trừ các tham số yêu cầu không liên quan đến chữ ký: unitJson (mảng), thêm sid và timeStamp, ta có danh sách các tham số và giá trị yêu cầu sau:

    | Tên tham số | Giá trị tham số |
    | — | — |
    | sid | 1000082 |
    | courseId | 132323 |
    | timeStamp | 1721095405 |

    Sắp xếp các tham số thu được theo thứ tự tăng dần của mã ASCII (thứ tự từ điển) và nối chúng lại thành một chuỗi stringA bằng cách sử dụng định dạng cặp khóa-giá trị URL (tức là key1=value1&key2=value2…).

    Khóa được thêm vào cuối chuỗi stringA để thu được chuỗi cần ký.

    Dựa trên các quy tắc trên, chuỗi cần ký trong ví dụ như sau:

     courseId=132323&sid=1000082&timeStamp=1673949471&key=Mb7SR6H
    
  2. Tính toán chữ ký

    Thực hiện thuật toán MD5 trên chuỗi cần ký để thu được giá trị chữ ký.

    Kết quả của phép tính ví dụ này là 4f97f55addf4921a05c2395617cd8a7b

Thuật toán mẫu

Đoạn code mẫu của thuật toán tạo chữ ký

Chữ ký không thành công

Dưới đây là các mã lỗi liên quan đến việc không ký được chữ ký:

Mã lỗiNội dung
101002005Lỗi chữ ký (chữ ký không được gửi hoặc không chính xác)
101002006Thời gian xác thực đã hết hạn (trong vòng 5 phút trước/sau thời điểm hiện tại)
101002008Timestamp không hợp lệ
121601030Tham số không đầy đủ hoặc không chính xác

ClassIn has introduced a new series of APIs to support its upgraded LMS-featured Client. The new LMS APIs will use headers for authentication, no longer relying on the SafeKey parameter.

  • The encrypted signature value will be placed in the request header under the parameter “X-EEO-SIGN,” with the encryption rule: sign = md5('request_string&key=secret_key').
  • The parameter of sid and timeStamp will be included in the header, instead of the body, corresponding to the parameters “X-EEO-UID” and “X-EEO-TS.” The body requests should no longer include sid and timeStamp.
  • The “X-EEO-TS” must be a Unix Epoch timestamp of the current API call within the last 5 minutes, at the second level!
  • The body parameters must not use “key” as a parameter name!

Header Example

{
    "X-EEO-SIGN": "5be8ad0ab3740080b8ef240d4e7d6ce4",  // Encrypted parameter sign
    "X-EEO-UID": "1540438",                       // School SID
    "X-EEO-TS": "1726125243",                     // Unix Epoch timestamp within 5 minutes of the current API call
    "Content-Type": "application/json"
}

How to Generate the Signature

Step 1: Read Parameters from the Request Body

  • The body is in JSON format. Extract the key/value pairs for signing based on the following criteria:

    1. Parameters of array and dictionary types do not participate in the signature and should be excluded.
    2. Parameters with a byte length greater than 1024 in the request body are also excluded from the signature.

Step 2: Add Required Parameters, Sort, and Concatenate the String

  1. Add sid=XXXXXX and timeStamp=XXXXXXXX to the parameters to be signed.
  2. Sort the parameters in ascending order based on their ASCII values (lexicographically), and concatenate them into a string using the URL key-value pair format (i.e., key1=value1&key2=value2…).

Step 3: Concatenate the Secret

Append &key=secret to the end of the string generated in the previous step.

Step 4: Calculate the 32-bit Lowercase MD5 Value

The MD5 hash of the entire string (in 32-bit lowercase) is used as the signature value, referred to as sign.

Step 5: Generate the Header for the API Request

{
    "X-EEO-SIGN": "sign",
    "X-EEO-UID": "sid",
    "X-EEO-TS": "timeStamp", 
    "Content-Type": "application/json"
}

Example

Here is a sample of the API request, with the secret: Mb7SR6H

curl --location --request POST '/lms/unit/test' \
--header 'X-EEO-SIGN: 4f97f55addf4921a05c2395617cd8a7b' \
--header 'X-EEO-UID: 1000082' \
--header 'X-EEO-TS: 1721095405' \
--header 'Content-Type: application/json' \
--data-raw '{
    "courseId": 132323,
    "unitJson": [
        {
            "name": "string",
            "content": "string",
            "publishFlag": 0
        }
    ],
}'
  1. Concatenate the Request String

    Exclude parameters that do not participate in the signature: unitJson (array). Then, add sid and timeStamp, resulting in the following list of parameters and values:

    | Parameter Name | Parameter Value | |—————-|—————–| | sid | 1000082 | | courseId | 132323 | | timeStamp | 1721095405 |

    Sort the obtained parameters in ascending order based on their ASCII values (lexicographically) and concatenate them into a string using the URL key-value pair format (i.e., key1=value1&key2=value2…), resulting in stringA.

    Finally, append the key (secret) to stringA to obtain the string to be signed.

    Following the above rules, the resulting string to be signed from the example is as follows:

     courseId=132323&sid=1000082&timeStamp=1673949471&key=Mb7SR6H
    
  2. Calculate the Signature

    Perform an MD5 hash on the string to be signed to obtain the signature value.

    The computed result for this example is 4f97f55addf4921a05c2395617cd8a7b.

Code Example

Signature Algorithm Code Example

Signature Failure

The error codes related to signature failure are as follows:

Error CodeDescription
101002005Signature exception (signature not provided or incorrect)
101002006Timestamp expired (must be within 5 minutes of the current time)
101002008Timestamp does not exist
121601030Incomplete or incorrect parameters