Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #12823] Randomly generate TokenRefreshWindow #13046

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.auth.impl.process.HttpLoginProcessor;
import com.alibaba.nacos.common.utils.RandomUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
Expand All @@ -35,6 +36,7 @@
* @author wuyfee
*/

@SuppressWarnings("checkstyle:SummaryJavadoc")
public class NacosClientAuthServiceImpl extends AbstractClientAuthService {

private static final Logger SECURITY_LOGGER = LoggerFactory.getLogger(NacosClientAuthServiceImpl.class);
Expand Down Expand Up @@ -97,7 +99,7 @@ public Boolean login(Properties properties) {
if (identityContext != null) {
if (identityContext.getAllKey().contains(NacosAuthLoginConstant.ACCESSTOKEN)) {
tokenTtl = Long.parseLong(identityContext.getParameter(NacosAuthLoginConstant.TOKENTTL));
tokenRefreshWindow = tokenTtl / 10;
tokenRefreshWindow = generateTokenRefreshWindow(tokenTtl);
lastRefreshTime = System.currentTimeMillis();

LoginIdentityContext newCtx = new LoginIdentityContext();
Expand All @@ -124,4 +126,15 @@ public LoginIdentityContext getLoginIdentityContext(RequestResource resource) {
public void shutdown() throws NacosException {

}

/**
* Randomly generate TokenRefreshWindow, Avoid a large number of logins causing pressure on the Nacos server.
* @param tokenTtl TTL of token in seconds.
* @return tokenRefreshWindow, numerical range [tokenTtl/15 ~ tokenTtl/10]
*/
public long generateTokenRefreshWindow(long tokenTtl) {
long startNumber = tokenTtl / 15;
long endNumber = tokenTtl / 10;
return RandomUtils.nextLong(startNumber, endNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,12 @@ void testReLogin() {
//when
assertTrue(nacosClientAuthService.login(properties));
}

@Test
void testGenerateTokenWithInvalidToken() {
NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
long tokenTtl = 18000L;
long tokenRefreshWindow = nacosClientAuthService.generateTokenRefreshWindow(tokenTtl);
assertTrue(tokenRefreshWindow <= tokenTtl / 10);
}
}
Loading