Class DingTalkCallbackCrypto

java.lang.Object
io.agentscope.extensions.channel.dingtalk.DingTalkCallbackCrypto

public final class DingTalkCallbackCrypto extends Object
Implements DingTalk's HTTP-callback request verification and optional body decryption for robot message reception. See the DingTalk docs on verifying robot requests and callback body encryption.

Inputs:

  • appSecret — the enterprise-internal app secret. Used as the HMAC key for request signature verification: each callback carries timestamp and sign headers, where sign = Base64(HmacSHA256(appSecret, timestamp + "\n" + appSecret)) and the timestamp is a millisecond epoch DingTalk requires to be no older than one hour; timestamps ahead of local time are tolerated only within a clock-skew allowance.
  • aesKey — optional 43-character base64 (no padding). When the robot is configured with encryption, the callback body is a {"encrypt": ...} envelope instead of plain JSON, and this key (decoded with an appended "=" to 32 bytes, its first 16 bytes also used as the CBC IV) decrypts it. May be null for robots configured without encryption.

Instances are immutable and thread-safe; verify(java.lang.String, java.lang.String) and decrypt(java.lang.String) may be invoked concurrently from multiple request threads.

  • Constructor Summary

    Constructors
    Constructor
    Description
    DingTalkCallbackCrypto(String appSecret, String aesKey)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    decrypt(String encryptBase64)
    Decrypts the encrypt body field and returns the inner JSON payload as a UTF-8 string.
    boolean
    Returns whether this channel is configured with an aesKey (encrypted callbacks).
    boolean
    verify(String timestamp, String sign)
    Verifies the timestamp/sign request headers of a callback: the timestamp must be a millisecond epoch no older than MAX_TIMESTAMP_AGE_MS and no further ahead of local time than MAX_FUTURE_TIMESTAMP_MS, and sign must equal Base64(HmacSHA256(appSecret, timestamp + "\n" + appSecret)).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DingTalkCallbackCrypto

      public DingTalkCallbackCrypto(String appSecret, String aesKey)
  • Method Details

    • verify

      public boolean verify(String timestamp, String sign)
      Verifies the timestamp/sign request headers of a callback: the timestamp must be a millisecond epoch no older than MAX_TIMESTAMP_AGE_MS and no further ahead of local time than MAX_FUTURE_TIMESTAMP_MS, and sign must equal Base64(HmacSHA256(appSecret, timestamp + "\n" + appSecret)).
    • decrypt

      public String decrypt(String encryptBase64)
      Decrypts the encrypt body field and returns the inner JSON payload as a UTF-8 string.

      DingTalk plaintext layout after AES-CBC decrypt + PKCS#7 unpad:

       | 16 bytes random | 4 bytes msg_len (big-endian) | msg (msg_len bytes) | optional trailing bytes |
       

      Trailing bytes beyond msg_len (present in DingTalk's event-callback variant of this envelope, which appends a receive id) are not part of the message and are ignored.

    • hasAesKey

      public boolean hasAesKey()
      Returns whether this channel is configured with an aesKey (encrypted callbacks).