From 8048cd9b6ab393b8a3e4a1aaf36e09abe20f605b Mon Sep 17 00:00:00 2001 From: Arnab Dutta <32794267+duttarnab@users.noreply.github.com> Date: Mon, 4 Jul 2022 12:51:30 +0530 Subject: [PATCH] feat: add support for date ranges in statistic client #1575 (#1653) * feat: add support for date ranges in statistic endpoint #1575 * feat: add support for date ranges in statistic endpoint #1575 * feat: add support for date ranges in statistic endpoint #1575 --- .../jans/as/client/service/StatService.java | 12 ++++++++-- .../io/jans/as/client/ws/rs/StatWSTest.java | 14 +++++++++-- .../rest/resource/auth/StatResource.java | 11 +++++---- .../security/client/AuthClientFactory.java | 23 +++++++++++-------- .../configapi/service/auth/AuthService.java | 6 ++--- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/jans-auth-server/client/src/main/java/io/jans/as/client/service/StatService.java b/jans-auth-server/client/src/main/java/io/jans/as/client/service/StatService.java index 6fd27025a64..c6b7028d96c 100644 --- a/jans-auth-server/client/src/main/java/io/jans/as/client/service/StatService.java +++ b/jans-auth-server/client/src/main/java/io/jans/as/client/service/StatService.java @@ -16,12 +16,20 @@ public interface StatService { @GET @Produces({MediaType.APPLICATION_JSON}) - JsonNode stat(@HeaderParam("Authorization") String authorization, @QueryParam("month") String month, @QueryParam("format") String format); + JsonNode stat(@HeaderParam("Authorization") String authorization, + @QueryParam("month") String month, + @QueryParam("start-month") String startMonth, + @QueryParam("end-month") String endMonth, + @QueryParam("format") String format); @GET String statOpenMetrics(@HeaderParam("Authorization") String authorization, @QueryParam("month") String month, @QueryParam("format") String format); @POST @Produces({MediaType.APPLICATION_JSON}) - JsonNode statPost(@HeaderParam("Authorization") String authorization, @FormParam("month") String month, @FormParam("format") String format); + JsonNode statPost(@HeaderParam("Authorization") String authorization, + @FormParam("month") String month, + @FormParam("start-month") String startMonth, + @FormParam("end-month") String endMonth, + @FormParam("format") String format); } diff --git a/jans-auth-server/client/src/test/java/io/jans/as/client/ws/rs/StatWSTest.java b/jans-auth-server/client/src/test/java/io/jans/as/client/ws/rs/StatWSTest.java index 5a2a6803048..89c5362df18 100644 --- a/jans-auth-server/client/src/test/java/io/jans/as/client/ws/rs/StatWSTest.java +++ b/jans-auth-server/client/src/test/java/io/jans/as/client/ws/rs/StatWSTest.java @@ -24,7 +24,17 @@ public void stat(final String umaPatClientId, final String umaPatClientSecret) t final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret); final StatService service = ClientFactory.instance().createStatService(issuer + STAT_PATH); - final JsonNode node = service.stat("Bearer " + authorization.getAccessToken(), "202101", null); + final JsonNode node = service.stat("Bearer " + authorization.getAccessToken(), "202101", null, null, null); + assertTrue(node != null && node.hasNonNull("response")); + } + + @Test(enabled = false) + @Parameters({"umaPatClientId", "umaPatClientSecret"}) + public void statWithDateRange(final String umaPatClientId, final String umaPatClientSecret) throws Exception { + final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret); + + final StatService service = ClientFactory.instance().createStatService(issuer + STAT_PATH); + final JsonNode node = service.stat("Bearer " + authorization.getAccessToken(), null, "202201", "202204", null); assertTrue(node != null && node.hasNonNull("response")); } @@ -33,7 +43,7 @@ public void stat(final String umaPatClientId, final String umaPatClientSecret) t public void statPost(final String umaPatClientId, final String umaPatClientSecret) throws Exception { final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret); final StatService service = ClientFactory.instance().createStatService(issuer + "/jans-auth/restv1/internal/stat"); - final JsonNode node = service.stat(authorization.getAccessToken(), "202101", null); + final JsonNode node = service.stat(authorization.getAccessToken(), "202101", null, null, null); assertTrue(node != null && node.hasNonNull("response")); } } \ No newline at end of file diff --git a/jans-config-api/server/src/main/java/io/jans/configapi/rest/resource/auth/StatResource.java b/jans-config-api/server/src/main/java/io/jans/configapi/rest/resource/auth/StatResource.java index 540d7d3ce24..e754cebc603 100644 --- a/jans-config-api/server/src/main/java/io/jans/configapi/rest/resource/auth/StatResource.java +++ b/jans-config-api/server/src/main/java/io/jans/configapi/rest/resource/auth/StatResource.java @@ -31,16 +31,19 @@ public class StatResource extends ConfigBaseResource { AuthService authService; @GET - @ProtectedApi(scopes = { ApiAccessConstants.STATS_USER_READ_ACCESS, ApiAccessConstants.JANS_STAT }) + @ProtectedApi(scopes = {ApiAccessConstants.STATS_USER_READ_ACCESS, ApiAccessConstants.JANS_STAT}) @Produces(MediaType.APPLICATION_JSON) public Response getStatistics(@HeaderParam("Authorization") String authorization, - @QueryParam(value = "month") String month, @QueryParam(value = "format") String format) { + @QueryParam(value = "month") String month, + @QueryParam(value = "start-month") String startMonth, + @QueryParam(value = "end-month") String endMonth, + @QueryParam(value = "format") String format) { if (StringUtils.isBlank(format)) { format = ""; } String url = getIssuer() + this.statUrl; - JsonNode jsonNode = this.authService.getStat(url, authorization, month, format); - logger.trace("StatResource::getUserStatistics() - jsonNode:{} ",jsonNode); + JsonNode jsonNode = this.authService.getStat(url, authorization, month, startMonth, endMonth, format); + logger.trace("StatResource::getUserStatistics() - jsonNode:{} ", jsonNode); return Response.ok(jsonNode.get("response")).build(); } diff --git a/jans-config-api/server/src/main/java/io/jans/configapi/security/client/AuthClientFactory.java b/jans-config-api/server/src/main/java/io/jans/configapi/security/client/AuthClientFactory.java index 525b8930569..f338875f836 100644 --- a/jans-config-api/server/src/main/java/io/jans/configapi/security/client/AuthClientFactory.java +++ b/jans-config-api/server/src/main/java/io/jans/configapi/security/client/AuthClientFactory.java @@ -7,6 +7,7 @@ package io.jans.configapi.security.client; import static io.jans.as.model.util.Util.escapeLog; + import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -18,7 +19,9 @@ import io.jans.as.model.common.GrantType; import io.jans.as.model.common.IntrospectionResponse; import io.jans.as.model.jwk.JSONWebKeySet; + import static io.jans.as.model.jwk.JWKParameter.JSON_WEB_KEY_SET; + import io.jans.configapi.core.util.Jackson; import org.apache.commons.lang.StringUtils; import org.json.JSONObject; @@ -41,10 +44,10 @@ @RegisterProvider(StatClient.class) @ApplicationScoped public class AuthClientFactory { - + private static final String CONTENT_TYPE = "Content-Type"; - + private static Logger log = LoggerFactory.getLogger(AuthClientFactory.class); public static IntrospectionService getIntrospectionService(String url, boolean followRedirects) { @@ -52,7 +55,7 @@ public static IntrospectionService getIntrospectionService(String url, boolean f } public static IntrospectionResponse getIntrospectionResponse(String url, String header, String token, - boolean followRedirects) { + boolean followRedirects) { log.debug("Introspect Token - url:{}, header:{}, token:{} ,followRedirects:{} ", url, header, token, followRedirects); ResteasyWebTarget target = (ResteasyWebTarget) ClientBuilder.newClient() @@ -61,15 +64,15 @@ public static IntrospectionResponse getIntrospectionResponse(String url, String return proxy.introspectToken(header, token); } - public static JsonNode getStatResponse(String url, String token, String month, String format) { + public static JsonNode getStatResponse(String url, String token, String month, String startMonth, String endMonth, String format) { if (log.isDebugEnabled()) { - log.debug("Stat Response Token - url:{}, token:{}, month:{} ,format:{} ", escapeLog(url), escapeLog(token), - escapeLog(month), escapeLog(format)); + log.debug("Stat Response Token - url:{}, token:{}, month:{}, startMonth:{}, endMonth:{}, format:{} ", escapeLog(url), escapeLog(token), + escapeLog(month), escapeLog(startMonth), escapeLog(endMonth), escapeLog(format)); } - ResteasyWebTarget webTarget = (ResteasyWebTarget) ClientBuilder.newClient() + ResteasyWebTarget webTarget = (ResteasyWebTarget) ClientBuilder.newClient() .target(url); StatService statService = webTarget.proxy(StatService.class); - return statService.stat(token, month, format); + return statService.stat(token, month, startMonth, endMonth, format); } public static JsonNode getHealthCheckResponse(String url) { @@ -86,7 +89,7 @@ public static JsonNode getHealthCheckResponse(String url) { } public static TokenResponse requestAccessToken(final String tokenUrl, final String clientId, - final String clientSecret, final String scope) { + final String clientSecret, final String scope) { log.debug("Request for Access Token - tokenUrl:{}, clientId:{}, clientSecret:{}, scope:{} ", tokenUrl, clientId, clientSecret, scope); Response response = null; @@ -186,7 +189,7 @@ public static JSONWebKeySet getJSONWebKeys(String jwksUri) { return null; } - + private static Builder getClientBuilder(String url) { return ClientBuilder.newClient().target(url).request(); } diff --git a/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/AuthService.java b/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/AuthService.java index 88587d99cfa..369ecdbf282 100644 --- a/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/AuthService.java +++ b/jans-config-api/server/src/main/java/io/jans/configapi/service/auth/AuthService.java @@ -16,10 +16,10 @@ public class AuthService { @Inject AuthClientFactory authClientFactory; - public JsonNode getStat(String url, String token, String month, String format) { - return AuthClientFactory.getStatResponse(url, token, month, format); + public JsonNode getStat(String url, String token, String month, String startMonth, String endMonth, String format) { + return AuthClientFactory.getStatResponse(url, token, month, startMonth, endMonth, format); } - + public JsonNode getHealthCheckResponse(String url) { return AuthClientFactory.getHealthCheckResponse(url); }