Skip to content

Commit a333ac7

Browse files
authoredMar 20, 2024··
Merge pull request #12166 from shnrndk/tokenissue4
Fixing token not Reseting when 401 response comes from a Backend
2 parents 47b87e5 + 1163f5d commit a333ac7

File tree

1 file changed

+12
-1
lines changed
  • components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/oauth

1 file changed

+12
-1
lines changed
 

‎components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/oauth/OAuthResponseMediator.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import org.wso2.carbon.apimgt.gateway.handlers.Utils;
3232
import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityConstants;
3333
import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException;
34+
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
35+
import org.wso2.carbon.apimgt.gateway.mediators.oauth.client.TokenResponse;
3436
import org.wso2.carbon.apimgt.gateway.mediators.oauth.conf.OAuthEndpoint;
37+
import org.wso2.carbon.apimgt.gateway.utils.redis.RedisCacheUtils;
3538

3639
/**
3740
* OAuthResponseMediator to handle error responses from OAuth 2.0 protected backends
@@ -59,7 +62,15 @@ public boolean mediate(MessageContext messageContext) {
5962
Object oauthEndpointObject = messageContext.getProperty(APIMgtGatewayConstants.OAUTH_ENDPOINT_INSTANCE);
6063
if (oauthEndpointObject instanceof OAuthEndpoint) {
6164
try {
62-
OAuthTokenGenerator.generateToken((OAuthEndpoint) oauthEndpointObject, null);
65+
OAuthEndpoint oAuthEndpoint = (OAuthEndpoint) oauthEndpointObject;
66+
if (ServiceReferenceHolder.getInstance().isRedisEnabled()) {
67+
new RedisCacheUtils(ServiceReferenceHolder.getInstance().getRedisPool())
68+
.deleteKey(oAuthEndpoint.getId());
69+
} else {
70+
TokenCache.getInstance().getTokenMap().put(oAuthEndpoint.getId(), null);
71+
}
72+
73+
OAuthTokenGenerator.generateToken(oAuthEndpoint, null);
6374
log.error("OAuth 2.0 access token has been rejected by the backend...");
6475
handleFailure(APISecurityConstants.OAUTH_TEMPORARY_SERVER_ERROR, messageContext,
6576
APISecurityConstants.OAUTH_TEMPORARY_SERVER_ERROR_MESSAGE, "Please try again");

0 commit comments

Comments
 (0)
Please sign in to comment.