Skip to content

Commit 34577e1

Browse files
committed
feat(core): Add check for expired refresh token
1 parent 3341a8d commit 34577e1

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

core/src/main/java/com/ibm/watson/developer_cloud/service/security/IamTokenManager.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public IamTokenManager(IamOptions options) {
4545
/**
4646
* This function returns an access token. The source of the token is determined by the following logic:
4747
* 1. If user provides their own managed access token, assume it is valid and send it
48-
* 2. If this class is managing tokens and does not yet have one, make a request for one
48+
* 2. If this class is managing tokens and does not yet have one, or the refresh token is expired, make a request
49+
* for one
4950
* 3. If this class is managing tokens and the token has expired, refresh it
5051
* 4. If this class is managing tokens and has a valid token stored, send it
5152
*
@@ -57,10 +58,10 @@ public String getToken() {
5758
if (userManagedAccessToken != null) {
5859
// use user-managed access token
5960
token = userManagedAccessToken;
60-
} else if (tokenData.getAccessToken() == null) {
61-
// request first-time token
61+
} else if (tokenData.getAccessToken() == null || isRefreshTokenExpired()) {
62+
// request new token
6263
token = requestToken();
63-
} else if (isTokenExpired()) {
64+
} else if (isAccessTokenExpired()) {
6465
// refresh current token
6566
token = refreshToken();
6667
} else {
@@ -129,16 +130,16 @@ public void setAccessToken(String userManagedAccessToken) {
129130
}
130131

131132
/**
132-
* Check if currently stored token is expired.
133+
* Check if currently stored access token is expired.
133134
*
134135
* Using a buffer to prevent the edge case of the
135136
* token expiring before the request could be made.
136137
*
137138
* The buffer will be a fraction of the total TTL. Using 80%.
138139
*
139-
* @return whether the current managed token is expired or not
140+
* @return whether the current managed access token is expired or not
140141
*/
141-
private boolean isTokenExpired() {
142+
private boolean isAccessTokenExpired() {
142143
if (tokenData.getExpiresIn() == null || tokenData.getExpiration() == null) {
143144
return true;
144145
}
@@ -152,6 +153,25 @@ private boolean isTokenExpired() {
152153
return refreshTime < currentTime;
153154
}
154155

156+
/**
157+
* Used as a fail-safe to prevent the condition of a refresh token expiring,
158+
* which could happen after around 30 days. This function will return true
159+
* if it has been at least 7 days and 1 hour since the last token was
160+
* retrieved.
161+
*
162+
* @returns whether the current managed refresh token is expired or not
163+
*/
164+
private boolean isRefreshTokenExpired() {
165+
if (tokenData.getExpiration() != null) {
166+
return true;
167+
}
168+
169+
int sevenDays = 7 * 24 * 3600;
170+
Double currentTime = Math.floor(System.currentTimeMillis() / 1000);
171+
Long newTokenTime = tokenData.getExpiration() + sevenDays;
172+
return newTokenTime < currentTime;
173+
}
174+
155175
/**
156176
* Executes call to IAM API and returns IamToken object representing the response.
157177
*

natural-language-classifier/src/main/java/com/ibm/watson/developer_cloud/natural_language_classifier/v1/NaturalLanguageClassifier.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public NaturalLanguageClassifier(String username, String password) {
7777
}
7878

7979
/**
80-
* Instantiates a new `NaturalLanguageClassifier` with IAM. Note that if the access token is specified in the iamOptions,
81-
* you accept responsibility for managing the access token yourself. You must set a new access token before this one
82-
* expires. Failing to do so will result in authentication errors after this token expires.
80+
* Instantiates a new `NaturalLanguageClassifier` with IAM. Note that if the access token is specified in the
81+
* iamOptions, you accept responsibility for managing the access token yourself. You must set a new access token
82+
* before this one expires. Failing to do so will result in authentication errors after this token expires.
8383
*
8484
* @param iamOptions the options for authenticating through IAM
8585
*/

0 commit comments

Comments
 (0)