oidc-client icon indicating copy to clipboard operation
oidc-client copied to clipboard

TokenId invalid error when using serviceworker, keycloak in an nx react app

Open Sneham14 opened this issue 1 year ago • 8 comments

Issue and Steps to Reproduce

I am integrating react-oidc lib with an nx monorepo react app. Because of the complex folder structure it's not able to locate the public folder directly. Hence I copied the OidcServiceWorker.js and OidcTrustedDomains.js from example repo and it looks to be setup correctly. The service worker is able to fetch the token with keycloak but the issue is with the nounce mismatch which gives token id invalid error. The isTokenOidcValid method recieves separate nonce - formattedTokens has NONCE_SECURED_BY_SERVICE_WORKER while nonceData.once has nonce from sessionStorage value.

Steps to reproduce:-

  1. Setup an integrated nx react app
  2. Setup keycloak using docker
  3. Integrate lib with
   configuration:export const configuration = {
    client_id: 'test',
    redirect_uri: 'http://localhost:4200/*',
    silent_redirect_uri: 'http://localhost:4200/home',
    scope: 'openid profile email api offline_access',
    authority: 'http://localhost:8080/realms/test-app'
    service_worker_relative_url: '/OidcServiceWorker.js',
    service_worker_only: false,
};

Versions

react-oidc: 6.24.1

Expected

To have NONCE_SECURED_BY_SERVICE_WORKER instead of undefined 'default' in sendMessageAsync in login.ts on a getNonce event. This flows down and in the verification fails as it defaults to sessionStore nounce value

Am I missing anything in serviceWorker configuration?

Sneham14 avatar Jul 16 '23 12:07 Sneham14

Hi @Sneham14 , thank you very much for your issue. Did you upgrade react oodc version recently ? If yes ServiceWorker.js is not up to date that may do this error occur.

guillaume-chervet avatar Jul 16 '23 17:07 guillaume-chervet

Thank you for the swift response on a Sunday. Yes, this is my first time working with react-oidc so using the latest. Should I use another version or update my OidcServiceWorker code somewhere? To note, even if the login fails, when I reload the app next time ServiceWorker calls seems to be working well along with the warn in console nonce not found in service worker, using session store instead. I can also see userinfo using service worker in network tab. The issue occurs only on first login where the workflow is:- Authenticating msg -> Keycloak IDP login -> Authentication success screen msg -> Authentication failed immediately(nonce issue) with the payload in the url.

Update: Works perfectly when I bumped down one level 6.23.0 works beautifully!

Sneham14 avatar Jul 16 '23 18:07 Sneham14

Hi @Sneham14 , thank you for your answer. Does it work if you disable service worker mode (comment serviceworker file from configuration and unistall service worker from dev tool)? Which browser are you using? I will publish tomorrow a version with more detailled logs about nonce check.

guillaume-chervet avatar Jul 16 '23 19:07 guillaume-chervet

Yes it works without serviceworker using session storage. I tried on Google Chrome and Microsoft Edge. Thanks

Sneham14 avatar Jul 16 '23 19:07 Sneham14

hi @Sneham14 , i need to wait for a big PullRequest to be done before adding more logs.

Can you wait a litle bit before doing the tests with more log ?

guillaume-chervet avatar Jul 17 '23 11:07 guillaume-chervet

Hi @Sneham14 ,

version 7.2.2 add a lot of more log detail to try to understand your problem.

guillaume-chervet avatar Aug 07 '23 16:08 guillaume-chervet

Hi @Sneham14 , do you have any news about the logs?

guillaume-chervet avatar Sep 15 '23 15:09 guillaume-chervet

Hi @Sneham14 , do you have any news?

guillaume-chervet avatar Feb 04 '24 12:02 guillaume-chervet