Class FileCredential

java.lang.Object
io.superdurable.dex.FileCredential
All Implemented Interfaces:
Credential

public final class FileCredential extends Object implements Credential
Reads a bearer token from a file and picks up rotations without a restart.

Construction reads the file immediately. Calls check its modification time and size at most once per second and reread changed content. Surrounding whitespace is removed. An empty or unreadable file fails calls until a later check succeeds; no stale token is sent. Concurrent callers share a short lock. Prefer atomic replacement when rotating the file.

Credential credential = new FileCredential("/var/run/secrets/dex/token");
ClientOptions clientOptions = ClientOptions.newBuilder().credential(credential).build();
WorkerOptions workerOptions = WorkerOptions.newBuilder().credential(credential).build();
  • Constructor Details

    • FileCredential

      public FileCredential(String path) throws IOException
      Reads the initial token from a file path.
      Parameters:
      path - the token file path
      Throws:
      IllegalArgumentException - if path is null or invalid
      IOException - if the file cannot be read or contains only whitespace
    • FileCredential

      public FileCredential(Path path) throws IOException
      Reads the initial token from a file.
      Parameters:
      path - the nonnull token file path
      Throws:
      IllegalArgumentException - if path is null
      IOException - if the file cannot be read or contains only whitespace
  • Method Details

    • token

      public String token() throws IOException
      Returns the cached token after any due file check.

      A changed file is reloaded on the next check, at most one second after the previous check. This method is safe for concurrent callers.

      Specified by:
      token in interface Credential
      Returns:
      the current token without surrounding whitespace
      Throws:
      IOException - if the file is unreadable or empty after trimming
    • toString

      public String toString()
      Describes this credential without revealing its token.
      Overrides:
      toString in class Object
      Returns:
      a fixed redacted description