Added an admin API for clearing access token cache.
Change-Id: I3ec2d47294229c3ccdfaa0701bec16eae54dada4
diff --git a/full/Changes b/full/Changes
index 816abfd..cda6540 100644
--- a/full/Changes
+++ b/full/Changes
@@ -1,6 +1,8 @@
# version 0.62.4
24/01/2020
- - Removed salt from config and updated config files.
+ - Removed salt from config and updated config files (margaretha)
+03/02/2020
+ - Added an admin API for clearing access token cache (margaretha)
# version 0.62.3
03/12/2019
diff --git a/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java b/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java
index 47cc265..dcbcafe 100644
--- a/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java
@@ -9,6 +9,7 @@
import java.util.Set;
import javax.persistence.NoResultException;
+import javax.servlet.ServletContext;
import javax.ws.rs.core.Response.Status;
import org.apache.oltu.oauth2.as.request.AbstractOAuthTokenRequest;
@@ -509,5 +510,23 @@
return dtoList;
}
-
+ public String clearAccessTokenCache (String adminToken, String accessToken,
+ ServletContext context) throws KustvaktException {
+ if (adminToken != null && !adminToken.isEmpty()
+ && adminToken.equals(context.getInitParameter("adminToken"))) {
+
+ if (accessToken == null) {
+ tokenDao.clearCache();
+ return "Cache has been cleared";
+ }
+ else {
+ tokenDao.removeCacheEntry(accessToken);
+ return "Access token has been removed from the cache";
+ }
+ }
+ else {
+ throw new KustvaktException(StatusCodes.INCORRECT_ADMIN_TOKEN,
+ "Admin token is incorrect");
+ }
+ }
}
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuth2Controller.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuth2Controller.java
index 6ba63fb..68f991c 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuth2Controller.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuth2Controller.java
@@ -3,6 +3,7 @@
import java.time.ZonedDateTime;
import java.util.List;
+import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
@@ -357,4 +358,21 @@
}
}
+
+ @POST
+ @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
+ @Path("token/clear")
+ public Response clearAccessTokenCache (
+ @FormParam("token") String adminToken,
+ @FormParam("access_token") String accessToken,
+ @Context ServletContext context) {
+ try {
+ String response = tokenService.clearAccessTokenCache(adminToken, accessToken,
+ context);
+ return Response.ok(response).build();
+ }
+ catch (KustvaktException e) {
+ throw responseHandler.throwit(e);
+ }
+ }
}