Class: FirebaseAuthRuby::PublicKeyFetcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/firebase_auth_ruby/public_key_fetcher.rb

Instance Method Summary collapse

Constructor Details

#initializePublicKeyFetcher

Returns a new instance of PublicKeyFetcher.



13
14
15
# File 'lib/firebase_auth_ruby/public_key_fetcher.rb', line 13

def initialize
  @cache_hit = false
end

Instance Method Details

#fetch(url) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/firebase_auth_ruby/public_key_fetcher.rb', line 17

def fetch(url)
  @cache_hit = false

  public_keys = read(public_keys_cache_key(url))
  public_keys_expire_at = read(expire_at_cache_key(url))

  public_keys_still_valid = public_keys && public_keys_expire_at && Time.now.to_i < public_keys_expire_at.to_i

  if public_keys_still_valid
    @cache_hit = true
    return public_keys
  end

  res = Util.get!(url)

  cache_control_header = res['cache-control']
  if cache_control_header
    parts = cache_control_header.split(',')
    parts.each { |part|
      subpart = part.strip.split('=')
      if subpart[0] == 'max-age'
        max_age = subpart[1].to_i
        write(expire_at_cache_key(url), Time.now.to_i + (max_age * 1000))
      end
    }
  end

  public_keys = JSON.parse(res.body)
  write(public_keys_cache_key(url), public_keys)

  public_keys
end