Class: Bravtroller::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/bravtroller/authenticator.rb

Constant Summary collapse

CLIENT_ID =
'bravtroller:de7cd7d5-a9a0-44a1-aba1-57d973a0ee8a'
AUTH_REQUEST_PARAMS =
{
    method: 'actRegister',
    params: [
        {
            clientid: CLIENT_ID,
            nickname: 'Bravtroller',
            level: 'private',
        },
        [
            value: 'yes',
            function: 'WOL'
        ]
    ],
    id: 8,
    version: '1.0'
}

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Authenticator

Returns a new instance of Authenticator.



29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#authorize(auth_code = nil?, , &callback) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/bravtroller/authenticator.rb', line 39

def authorized?
  response = @bravia_client.post_request('/sony/accessControl', AUTH_REQUEST_PARAMS)
  '200' == response.code
end