Skip to content

Commit

Permalink
Fixes for papi limit filter and ML start
Browse files Browse the repository at this point in the history
  • Loading branch information
Camelia-Orcid committed Nov 4, 2024
1 parent f3aa7c6 commit 5ea0d73
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface OrcidTokenStore extends TokenStore {
OAuth2Authentication readAuthenticationFromCachedToken(Map<String, String> cachedTokenData);

void isClientEnabled(String clientId) throws InvalidTokenException;

String readClientId(String tokenValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public OrcidOauth2TokenDetail readOrcidOauth2TokenDetail(String token) {
return orcidOauthTokenDetailService.findIgnoringDisabledByTokenValue(token);
}

@Override
public String readClientId(String token) {
String clientId = null;
OrcidOauth2TokenDetail orcidTokenDetail = orcidOauthTokenDetailService.findIgnoringDisabledByTokenValue(token);
if(orcidTokenDetail != null) {
clientId = orcidTokenDetail.getClientDetailsId();
}
return clientId;
}

/**
* Read the authentication stored under the specified token value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@

<!-- Panoply Redshift -->
<bean id="panoplyRedshiftDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="driverClassName" value="\${org.orcid.core.utils.panoply.driver:com.amazon.redshift.jdbc.Driver}" />
<property name="jdbcUrl" value="\${org.orcid.core.utils.panoply.jdbcUrl}" />
<property name="username" value="\${org.orcid.core.utils.panoply.username}" />
<property name="password" value="\${org.orcid.core.utils.panoply.password}" />
<property name="maximumPoolSize" value="\${org.orcid.core.utils.panoply.maxPoolSize:5}" />
<property name="idleTimeout" value="\${org.orcid.core.utils.panoply.idleConnectionTimeout:3600}" />
<property name="connectionTimeout" value="\${org.orcid.core.utils.panoply.connectionTimeout:3600}" />
<property name="driverClassName" value="${org.orcid.core.utils.panoply.driver:com.amazon.redshift.jdbc.Driver}" />
<property name="jdbcUrl" value="${org.orcid.core.utils.panoply.jdbcUrl}" />
<property name="username" value="${org.orcid.core.utils.panoply.username}" />
<property name="password" value="${org.orcid.core.utils.panoply.password}" />
<property name="maximumPoolSize" value="${org.orcid.core.utils.panoply.maxPoolSize:5}" />
<property name="idleTimeout" value="${org.orcid.core.utils.panoply.idleConnectionTimeout:3600}" />
<property name="connectionTimeout" value="${org.orcid.core.utils.panoply.connectionTimeout:3600}" />
</bean>

<!-- Panoply JdbcTemplate Bean Definition -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PublicApiDailyRateLimitDaoImpl() {

@Override
public PublicApiDailyRateLimitEntity findByClientIdAndRequestDate(String clientId, LocalDate requestDate) {
Query nativeQuery = entityManager.createNativeQuery("SELECT * FROM public_api_daily_rate_limit p client_id=:clientId and requestDate=:requestDate",
Query nativeQuery = entityManager.createNativeQuery("SELECT * FROM public_api_daily_rate_limit p where p.client_id=:clientId and p.request_date=:requestDate",
PublicApiDailyRateLimitEntity.class);
nativeQuery.setParameter("clientId", clientId);
nativeQuery.setParameter("requestDate", requestDate);
Expand Down Expand Up @@ -95,7 +95,7 @@ public boolean updatePublicApiDailyRateLimit(PublicApiDailyRateLimitEntity papiR
query.setParameter("ipAddress", papiRateLimitingEntity.getIpAddress());
}
query.setParameter("requestCount", papiRateLimitingEntity.getRequestCount());
query.setParameter("requestDate", papiRateLimitingEntity.getRequestDate().toString());
query.setParameter("requestDate", papiRateLimitingEntity.getRequestDate());
return query.executeUpdate() > 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,13 @@

<!-- Panoply Redshift -->
<bean id="panoplyRedshiftDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="driverClassName" value="\${org.orcid.core.utils.panoply.driver:com.amazon.redshift.jdbc.Driver}" />
<property name="jdbcUrl" value="\${org.orcid.core.utils.panoply.jdbcUrl}" />
<property name="username" value="\${org.orcid.core.utils.panoply.username}" />
<property name="password" value="\${org.orcid.core.utils.panoply.password}" />
<property name="maximumPoolSize" value="\${org.orcid.core.utils.panoply.maxPoolSize:5}" />
<property name="idleTimeout" value="\${org.orcid.core.utils.panoply.idleConnectionTimeout:3600}" />
<property name="connectionTimeout" value="\${org.orcid.core.utils.panoply.connectionTimeout:3600}" />
<property name="driverClassName" value="${org.orcid.core.utils.panoply.driver:com.amazon.redshift.jdbc.Driver}" />
<property name="jdbcUrl" value="${org.orcid.core.utils.panoply.jdbcUrl}" />
<property name="username" value="${org.orcid.core.utils.panoply.username}" />
<property name="password" value="${org.orcid.core.utils.panoply.password}" />
<property name="maximumPoolSize" value="${org.orcid.core.utils.panoply.maxPoolSize:5}" />
<property name="idleTimeout" value="${org.orcid.core.utils.panoply.idleConnectionTimeout:3600}" />
<property name="connectionTimeout" value="${org.orcid.core.utils.panoply.connectionTimeout:3600}" />
</bean>

<!-- Panoply JdbcTemplate Bean Definition -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.orcid.core.manager.impl.OrcidUrlManager;
import org.orcid.core.manager.v3.EmailManager;
import org.orcid.core.manager.v3.RecordNameManager;
import org.orcid.core.oauth.service.OrcidTokenStore;
import org.orcid.core.utils.OrcidRequestUtil;
import org.orcid.persistence.dao.ProfileDao;
import org.orcid.persistence.dao.PublicApiDailyRateLimitDao;
Expand Down Expand Up @@ -73,6 +74,9 @@ public class ApiRateLimitFilter extends OncePerRequestFilter {
@Resource
private PanoplyRedshiftClient panoplyClient;

@Autowired
private OrcidTokenStore orcidTokenStore;

@Value("${org.orcid.papi.rate.limit.anonymous.requests:10000}")
private int anonymousRequestLimit;

Expand All @@ -97,8 +101,17 @@ protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServl
throws ServletException, IOException {
LOG.trace("ApiRateLimitFilter starts, rate limit is : " + enableRateLimiting);
if (enableRateLimiting) {
String clientId = orcidSecurityManager.getClientIdFromAPIRequest();
String ipAddress = OrcidRequestUtil.getIpAddress(httpServletRequest);
String tokenValue = httpServletRequest.getHeader("Authorization").replaceAll("Bearer|bearer", "").trim();

String ipAddress = httpServletRequest.getRemoteAddr();

String clientId = null;
try {
clientId = orcidTokenStore.readClientId(tokenValue);
}
catch(Exception ex) {
LOG.error("Exception when trying to get the client id from token value, ignoring and treating as anonymous client");
}
boolean isAnonymous = (clientId == null);
LocalDate today = LocalDate.now();

Expand Down Expand Up @@ -169,7 +182,7 @@ private void rateLimitClientRequest(String clientId, LocalDate today) {
// create
rateLimitEntity = new PublicApiDailyRateLimitEntity();
rateLimitEntity.setClientId(clientId);
rateLimitEntity.setRequestCount(0L);
rateLimitEntity.setRequestCount(1L);
rateLimitEntity.setRequestDate(today);
papiRateLimitingDao.persist(rateLimitEntity);
}
Expand Down

0 comments on commit 5ea0d73

Please sign in to comment.