Make HttpsJwks threadsafe

Merged
#13 · Created  · Last updated

Merged pull request

Merged in zimmi/jose4j/threadsafe-https-jwks (pull request #13)

ba3913d·Author: ·Closed by: ·2018-05-07

Description

Hi!

The Javadoc of HttpsJwks reads:

The keys are cached per {@code HttpsJwks} instance so your application will need to keep using the same instance, however is appropriate for that application, to get the benefit of the caching.

However, HttpsJwks is not threadsafe: The cache field can be written to and read from different Threads without any happens-before relationship. This is a problem in typical web applications where every request is assigned to a Thread out of a shared pool. So I believe it's best to be threadsafe by default.

Since the other fields are mutable and could be potentially altered after construction they need to be at least volatile as well. It looks ok now, but it's hard to imagine all the potential interactions.

I also added a Lock to avoid flooding the OP with requests from multiple threads that try to refresh at the same time.

I believe this is threadsafe now, but I'd love another opinion.

Oh, and thank you so much for this library. The lack of dependencies is great. :)

0 attachments

0 comments

Loading commits...