Record Class GmailCredentials

java.lang.Object
java.lang.Record
uno.anahata.asi.yam.tools.gmail.GmailCredentials
Record Components:
clientId - The Google Cloud OAuth 2.0 Client ID.
clientSecret - The Google Cloud OAuth 2.0 Client Secret.
refreshToken - The long-lived refresh token obtained after browser authorization.
emailAddress - The primary Gmail address authorized by this session.

public record GmailCredentials(String clientId, String clientSecret, String refreshToken, String emailAddress) extends Record
Encapsulates Google OAuth 2.0 credentials and metadata for the Gmail API.

Manages loading and persisting client ID, client secret, long-lived refresh token, and the authorized user email address in ~/.anahata/asi/gmail/credentials.json. Also supports importing client credentials from official Google Cloud Console secrets JSON.

Author:
anahata
  • Field Details

    • clientId

      private final String clientId
      The field for the clientId record component.
    • clientSecret

      private final String clientSecret
      The field for the clientSecret record component.
    • refreshToken

      private final String refreshToken
      The field for the refreshToken record component.
    • emailAddress

      private final String emailAddress
      The field for the emailAddress record component.
    • MAPPER

      private static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
      Shared JSON object mapper configured for formatted indentation and lenient property parsing.
  • Constructor Details

    • GmailCredentials

      public GmailCredentials(String clientId, String clientSecret, String refreshToken, String emailAddress)
      Creates an instance of a GmailCredentials record class.
      Parameters:
      clientId - the value for the clientId record component
      clientSecret - the value for the clientSecret record component
      refreshToken - the value for the refreshToken record component
      emailAddress - the value for the emailAddress record component
  • Method Details

    • getCredentialsPath

      public static Path getCredentialsPath()
      Resolves the canonical credentials file path: ~/.anahata/asi/gmail/credentials.json.
      Returns:
      The canonical Path to the Gmail credentials file.
    • resolveExistingCredentialsPath

      public static Path resolveExistingCredentialsPath()
      Resolves the active credentials path, checking canonical and fallback locations.
      Returns:
      The Path to an existing credentials file, or canonical path if none exist.
    • exists

      public static boolean exists()
      Checks if a credentials file exists in either canonical or fallback paths.
      Returns:
      true if credentials exist on disk.
    • load

      public static GmailCredentials load() throws IOException
      Loads the stored Gmail credentials from disk.
      Returns:
      The loaded GmailCredentials.
      Throws:
      IOException - If the credentials file cannot be found or read.
    • fromClientSecrets

      public static GmailCredentials fromClientSecrets(Path clientSecretsPath) throws IOException
      Parses client ID and client secret from a Google Cloud Console credentials JSON file.

      Supports standard Google formats including installed (Desktop app), web (Web app), or flat JSON representations.

      Parameters:
      clientSecretsPath - The path to the client secrets JSON file.
      Returns:
      A GmailCredentials instance containing parsed client secrets.
      Throws:
      IOException - If file reading or JSON parsing fails.
    • save

      public void save() throws IOException
      Persists these credentials to the canonical storage location: ~/.anahata/asi/gmail/credentials.json.
      Throws:
      IOException - If writing to disk fails.
    • hasClientSecrets

      public boolean hasClientSecrets()
      Checks if client ID and client secret are configured.
      Returns:
      true if OAuth client details are present.
    • isAuthenticated

      public boolean isAuthenticated()
      Checks if a refresh token is stored and ready for autonomous API access.
      Returns:
      true if authenticated with a refresh token.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • clientId

      public String clientId()
      Returns the value of the clientId record component.
      Returns:
      the value of the clientId record component
    • clientSecret

      public String clientSecret()
      Returns the value of the clientSecret record component.
      Returns:
      the value of the clientSecret record component
    • refreshToken

      public String refreshToken()
      Returns the value of the refreshToken record component.
      Returns:
      the value of the refreshToken record component
    • emailAddress

      public String emailAddress()
      Returns the value of the emailAddress record component.
      Returns:
      the value of the emailAddress record component