Class BitwardenSessionManager

java.lang.Object
com.mwdle.bitwarden.cli.BitwardenSessionManager

public class BitwardenSessionManager extends Object
A thread-safe singleton that manages and caches a single, global Bitwarden session token.

This class ensures that the slow, network-intensive login and unlock operations required for Bitwarden interactions are performed infrequently. It uses a high-performance, double-checked locking pattern to provide concurrent access to the session token.

  • Method Details

    • getInstance

      public static BitwardenSessionManager getInstance()
      Provides global access to the single instance of this manager.
      Returns:
      The singleton instance of BitwardenSessionManager.
    • getSessionToken

      public Secret getSessionToken() throws IOException, InterruptedException
      Provides thread-safe, high-performance access to a valid Bitwarden session token.

      This method uses a double-checked locking pattern. The "fast path" checks the validity of the token without acquiring a lock, handling the vast majority of calls. Only if the token is invalid will it enter a synchronized block to perform the expensive re-authentication.

      Returns:
      A valid session token.
      Throws:
      IOException - If the login/unlock process fails.
      InterruptedException - If the CLI command is interrupted.
    • isSessionValid

      public boolean isSessionValid()
      Performs a check to see if the cached session token is still valid by calling bw status.
      Returns:
      true if the token is present and the vault status is unlocked.
    • invalidateSessionToken

      public void invalidateSessionToken()
      Invalidates the current session token.

      The next call to getSessionToken() will be forced to perform a full re-authentication. This is typically called after the plugin's global configuration has been changed.