Class DocumentServerTokens

java.lang.Object
org.exoplatform.onlyoffice.DocumentServerTokens

public class DocumentServerTokens extends Object
Signs the tokens the addon sends to ONLYOFFICE Docs and validates the tokens the Document Server sends back, with the shared secret. Every token carries its purpose in the root operation claim (DocumentServerOperation): ONLYOFFICE Docs 10.0 refuses a token sent to a service of another purpose, and this class refuses a token received on an endpoint of another purpose. Without a usable secret nothing is signed and every received token is refused.
  • Field Details

    • MIN_SECRET_BYTES

      public static final int MIN_SECRET_BYTES
      The minimum length of the secret, in bytes: HMAC-SHA256 needs 256 bits.
      See Also:
  • Constructor Details

    • DocumentServerTokens

      public DocumentServerTokens(String secret)
      Reports a missing or too short secret as an ERROR, once.
      Parameters:
      secret - the secret shared with the Document Server, may be null
  • Method Details

    • getSecretProblem

      public String getSecretProblem()
      Returns:
      why the secret cannot be used, or null when it can
    • sign

      public String sign(Map<String,?> claims, DocumentServerOperation operation)
      Signs a token for a request to the Document Server.
      Parameters:
      claims - the claims of the request, left unchanged
      operation - the service the token is for
      Returns:
      the signed token
      Throws:
      IllegalStateException - when the secret is missing or too short
    • sign

      public String sign(Map<String,?> claims, DocumentServerOperation operation, Date expiresAt)
      Signs a token that expires, for a URL the addon hands out and validates itself.
      Parameters:
      claims - the claims of the request, left unchanged
      operation - the service the token is for
      expiresAt - when the token expires, none when null
      Returns:
      the signed token
      Throws:
      IllegalStateException - when the secret is missing or too short
    • validate

      public boolean validate(String token, String key, DocumentServerOperation expected)
      Validates a token the Document Server sent to an addon endpoint.
      Parameters:
      token - the token, may be null
      key - the document key the endpoint was called for, may be null
      expected - the purpose of the endpoint, DocumentServerOperation.DOWNLOAD for a file fetch. This checks the token only: a callback, whose body the addon acts on, is validated by validateCallback(String, String, String)
      Returns:
      true when the token is signed with the secret, carries an expiry not yet reached and the expected purpose, and is for that key: for a callback its payload.key equals the key; for a download its payload.key equals the key or its payload.url ends with it
    • validateCallback

      public boolean validateCallback(String token, String key, String body)
      Validates a save or status callback: its token, and the callback the addon acts on. ONLYOFFICE Docs signs the whole callback in the token's payload, so the body must be that payload, its own token field aside. Both sides are read into the same form, where a null value and an absent one are alike, as they are to the addon.
      Parameters:
      token - the token of the Authorization header, may be null
      key - the document key the endpoint was called for, may be null
      body - the callback body, may be null
      Returns:
      true when the token is a valid callback token whose payload.key equals the key and whose payload equals the body