Class: Fb::Jwt::Auth::ServiceTokenClient

Inherits:
Object
  • Object
show all
Defined in:
lib/fb/jwt/auth/service_token_client.rb

Defined Under Namespace

Classes: ServiceTokenCacheError

Constant Summary collapse

ENDPOINTS =
{
  v2: '/service/v2/%{application}',
  v3: '/v3/applications/%{application}/namespaces/%{namespace}'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application:, namespace: nil, ignore_cache: false) ⇒ ServiceTokenClient

Returns a new instance of ServiceTokenClient.



15
16
17
18
19
20
21
# 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

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



13
14
15
# File 'lib/fb/jwt/auth/service_token_client.rb', line 13

def api_version
  @api_version
end

#applicationObject

Returns the value of attribute application.



13
14
15
# File 'lib/fb/jwt/auth/service_token_client.rb', line 13

def application
  @application
end

#namespaceObject

Returns the value of attribute namespace.



13
14
15
# File 'lib/fb/jwt/auth/service_token_client.rb', line 13

def namespace
  @namespace
end

#root_urlObject

Returns the value of attribute root_url.



13
14
15
# File 'lib/fb/jwt/auth/service_token_client.rb', line 13

def root_url
  @root_url
end

Instance Method Details

#public_keyObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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