-
Notifications
You must be signed in to change notification settings - Fork 49
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
add an extension point to customize evaluation time used in DefaultJWTTokenParser #757
Comments
Hi @erickloss Can you clarify please a bit more why the current time is not suitable ? Is it because you may have some test tokens with the expiry time already in the past or too far in the future for the test to wait till they expire ? The evaluation time seems to be a test scope only property but one can easily generate test tokens with the JWT build api, setting the required expiry time... |
Hello @sberyozkin ,
Our End-To-End test setup uses a Playwright Browser to Test most of the application in "real-life" mode.
Basically, I want to test the whole login-feature as if a real user would login via web form. That includes the creation of the JWT by the library. If possible, I want to have no lines of code in the productive logic, that are only executed during testing. In other words, I don't want to reimplement the logic that sets the expiration date by myself when creating the JWT, as it is already implemented in the library using the property What I did to Fix this on my side:
public class JwtMockDateProvider {
private static MockedStatic<NumericDate> mocked;
@ServerRequestFilter(preMatching = true)
public void pre(ContainerRequestContext context) {
mocked = Mockito.mockStatic(NumericDate.class, CALLS_REAL_METHODS);
mocked.when(NumericDate::now).thenAnswer(i -> {
if (TestCurrentDateProvider.mockNow != null) {
return NumericDate.fromSeconds(TestCurrentDateProvider.mockNow.toEpochSecond());
} else {
return i.callRealMethod();
}
});
}
@ServerResponseFilter
public void post(ContainerResponseContext context) {
mocked.close();
}
} I might also simply reimplement the expiration date by myself setting the I hope that clarifies my use-case a bit better. If you have any further questions, please tell me. What do you think? |
PS: there are lots of other test-cases where we assume an already authenticated JWT. For all those tests, we create a JWT programmatically in the test code. This only concerns test-cases where we actually test the creation of the JWT. |
PPS: Concerning the validation / authentication of the JWT, I see no way to test an expired JWT that is created via the library without:
Point 1. would be in the category "Configuring the prod application differently, only for sake of testability" |
Hi @erickloss Would you be open to creating a PR, to allow configuring this property ? |
Hi @sberyozkin , Yes, I would love to contribute. Earliest possible will be next week. A few questions in advance:
Cheers |
@erickloss Hey, by the way, I was thinking about it, we have Can you clarify again please (without gherkin scenarios), how can the evaluation time help instead. Lets say you have a token whose expiry claim will keep it valid for another hour from now. And, you'd like to test that if the token has expired, 401 is returned, for the higher level Gherkin scenario to pass, right ? If the evaluation time property were available, how would you know how to set it correctly for the test to pass ? Can you imagine |
@erickloss I'm not sure what kind of an extension point you have in mind, can you prototype some code to clarify it ? |
Hello there,
Problem
I need to set / mock the actual evaluation time for creating and verifying a JWT.
Why
we use quarkus / smallrye JWT in our project. I want to write end-to-end tests for the application.
One whole scenario is: handling expired JWT tokens.
Our gherkin test looks somehow like this:
My solution idea
I've looked up the code, and there is an extension point in jose that allows you to set a static evaluation time
org.jose4j.jwt.consumer.JwtConsumerBuilder#setEvaluationTime
That could be exposed in smallrye-jwt via configuration or CDI extension point.
What do you think? Is this somehow possible already and I just haven't found a doc for that?
Any help would be nice :)
Thank you + cheers
The text was updated successfully, but these errors were encountered: