From 600e2529bd9072400b7e5f55cce5ecdcd01508d4 Mon Sep 17 00:00:00 2001 From: Megala Date: Fri, 16 Dec 2016 14:10:38 +0530 Subject: [PATCH] Fixing race conditions in refresh token generation --- .../authorization/client/OAuthRequestInterceptor.java | 2 +- .../authorization/client/OAuthRequestInterceptor.java | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/OAuthRequestInterceptor.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/OAuthRequestInterceptor.java index 1458c85ab5..76466b09d1 100755 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/OAuthRequestInterceptor.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/OAuthRequestInterceptor.java @@ -96,7 +96,7 @@ public class OAuthRequestInterceptor implements RequestInterceptor { tokenInfo = tokenIssuerService.getToken(PASSWORD_GRANT_TYPE, username, password); tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in()); } - synchronized(tokenInfo) { + synchronized(this) { if (System.currentTimeMillis() + refreshTimeOffset > tokenInfo.getExpires_in()) { tokenInfo = tokenIssuerService.getToken(REFRESH_GRANT_TYPE, tokenInfo.getRefresh_token()); tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in()); diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/client/OAuthRequestInterceptor.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/client/OAuthRequestInterceptor.java index 9c8832f007..886daa9124 100755 --- a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/client/OAuthRequestInterceptor.java +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/client/OAuthRequestInterceptor.java @@ -79,9 +79,11 @@ public class OAuthRequestInterceptor implements RequestInterceptor { tokenInfo = tokenIssuerService.getToken(PASSWORD_GRANT_TYPE, username, password); tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in()); } - if (System.currentTimeMillis() + refreshTimeOffset > tokenInfo.getExpires_in()) { - tokenInfo = tokenIssuerService.getToken(REFRESH_GRANT_TYPE, tokenInfo.getRefresh_token()); - tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in()); + synchronized (this) { + if (System.currentTimeMillis() + refreshTimeOffset > tokenInfo.getExpires_in()) { + tokenInfo = tokenIssuerService.getToken(REFRESH_GRANT_TYPE, tokenInfo.getRefresh_token()); + tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in()); + } } String headerValue = "Bearer " + tokenInfo.getAccess_token(); template.header("Authorization", headerValue);