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

Store singing keys as fallback #1047

Merged
merged 2 commits into from
Apr 17, 2021
Merged

Store singing keys as fallback #1047

merged 2 commits into from
Apr 17, 2021

Conversation

dome4
Copy link
Contributor

@dome4 dome4 commented Apr 14, 2021

I was thinking about how to cache the JWKS in callbackHistoryAndResetJwtKeys() of the HistoryJwtKeysCallbackHandlerService because the method is used in every refresh cycle. In production, the token refresh might succeed, but if the connection is lost before requesting the JWKS, the entire pipeline will break after two retries. However, storing the result like in getAuthWellKnownEndPoints() of the AuthWellKnownService is not feasible because of possible key rotations by the identity server. Therefore, I decided on a network-first strategy that tries the request for three times but falls back to the stored keys in case of failure.

In addition, the retrieved JWKS always replace the stored keys, which minimizes the risk of validating the tokens against expired keys.

Any thoughts?

@damienbod
Copy link
Owner

damienbod commented Apr 15, 2021

Hi @dome4 thanks for the PR

Thinking out loud now. Need to think about this a bit. I think that this is cached anyway by the browser as we use a HTTP GET request.

One problem would be with key rotation. If we read the keys first from storage , we might be using old keys and validation will fail. This would probably never happen because the session storage gets clean up once we close the tab, browser etc., so not really a problem.

If the HTTP request fail this is awesome.

So maybe it good.

@FabianGosebrink @dome4 Would love your thoughts on this

Greetings Damien

@dome4
Copy link
Contributor Author

dome4 commented Apr 16, 2021

I totally agree with the concerns about key rotation. That's the reason why I tried to implement a network-first strategy: the idea is to try the request for three times (retry(2) in SigninKeyDataService ). If and only if the request fails for three times, the stored keys are used as a fallback (first catchError() block in HistoryJwtKeysCallbackHandlerService). Thus, in the most cases, the stored keys are skipped.

Furthermore, caching get requests through the browser is not suitable for me, as I am trying to implement a PWA that stores the keys in local storage to be able to launch the app event when offline. In this case the session could be already destroyed when restarting the app.

@damienbod
Copy link
Owner

@dome4 thanks, I think this is good and we add the feature. I'll test this once the code is good. @FabianGosebrink can you do a code review as well, LGTM, needs some tests.

Greetings Damien

@@ -88,4 +100,12 @@ export class HistoryJwtKeysCallbackHandlerService {
private resetBrowserHistory() {
window.history.replaceState({}, window.document.title, window.location.origin + window.location.pathname);
}

private storeSigningKeys(jwtKeys: JwtKeys) {
this.storagePersistanceService.write('jwtKeys', jwtKeys);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a constant in the class to avoid having different keys on read/write

@FabianGosebrink
Copy link
Collaborator

FabianGosebrink commented Apr 16, 2021

Code looks absolutely fine, just one small comment. coverage dropped a bit, would be nice to have this staying at least the same. Good Job.

UPDATE: Just checked the coverage and it went down in a completely different place. IMHO we can merge this.

@damienbod damienbod merged commit 2bcabd6 into damienbod:main Apr 17, 2021
@dome4 dome4 deleted the jwks branch April 17, 2021 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants