Class: Bravtroller::Authenticator
- Inherits:
-
Object
- Object
- Bravtroller::Authenticator
- 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
- #authorize(auth_code = nil?, , &callback) ⇒ Object
- #authorized? ⇒ Boolean
-
#initialize(host) ⇒ Authenticator
constructor
A new instance of Authenticator.
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 (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' (auth_response) else # Already authorized, but the Set-Cookie header should still be set (response) end end |
#authorized? ⇒ Boolean
39 40 41 42 |
# File 'lib/bravtroller/authenticator.rb', line 39 def response = @bravia_client.post_request('/sony/accessControl', AUTH_REQUEST_PARAMS) '200' == response.code end |