Skip to content

Commit

Permalink
Merge branch 'add-summary-to-summary.json-and-mapi-summary-endpoint' of
Browse files Browse the repository at this point in the history
https://github.com/ORCID/ORCID-Source into add-summary-to-summary.json-and-mapi-summary-endpoint
  • Loading branch information
auumgn committed Nov 8, 2024
2 parents 6dfe9a6 + 29ca6bd commit 7d10f6f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.67.11 - 2024-11-05

[Full Changelog](https://github.com/ORCID/ORCID-Source/compare/v2.67.10...v2.67.11)

## v2.67.10 - 2024-11-05

[Full Changelog](https://github.com/ORCID/ORCID-Source/compare/v2.67.9...v2.67.10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

Expand All @@ -14,6 +16,7 @@
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.orcid.core.manager.ClientDetailsEntityCacheManager;
import org.orcid.core.manager.OrcidSecurityManager;
import org.orcid.core.manager.TemplateManager;
Expand Down Expand Up @@ -88,6 +91,9 @@ public class ApiRateLimitFilter extends OncePerRequestFilter {

@Value("${org.orcid.persistence.panoply.papiExceededRate.production:false}")
private boolean enablePanoplyPapiExceededRateInProduction;

@Value("${org.orcid.papi.rate.limit.ip.whiteSpaceSeparatedWhiteList:127.0.0.1}")
private String papiWhiteSpaceSeparatedWhiteList;

private static final String TOO_MANY_REQUESTS_MSG = "Too Many Requests - You have exceeded the daily allowance of API calls.\\n"
+ "You can increase your daily quota by registering for and using Public API client credentials "
Expand All @@ -105,7 +111,7 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl
if (httpServletRequest.getHeader("Authorization") != null) {
tokenValue = httpServletRequest.getHeader("Authorization").replaceAll("Bearer|bearer", "").trim();
}
String ipAddress = httpServletRequest.getRemoteAddr();
String ipAddress = getClientIpAddress(httpServletRequest);

String clientId = null;
if (tokenValue != null) {
Expand All @@ -118,9 +124,11 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl
boolean isAnonymous = (clientId == null);
LocalDate today = LocalDate.now();

if (isAnonymous) {
LOG.info("ApiRateLimitFilter anonymous request");
this.rateLimitAnonymousRequest(ipAddress, today, httpServletResponse);
if (isAnonymous ) {
if(!isWhiteListed(ipAddress)) {
LOG.info("ApiRateLimitFilter anonymous request for ip: " + ipAddress);
this.rateLimitAnonymousRequest(ipAddress, today, httpServletResponse);
}

} else {
LOG.info("ApiRateLimitFilter client request with clientId: " + clientId);
Expand Down Expand Up @@ -250,5 +258,33 @@ private void setPapiRateExceededItemInPanoply(PanoplyPapiDailyRateExceededItem i

});
}

//gets actual client IP address, using the headers that the proxy server ads
private String getClientIpAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("X-REAL-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
if (ipAddress != null && ipAddress.contains(",")) {
ipAddress = ipAddress.split(",")[0].trim();
}
return ipAddress;
}

private boolean isWhiteListed(String ipAddress) {
List<String> papiIpWhiteList = null;
if(StringUtils.isNotBlank(papiWhiteSpaceSeparatedWhiteList)) {
papiIpWhiteList = Arrays.asList(papiWhiteSpaceSeparatedWhiteList.split("\\s"));
}

if(papiIpWhiteList != null) {
return papiIpWhiteList.contains(ipAddress);

}
return false;
}

}

0 comments on commit 7d10f6f

Please sign in to comment.