Class: WhopSDK::Helpers::VerifyUserToken::RemoteJwks

Inherits:
Object
  • Object
show all
Defined in:
lib/whop_sdk/helpers/verify_user_token.rb

Overview

Thread-safe, TTL-bounded remote JWKS cache with cooldown-gated refresh on kid miss.

Instance Method Summary collapse

Constructor Details

#initialize(url, cache_max_age_seconds:, cooldown_seconds:) ⇒ RemoteJwks

Returns a new instance of RemoteJwks.



45
46
47
48
49
50
51
52
53
# File 'lib/whop_sdk/helpers/verify_user_token.rb', line 45

def initialize(url, cache_max_age_seconds:, cooldown_seconds:)
  @url = url
  @cache_max_age_seconds = cache_max_age_seconds
  @cooldown_seconds = cooldown_seconds
  @mutex = Mutex.new
  @jwk_set = nil
  @fetched_at = nil
  @last_refresh_attempt_at = nil
end

Instance Method Details

#jwk_set(force_refresh: false) ⇒ Object

Returns the current JWK::Set, fetching or refreshing as needed. When ‘force_refresh` is true, bypass the freshness window but still honor the refetch cooldown.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/whop_sdk/helpers/verify_user_token.rb', line 58

def jwk_set(force_refresh: false)
  @mutex.synchronize do
    now = monotonic_now
    cache_stale = @jwk_set.nil? || @fetched_at.nil? ||
                  (now - @fetched_at) > @cache_max_age_seconds
    cooldown_elapsed = @last_refresh_attempt_at.nil? ||
                       (now - @last_refresh_attempt_at) > @cooldown_seconds

    refresh_now = @jwk_set.nil? || cache_stale ||
                  (force_refresh && cooldown_elapsed)

    fetch! if refresh_now
    @jwk_set
  end
end