Skip to content

Commit f5e4f8a

Browse files
committed
Add Javadoc to Token.java
1 parent 5a37c59 commit f5e4f8a

File tree

2 files changed

+57
-0
lines changed
  • databricks-sdk-java/src

2 files changed

+57
-0
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Token.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public Token(
6666
this.clockSupplier = clockSupplier;
6767
}
6868

69+
/**
70+
* Checks if the token is expired. Tokens are considered expired 40 seconds before their actual
71+
* expiry time to account for Azure Databricks rejecting tokens that expire in 30 seconds or less.
72+
*
73+
* @return true if the token is expired or about to expire, false otherwise
74+
*/
6975
public boolean isExpired() {
7076
if (expiry == null) {
7177
return false;
@@ -77,26 +83,57 @@ public boolean isExpired() {
7783
return potentiallyExpired.isBefore(now);
7884
}
7985

86+
/**
87+
* Checks if the token is valid. A token is valid if it has a non-null access token and is not
88+
* expired.
89+
*
90+
* @return true if the token is valid, false otherwise
91+
*/
8092
public boolean isValid() {
8193
return accessToken != null && !isExpired();
8294
}
8395

96+
/**
97+
* Returns the type of the token (e.g., "Bearer").
98+
*
99+
* @return the token type
100+
*/
84101
public String getTokenType() {
85102
return tokenType;
86103
}
87104

105+
/**
106+
* Returns the refresh token, if available. May be null for non-refreshable tokens.
107+
*
108+
* @return the refresh token or null
109+
*/
88110
public String getRefreshToken() {
89111
return refreshToken;
90112
}
91113

114+
/**
115+
* Returns the access token string.
116+
*
117+
* @return the access token
118+
*/
92119
public String getAccessToken() {
93120
return accessToken;
94121
}
95122

123+
/**
124+
* Returns the expiry time of the token as a LocalDateTime.
125+
*
126+
* @return the expiry time
127+
*/
96128
public LocalDateTime getExpiry() {
97129
return this.expiry;
98130
}
99131

132+
/**
133+
* Returns the remaining lifetime of the token as a Duration.
134+
*
135+
* @return the duration between now and the token's expiry
136+
*/
100137
public Duration getLifetime() {
101138
return Duration.between(LocalDateTime.now(clockSupplier.getClock()), this.expiry);
102139
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,24 @@ void expiredToken() {
7171
assertTrue(token.isExpired());
7272
assertFalse(token.isValid());
7373
}
74+
75+
@Test
76+
void tokenLifetimeInFuture() {
77+
Token token =
78+
new Token(accessToken, tokenType, currentLocalDateTime.plusMinutes(10), fakeClockSupplier);
79+
assertEquals(java.time.Duration.ofMinutes(10), token.getLifetime());
80+
}
81+
82+
@Test
83+
void tokenLifetimeExpired() {
84+
Token token =
85+
new Token(accessToken, tokenType, currentLocalDateTime.minusMinutes(2), fakeClockSupplier);
86+
assertEquals(java.time.Duration.ofMinutes(-2), token.getLifetime());
87+
}
88+
89+
@Test
90+
void tokenLifetimeZero() {
91+
Token token = new Token(accessToken, tokenType, currentLocalDateTime, fakeClockSupplier);
92+
assertEquals(java.time.Duration.ZERO, token.getLifetime());
93+
}
7494
}

0 commit comments

Comments
 (0)