class Bravtroller::Authenticator

Constants

AUTH_REQUEST_PARAMS
CLIENT_ID

Public Class Methods

new(host) click to toggle source
# File lib/bravtroller/authenticator.rb, line 29
def initialize(host)
  if host.is_a?(String)
    @bravia_client = Bravtroller::Client.new(host)
  elsif host.is_a?(Bravtroller::Client)
    @bravia_client = host
  else
    raise "Unsupported type for host: #{host.class}"
  end
end

Public Instance Methods

authorize(auth_code = nil?, &callback) click to toggle source
# File lib/bravtroller/authenticator.rb, line 44
def authorize(auth_code = nil?, &callback)
  response = @bravia_client.post_request('/sony/accessControl', AUTH_REQUEST_PARAMS)

  if response.code == '401'
    challenge_value = auth_code || callback.call(response)
    auth_value = "Basic #{Base64.encode64(":#{challenge_value}")}"
    headers = { 'Authorization' => auth_value }

    auth_response = @bravia_client.post_request('/sony/accessControl', AUTH_REQUEST_PARAMS, headers)

    raise AuthorizationError, 'Authentication failed' if auth_response.code == '401'

    extract_cookie(auth_response)
  else
    # Already authorized, but the Set-Cookie header should still be set
    extract_cookie(response)
  end
end
authorized?() click to toggle source
# File lib/bravtroller/authenticator.rb, line 39
def authorized?
  response = @bravia_client.post_request('/sony/accessControl', AUTH_REQUEST_PARAMS)
  '200' == response.code
end

Private Instance Methods