-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make JWKS cache shared across SDK instances
After 1123911 the JWKs cache wasn't working as expected, since the middleware constructed a new SDK instance on each request and the cache was an instance variable. So each request was effectively bypassing the cache and requested the JWKS anew. With this change, the JWKs cache is made thread-safe and shared between all instances of the SDK. Fixes AUTH-76
- Loading branch information
Showing
4 changed files
with
43 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class JWKSCache | ||
def initialize(lifetime) | ||
@lifetime = lifetime | ||
@jwks = nil | ||
@fetched_at = nil | ||
@lock = Concurrent::ReadWriteLock.new | ||
end | ||
|
||
def fetch(sdk, force_refresh: false) | ||
should_refresh = @lock.with_read_lock do | ||
force_refresh || @jwks.nil? || (@fetched_at && ((Time.now.to_i-@fetched_at) > @lifetime)) | ||
end | ||
|
||
if should_refresh | ||
@lock.with_write_lock do | ||
@jwks = sdk.jwks.all["keys"] | ||
@fetched_at = Time.now.to_i | ||
end | ||
end | ||
|
||
@lock.with_read_lock do | ||
@jwks | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters