Skip to content

Commit 166ebcf

Browse files
committed
Fixed the problem with reversed internal token prolonging. Added test coverage for this edge case.
1 parent 093fcad commit 166ebcf

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/main/java/io/github/majusko/grpc/jwt/service/JwtService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public String getInternal() {
6161

6262
final long refreshThresholdValue = Double.valueOf(properties.getExpirationSec() * REFRESH_TIME_THRESHOLD).longValue();
6363

64-
if (LocalDateTime.now().minusSeconds(refreshThresholdValue).isAfter(internal.getExpiration())) {
64+
if (LocalDateTime.now().plusSeconds(refreshThresholdValue).isAfter(internal.getExpiration())) {
6565
refreshInternalToken();
6666
}
6767

src/test/java/io/github/majusko/grpc/jwt/GrpcJwtSpringBootStarterApplicationTest.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
import org.lognet.springboot.grpc.GRpcService;
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Import;
29+
import org.springframework.context.annotation.Primary;
2630
import org.springframework.core.env.Environment;
2731
import org.springframework.test.context.ActiveProfiles;
2832
import org.springframework.test.context.junit4.SpringRunner;
2933

3034
import java.io.IOException;
3135
import java.lang.reflect.Field;
36+
import java.lang.reflect.InvocationTargetException;
37+
import java.lang.reflect.Method;
3238

3339
@RunWith(SpringRunner.class)
3440
@SpringBootTest
@@ -312,37 +318,38 @@ public void testEmptyUserIdInToken() throws IOException {
312318
}
313319

314320
@Test
315-
public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException {
321+
public void testExpiredToken() throws IOException, NoSuchFieldException, IllegalAccessException,
322+
NoSuchMethodException, InvocationTargetException, InterruptedException {
316323

317324
final GrpcJwtProperties customProperties = new GrpcJwtProperties();
318325
final Field field = customProperties.getClass().getDeclaredField("expirationSec");
319326
field.setAccessible(true);
320-
field.set(customProperties, -10L);
327+
field.set(customProperties, 1L);
321328

329+
final Field propertyField = jwtService.getClass().getDeclaredField("properties");
330+
propertyField.setAccessible(true);
331+
final GrpcJwtProperties existingProperties = (GrpcJwtProperties) propertyField.get(jwtService);
332+
propertyField.set(jwtService, customProperties);
322333

323-
final JwtService customJwtService = new JwtService(environment, customProperties);
324-
final String token = customJwtService.generate(new JwtData("lala", Sets.newHashSet(ExampleService.ADMIN)));
334+
final Method refreshMethod = jwtService.getClass().getDeclaredMethod("refreshInternalToken");
335+
refreshMethod.setAccessible(true);
336+
337+
refreshMethod.invoke(jwtService);
325338

326339
final ManagedChannel channel = initTestServer(new ExampleService());
327340
final Channel interceptedChannel = ClientInterceptors.intercept(channel, authClientInterceptor);
328341
final ExampleServiceGrpc.ExampleServiceBlockingStub stub = ExampleServiceGrpc.newBlockingStub(interceptedChannel);
329-
330-
final Metadata header = new Metadata();
331-
header.put(GrpcHeader.AUTHORIZATION, token);
332-
333-
final ExampleServiceGrpc.ExampleServiceBlockingStub injectedStub = MetadataUtils.attachHeaders(stub, header);
334342
final Example.GetExampleRequest request = Example.GetExampleRequest.newBuilder()
335343
.setUserId("other-user-id").build();
336344

337-
Status status = Status.OK;
345+
Thread.sleep(2000);
338346

339-
try {
340-
final Empty ignore = injectedStub.getExample(request);
341-
} catch (StatusRuntimeException e) {
342-
status = e.getStatus();
343-
}
347+
final Empty response = stub.getExample(request);
344348

345-
Assert.assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode());
349+
Assert.assertNotNull(response);
350+
351+
propertyField.set(jwtService, existingProperties);
352+
refreshMethod.invoke(jwtService);
346353
}
347354

348355
@Test

0 commit comments

Comments
 (0)