2020import com .databricks .sdk .core .utils .SerDeUtils ;
2121import com .fasterxml .jackson .databind .ObjectMapper ;
2222import com .google .common .annotations .VisibleForTesting ;
23+ import org .slf4j .Logger ;
24+ import org .slf4j .LoggerFactory ;
2325
2426/**
2527 * TokenCache stores OAuth tokens on disk to avoid repeated authentication.
2628 * It generates a unique cache filename based on the host, client ID, and scopes.
2729 * If a passphrase is provided, the token data is encrypted for added security.
30+ * Cache operations can be disabled by setting isEnabled to false.
2831 */
2932public class TokenCache {
33+ private static final Logger LOGGER = LoggerFactory .getLogger (TokenCache .class );
34+
3035 // Base path for token cache files, aligned with Python implementation
3136 private static final String BASE_PATH = ".config/databricks-sdk-java/oauth" ;
3237
@@ -43,6 +48,7 @@ public class TokenCache {
4348 private final Path cacheFile ;
4449 private final String passphrase ;
4550 private final ObjectMapper mapper ;
51+ private final boolean isEnabled ;
4652
4753 /**
4854 * Constructs a new TokenCache instance for OAuth token caching
@@ -51,17 +57,20 @@ public class TokenCache {
5157 * @param clientId The OAuth client ID
5258 * @param scopes The OAuth scopes requested (optional)
5359 * @param passphrase The passphrase used to encrypt/decrypt the token cache (optional)
60+ * @param isEnabled Whether token caching is enabled
5461 */
5562 public TokenCache (
5663 String host ,
5764 String clientId ,
5865 List <String > scopes ,
59- String passphrase ) {
66+ String passphrase ,
67+ boolean isEnabled ) {
6068 this .host = Objects .requireNonNull (host , "host must be defined" );
6169 this .clientId = Objects .requireNonNull (clientId , "clientId must be defined" );
6270 this .scopes = scopes != null ? scopes : new ArrayList <>();
6371 this .passphrase = passphrase ; // Can be null or empty, encryption will be skipped in that case
6472 this .mapper = SerDeUtils .createMapper ();
73+ this .isEnabled = isEnabled ;
6574
6675 this .cacheFile = getFilename ();
6776 }
@@ -113,11 +122,17 @@ private boolean shouldUseEncryption() {
113122
114123 /**
115124 * Saves a Token to the cache file, encrypting if a passphrase is provided
125+ * Does nothing if caching is disabled
116126 *
117127 * @param token The Token to save
118128 * @throws IOException If an error occurs writing to the file
119129 */
120130 public void save (Token token ) throws IOException {
131+ if (!isEnabled ) {
132+ LOGGER .debug ("Token caching is disabled, skipping save operation" );
133+ return ;
134+ }
135+
121136 try {
122137 Files .createDirectories (cacheFile .getParent ());
123138
@@ -138,19 +153,28 @@ public void save(Token token) throws IOException {
138153 cacheFile .toFile ().setReadable (true , true );
139154 cacheFile .toFile ().setWritable (false , false );
140155 cacheFile .toFile ().setWritable (true , true );
156+
157+ LOGGER .debug ("Successfully saved token to cache: {}" , cacheFile );
141158 } catch (Exception e ) {
142159 throw new IOException ("Failed to save token cache: " + e .getMessage (), e );
143160 }
144161 }
145162
146163 /**
147164 * Loads a Token from the cache file, decrypting if a passphrase was provided
165+ * Returns null if caching is disabled
148166 *
149- * @return The Token from the cache or null if the cache file doesn't exist or is invalid
167+ * @return The Token from the cache or null if the cache file doesn't exist, is invalid, or caching is disabled
150168 */
151169 public Token load () {
170+ if (!isEnabled ) {
171+ LOGGER .debug ("Token caching is disabled, skipping load operation" );
172+ return null ;
173+ }
174+
152175 try {
153176 if (!Files .exists (cacheFile )) {
177+ LOGGER .debug ("No token cache file found at: {}" , cacheFile );
154178 return null ;
155179 }
156180
@@ -163,6 +187,7 @@ public Token load() {
163187 } catch (Exception e ) {
164188 // If decryption fails, it might be because the file was saved without encryption
165189 // or the passphrase is incorrect
190+ LOGGER .debug ("Failed to decrypt token cache: {}" , e .getMessage ());
166191 return null ;
167192 }
168193 } else {
@@ -171,10 +196,13 @@ public Token load() {
171196
172197 // Deserialize token from JSON
173198 String json = new String (decodedContent , StandardCharsets .UTF_8 );
174- return mapper .readValue (json , Token .class );
199+ Token token = mapper .readValue (json , Token .class );
200+ LOGGER .debug ("Successfully loaded token from cache: {}" , cacheFile );
201+ return token ;
175202 } catch (Exception e ) {
176203 // If there's any issue loading the token, return null
177204 // to allow a fresh token to be obtained
205+ LOGGER .debug ("Failed to load token from cache: {}" , e .getMessage ());
178206 return null ;
179207 }
180208 }
0 commit comments