|
33 | 33 | import org.wso2.carbon.apimgt.impl.utils.APIUtil;
|
34 | 34 | import org.wso2.carbon.apimgt.keymgt.APIKeyMgtException;
|
35 | 35 | import org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder;
|
| 36 | +import org.wso2.carbon.apimgt.keymgt.internal.ServiceReferenceHolder; |
36 | 37 | import org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore;
|
37 | 38 | import org.wso2.carbon.apimgt.keymgt.model.entity.API;
|
38 | 39 | import org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext;
|
@@ -212,14 +213,53 @@ public boolean validateScopes(TokenValidationContext validationContext) throws A
|
212 | 213 | return scopesValidated;
|
213 | 214 | }
|
214 | 215 |
|
| 216 | + private boolean isAccessTokenExpired(long validityPeriod, long issuedTime) { |
| 217 | + |
| 218 | + long timestampSkew = |
| 219 | + ServiceReferenceHolder.getInstance().getOauthServerConfiguration().getTimeStampSkewInSeconds() * 1000; |
| 220 | + long currentTime = System.currentTimeMillis(); |
| 221 | + |
| 222 | + //If the validity period is not an never expiring value |
| 223 | + if (validityPeriod != Long.MAX_VALUE && |
| 224 | + // For cases where validityPeriod is closer to Long.MAX_VALUE (then issuedTime + validityPeriod would spill |
| 225 | + // over and would produce a negative value) |
| 226 | + (currentTime - timestampSkew) > validityPeriod) { |
| 227 | + //check the validity of cached OAuth2AccessToken Response |
| 228 | + |
| 229 | + if ((currentTime - timestampSkew) > (issuedTime + validityPeriod)) { |
| 230 | + return true; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + return false; |
| 235 | + } |
| 236 | + |
| 237 | + |
215 | 238 | private AccessTokenInfo getAccessTokenInfo(TokenValidationContext validationContext)
|
216 | 239 | throws APIManagementException {
|
217 | 240 |
|
218 | 241 | Object cachedAccessTokenInfo =
|
219 | 242 | CacheProvider.createIntrospectionCache().get(validationContext.getAccessToken());
|
220 |
| - if (cachedAccessTokenInfo != null) { |
221 |
| - log.debug("AccessToken available in introspection Cache."); |
222 |
| - return (AccessTokenInfo) cachedAccessTokenInfo; |
| 243 | + AccessTokenInfo cachedAccessTokenInfoObject = null; |
| 244 | + |
| 245 | + if (cachedAccessTokenInfo instanceof AccessTokenInfo) { |
| 246 | + cachedAccessTokenInfoObject = (AccessTokenInfo) cachedAccessTokenInfo; |
| 247 | + |
| 248 | + // Since validationInfoDTO object is not passed into isAccessTokenExpired(), |
| 249 | + // validation status need to be set explicitly. |
| 250 | + if (isAccessTokenExpired(cachedAccessTokenInfoObject.getValidityPeriod(), |
| 251 | + cachedAccessTokenInfoObject.getIssuedTime())) { |
| 252 | + |
| 253 | + if (log.isDebugEnabled()) { |
| 254 | + log.debug("Invalid OAuth Token in Introspect Cache : Access Token " + |
| 255 | + APIUtil.getMaskedToken(validationContext.getAccessToken()) + " has been expired."); |
| 256 | + } |
| 257 | + // if token is expired remove cache entry from introspection cache |
| 258 | + CacheProvider.getGatewayIntrospectCache().remove(validationContext.getAccessToken()); |
| 259 | + cachedAccessTokenInfoObject.setErrorcode(APIConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS); |
| 260 | + cachedAccessTokenInfoObject.setTokenValid(false); |
| 261 | + } |
| 262 | + return cachedAccessTokenInfoObject; |
223 | 263 | }
|
224 | 264 | String electedKeyManager = null;
|
225 | 265 | // Obtaining details about the token.
|
|
0 commit comments