Class: Clavem::Authorizer
- Inherits:
-
Object
- Object
- Clavem::Authorizer
- Includes:
- R18n::Helpers
- Defined in:
- lib/clavem/authorizer.rb
Overview
A class to handle oAuth authorizations.
Instance Attribute Summary (collapse)
-
- (String) command
The command to open the URL.
-
- (Object) i18n
Returns the value of attribute i18n.
-
- (String) ip
The IP address on which listening for replies.
-
- (R18N::Translation) localizer
A localizer object.
-
- (Fixnum) port
The port on which listening for replies.
-
- (Proc) response_handler
A Ruby block to handle response and check for success.
-
- (Object) status
Returns the value of attribute status.
-
- (String) template
Alternative template to show progress in user's browser.
-
- (Fixnum) timeout
The amount of milliseconds to wait for response from the remote endpoint before returning a failure.
-
- (String) title
The title for response template.
-
- (String) token
The token obtained by the remote endpoint.
-
- (String) url
The URL where to send the user to start authorization..
Class Method Summary (collapse)
-
+ (Authorizer) instance(ip = "127.0.0.1", port = 2501, command = nil, title = nil, template = nil, timeout = 0, force = false, &response_handler)
Returns a unique (singleton) instance of the authorizer.
Instance Method Summary (collapse)
-
- (Authorizer) authorize(url)
Starts the authorization flow.
-
- (String) callback_url
Returns the callback_url for this authorizer.
-
- (String|nil) default_response_handler(authorizer, request, response)
Handles a response from the remote endpoint.
-
- (Authorizer) initialize(ip = "127.0.0.1", port = 2501, command = nil, title = nil, template = nil, timeout = 0, &response_handler)
constructor
Creates a new authorizer.
Constructor Details
- (Authorizer) initialize(ip = "127.0.0.1", port = 2501, command = nil, title = nil, template = nil, timeout = 0, &response_handler)
Creates a new authorizer.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/clavem/authorizer.rb', line 80 def initialize(ip = "127.0.0.1", port = 2501, command = nil, title = nil, template = nil, timeout = 0, &response_handler) @ip = ip.ensure_string @port = port.to_integer @command = command.ensure_string @title = title.ensure_string @template = template.ensure_string @timeout = timeout.to_integer @response_handler = response_handler sanitize_arguments @token = nil @status = :waiting @compiled_template ||= ::ERB.new(@template) @i18n = t @timeout_expired = false @timeout_thread = nil self end |
Instance Attribute Details
- (String) command
The command to open the URL. {{URL}}
is replaced with the specified URL. Default is open "{{URL}}"
.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def command @command end |
- (Object) i18n
Returns the value of attribute i18n
51 52 53 |
# File 'lib/clavem/authorizer.rb', line 51 def i18n @i18n end |
- (String) ip
The IP address on which listening for replies. Default is 127.0.0.1
.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def ip @ip end |
- (R18N::Translation) localizer
A localizer object.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def localizer @localizer end |
- (Fixnum) port
The port on which listening for replies. Default is 2501
.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def port @port end |
- (Proc) response_handler
A Ruby block to handle response and check for success. @see #default_response_handler.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def response_handler @response_handler end |
- (Object) status
Returns the value of attribute status
50 51 52 |
# File 'lib/clavem/authorizer.rb', line 50 def status @status end |
- (String) template
Alternative template to show progress in user's browser.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def template @template end |
- (Fixnum) timeout
The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is 0
, which means disabled.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def timeout @timeout end |
- (String) title
The title for response template. Default is Clavem Authorization
.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def title @title end |
- (String) token
The token obtained by the remote endpoint.
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def token @token end |
- (String) url
The URL where to send the user to start authorization..
39 40 41 |
# File 'lib/clavem/authorizer.rb', line 39 def url @url end |
Class Method Details
+ (Authorizer) instance(ip = "127.0.0.1", port = 2501, command = nil, title = nil, template = nil, timeout = 0, force = false, &response_handler)
Returns a unique (singleton) instance of the authorizer.
64 65 66 67 68 |
# File 'lib/clavem/authorizer.rb', line 64 def self.instance(ip = "127.0.0.1", port = 2501, command = nil, title = nil, template = nil, timeout = 0, force = false, &response_handler) @instance = nil if force @instance ||= Clavem::Authorizer.new(ip, port, command, title, template, timeout, &response_handler) @instance end |
Instance Method Details
- (Authorizer) authorize(url)
Starts the authorization flow.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/clavem/authorizer.rb', line 105 def (url) @url = url @status = :waiting begin # Setup stuff setup_webserver setup_interruptions_handling setup_timeout_handling # Open the endpoint then start the server open_endpoint @server.start raise Clavem::Exceptions::Timeout.new if @timeout_expired rescue Clavem::Exceptions::Timeout => t @status = :failure raise t rescue => e @status = :failure raise Clavem::Exceptions::Failure.new("Cannot handle response: #{e.to_s}") ensure cleanup end raise Clavem::Exceptions::AuthorizationDenied.new if @status == :denied self end |
- (String) callback_url
Returns the callback_url for this authorizer.
137 138 139 |
# File 'lib/clavem/authorizer.rb', line 137 def callback_url "http://#{ip}:#{port}/" end |
- (String|nil) default_response_handler(authorizer, request, response)
Handles a response from the remote endpoint.
147 148 149 |
# File 'lib/clavem/authorizer.rb', line 147 def default_response_handler(, request, response) request.query['oauth_token'].ensure_string end |