class Fb::Jwt::Auth::ServiceTokenClient

Constants

ENDPOINTS

Attributes

api_version[RW]
application[RW]
ignore_cache[R]
namespace[RW]
root_url[RW]

Public Class Methods

new(application:, namespace: nil, ignore_cache: false) click to toggle source
# File lib/fb/jwt/auth/service_token_client.rb, line 15
def initialize(application:, namespace: nil, ignore_cache: false)
  @application = application
  @namespace = namespace
  @ignore_cache = ignore_cache
  @root_url = Fb::Jwt::Auth.service_token_cache_root_url
  @api_version = Fb::Jwt::Auth.service_token_cache_api_version || :v2
end

Public Instance Methods

public_key() click to toggle source
# File lib/fb/jwt/auth/service_token_client.rb, line 23
def public_key
  response = Net::HTTP.get_response(public_key_uri)

  unless response.code.to_i == 200
    raise ServiceTokenCacheError.new(
      "Unexpected response code\n" \
      "Response code: #{response.code} => Response body: #{response.body}"
    )
  end

  Base64.strict_decode64(JSON.parse(response.body).fetch('token'))
rescue Errno::ECONNREFUSED => e
  raise ServiceTokenCacheError.new(
    "Unable to connect to the Service Token Cache\n#{e.message}"
  )
end

Private Instance Methods

public_key_uri() click to toggle source
# File lib/fb/jwt/auth/service_token_client.rb, line 44
def public_key_uri
  URI.join(root_url, "#{version_url}#{query_param}")
end
query_param() click to toggle source
# File lib/fb/jwt/auth/service_token_client.rb, line 48
def query_param
  ignore_cache ? '?ignore_cache=true' : ''
end
version_url() click to toggle source
# File lib/fb/jwt/auth/service_token_client.rb, line 52
def version_url
  if api_version == :v3
    ENDPOINTS[api_version] % { application: application, namespace: namespace }
  else
    ENDPOINTS[api_version] % { application: application }
  end
end