JsonWebTokenError: error in secret or public key callback: socket hang up - jwks-rsa

Problem statement

We have been encountering these intermittent errors for accessing https://TENANT_DOMAIN/.well-known/jwks.json.

JsonWebTokenError: error in secret or public key callback: socket hang up

These errors also happen across other tenants. I saw other users having the same issues in this thread:

What is causing these errors?

Cause

For Node JS based, the error occurs when a ECONNRESET is thrown:

This is because the server closed the socket before the client did (TCP level), usually because the client is reusing sockets to make requests, and unfortunately times the request with the socket closing.

Solution

In this case caching should be used:

This should reduce the chance a socket is reused by reducing the number of requests being made in general - signing keys should only change when a tenant admin rotates them or in the unlikely event that Auth0 has to rotate a compromised private key and notify the impacted customer. Thus the keys can be cached for long periods. If there is an error, then the jwks.json endpoint can be called to check the cache is updated.

1 Like