From 6f32fd961a857b016d0acbfdf3a50f984e9e1f15 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Fri, 25 Oct 2019 11:53:39 +0200 Subject: [PATCH] Feature #17862, oAuth2 Service resilient to memcached server restarts --- .../portal/oauth/DistributedCacheClient.java | 20 +++---------------- .../org/gcube/portal/oauth/OauthService.java | 18 ++++------------- 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/gcube/portal/oauth/DistributedCacheClient.java b/src/main/java/org/gcube/portal/oauth/DistributedCacheClient.java index 138ae9d..3d4dc7e 100644 --- a/src/main/java/org/gcube/portal/oauth/DistributedCacheClient.java +++ b/src/main/java/org/gcube/portal/oauth/DistributedCacheClient.java @@ -18,6 +18,7 @@ import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.context.application.ApplicationContext; import org.slf4j.LoggerFactory; +import net.spy.memcached.KetamaConnectionFactory; import net.spy.memcached.MemcachedClient; /** @@ -33,31 +34,16 @@ public class DistributedCacheClient { private MemcachedClient mClient; - /** - * Singleton object - */ - private static DistributedCacheClient singleton = new DistributedCacheClient(); - /** * Build the singleton instance */ - private DistributedCacheClient(){ + public DistributedCacheClient(){ List addrs = discoverHostOfServiceEndpoint(); try { - mClient = new MemcachedClient(addrs); + mClient = new MemcachedClient(new KetamaConnectionFactory(), addrs); } catch (IOException e) { e.printStackTrace(); } - } - - /** - * Retrieve the singleton instance - */ - public static DistributedCacheClient getInstance(){ - if (singleton == null) { - singleton = new DistributedCacheClient(); - } - return singleton; } public MemcachedClient getMemcachedClient() { diff --git a/src/main/java/org/gcube/portal/oauth/OauthService.java b/src/main/java/org/gcube/portal/oauth/OauthService.java index 50e4692..fa0fc20 100644 --- a/src/main/java/org/gcube/portal/oauth/OauthService.java +++ b/src/main/java/org/gcube/portal/oauth/OauthService.java @@ -40,25 +40,12 @@ public class OauthService { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(OauthService.class); - /** - * This map contains couples - */ - private MemcachedClient entries; - - - /** * Since this is a singleton sub-service, there will be just one call to this constructor and one running thread * to clean up expired codes. */ public OauthService() { - logger.info("Singleton gcube-oauth service built."); - entries = DistributedCacheClient.getInstance().getMemcachedClient(); - } - - @Override - protected void finalize(){ - entries.shutdown(); + logger.info("Singleton gcube-oauth service built."); } /** @@ -174,6 +161,9 @@ public class OauthService { return new MemCachedBean("invalid_request"); if(!checkIsapplicationTokenType(authorizationService().get(credentials.getClientSecret()).getClientInfo().getType())) // it is not an app token or it is not a token return new MemCachedBean("invalid_client"); + + MemcachedClient entries = new DistributedCacheClient().getMemcachedClient(); + if(entries.get(code) == null) return new MemCachedBean("invalid_grant"); logger.debug("Got tempCode and looking into memcached for correspondance, "+code);