Skip to content

Commit a259841

Browse files
author
Stephen Moore
committed
Added getters and setters to IntrospectingTokenService
Fixed TokenCacheObject constructor for setting TCO's expire time
1 parent 698fe55 commit a259841

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

openid-connect-client/src/main/java/org/mitre/oauth2/introspectingfilter/IntrospectingTokenService.java

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ private TokenCacheObject(OAuth2AccessToken token, OAuth2Authentication auth) {
8888
this.token = token;
8989
this.auth = auth;
9090

91-
92-
// if the token doesn't have an expire time, use the default expire time
93-
// also use the default expire time if the token is valid for longer than that time (i.e. force a check of the token, if force check is valid)
94-
if (this.token.getExpiration() != null || (forceCacheExpireTime && (this.token.getExpiration().getTime() - System.currentTimeMillis() <= defaultExpireTime))) {
91+
// we don't need to check the cacheTokens values, because this won't actually be added to the cache if cacheTokens is false
92+
// if the token isn't null we use the token expire time
93+
// if forceCacheExpireTime is also true, we also make sure that the token expire time is shorter than the default expire time
94+
if ((this.token.getExpiration() != null) && (!forceCacheExpireTime || (forceCacheExpireTime && (this.token.getExpiration().getTime() - System.currentTimeMillis() <= defaultExpireTime)))) {
9595
this.cacheExpire = this.token.getExpiration();
96-
} else {
96+
} else { // if the token doesn't have an expire time, or if the using forceCacheExpireTime the token expire time is longer than the default, then use the default expire time
9797
Calendar cal = Calendar.getInstance();
9898
cal.add(Calendar.MILLISECOND, defaultExpireTime);
9999
this.cacheExpire = cal.getTime();
@@ -151,8 +151,63 @@ public void setDefaultExpireTime(int defaultExpireTime) {
151151
this.defaultExpireTime = defaultExpireTime;
152152
}
153153

154-
// Check if there is a token and authentication in the cache
155-
// and check if it is not expired.
154+
/**
155+
* check if forcing a cache expire time maximum value
156+
* @return the forceCacheExpireTime setting
157+
*/
158+
public boolean isForceCacheExpireTime() {
159+
return forceCacheExpireTime;
160+
}
161+
162+
/**
163+
* set forcing a cache expire time maximum value
164+
* @param forceCacheExpireTime
165+
*/
166+
public void setForceCacheExpireTime(boolean forceCacheExpireTime) {
167+
this.forceCacheExpireTime = forceCacheExpireTime;
168+
}
169+
170+
/**
171+
* Are non-expiring tokens cached using the default cache time
172+
* @return state of cacheNonExpiringTokens
173+
*/
174+
public boolean isCacheNonExpiringTokens() {
175+
return cacheNonExpiringTokens;
176+
}
177+
178+
/**
179+
* should non-expiring tokens be cached using the default cache timeout
180+
* @param cacheNonExpiringTokens
181+
*/
182+
public void setCacheNonExpiringTokens(boolean cacheNonExpiringTokens) {
183+
this.cacheNonExpiringTokens = cacheNonExpiringTokens;
184+
}
185+
186+
/**
187+
* Is the service caching tokens, or is it hitting the introspection end point every time
188+
* @return true is caching tokens locally, false hits the introspection end point every time
189+
*/
190+
public boolean isCacheTokens() {
191+
return cacheTokens;
192+
}
193+
194+
/**
195+
* Configure if the client should cache tokens locally or not
196+
* @param cacheTokens
197+
*/
198+
public void setCacheTokens(boolean cacheTokens) {
199+
this.cacheTokens = cacheTokens;
200+
}
201+
202+
/**
203+
* Check to see if the introspection end point response for a token has been cached locally
204+
* This call will return the token if it has been cached and is still valid according to
205+
* the cache expire time on the TokenCacheObject. If a cached value has been found but is
206+
* expired, either by default expire times or the token's own expire time, then the token is
207+
* removed from the cache and null is returned.
208+
* @param key is the token to check
209+
* @return the cached TokenCacheObject or null
210+
*/
156211
private TokenCacheObject checkCache(String key) {
157212
if (cacheTokens && authCache.containsKey(key)) {
158213
TokenCacheObject tco = authCache.get(key);
@@ -189,9 +244,13 @@ private OAuth2AccessToken createAccessToken(final JsonObject token, final String
189244
return accessToken;
190245
}
191246

192-
// Validate a token string against the introspection endpoint,
193-
// then parse it and store it in the local cache. Return TokenCacheObject
194-
// if token is valid, otherwise return null
247+
/**
248+
* Validate a token string against the introspection endpoint,
249+
* then parse it and store it in the local cache if caching is enabled.
250+
*
251+
* @param accessToken Token to pass to the introspection endpoint
252+
* @return TokenCacheObject containing authentication and token if the token was valid, otherwise null
253+
*/
195254
private TokenCacheObject parseToken(String accessToken) {
196255

197256
// find out which URL to ask
@@ -275,7 +334,7 @@ protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOE
275334
}
276335
}
277336

278-
// If we never put a token and an authentication in the cache...
337+
// when the token is invalid for whatever reason
279338
return null;
280339
}
281340

0 commit comments

Comments
 (0)