Class: Clavem::Authorizer

Inherits:
Object
  • Object
show all
Includes:
R18n::Helpers
Defined in:
lib/clavem/authorizer.rb

Overview

A class to handle oAuth authorizations.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

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.

Parameters:

  • ip (String) (defaults to: "127.0.0.1")

    The IP address on which listening for replies. Default is 127.0.0.1.

  • port (Fixnum) (defaults to: 2501)

    The port on which listening for replies. Default is 2501.

  • command (String|nil) (defaults to: nil)

    The command to open the URL. {{URL}} is replaced with the specified URL. Default is open "{{URL}}".

  • title (String|nil) (defaults to: nil)

    The title for response template. Default is Clavem Authorization

  • template (String|nil) (defaults to: nil)

    Alternative template to show progress in user's browser.

  • timeout (Fixnum) (defaults to: 0)

    The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is 0, which means disabled.

  • response_handler (Proc)

    A Ruby block to handle response and check for success. See #default_response_handler.



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}}".

Returns:

  • (String)

    the current value of command



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.

Returns:

  • (String)

    the current value of ip



39
40
41
# File 'lib/clavem/authorizer.rb', line 39

def ip
  @ip
end

- (R18N::Translation) localizer

A localizer object.

Returns:

  • (R18N::Translation)

    the current value of localizer



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.

Returns:

  • (Fixnum)

    the current value of port



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.

Returns:

  • (Proc)

    the current value of 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.

Returns:

  • (String)

    the current value of template



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.

Returns:

  • (Fixnum)

    the current value of timeout



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.

Returns:

  • (String)

    the current value of title



39
40
41
# File 'lib/clavem/authorizer.rb', line 39

def title
  @title
end

- (String) token

The token obtained by the remote endpoint.

Returns:

  • (String)

    the current value of token



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..

Returns:

  • (String)

    the current value of url



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.

Parameters:

  • ip (String) (defaults to: "127.0.0.1")

    The IP address on which listening for replies. Default is 127.0.0.1.

  • port (Fixnum) (defaults to: 2501)

    The port on which listening for replies. Default is 2501.

  • command (String|nil) (defaults to: nil)

    The command to open the URL. {{URL}} is replaced with the specified URL. Default is open "{{URL}}".

  • title (String|nil) (defaults to: nil)

    The title for response template. Default is Clavem Authorization

  • template (String|nil) (defaults to: nil)

    Alternative template to show progress in user's browser.

  • timeout (Fixnum) (defaults to: 0)

    The amount of milliseconds to wait for response from the remote endpoint before returning a failure. Default is 0, which means disabled.

  • response_handler (Proc)

    A Ruby block to handle response and check for success. See #default_response_handler.

  • force (Boolean) (defaults to: false)

    If to force recreation of the instance.

Returns:

  • (Authorizer)

    The 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.

Parameters:

  • url (String)

    The URL where to send the user to start authorization.

Returns:

Raises:



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 authorize(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.

Returns:

  • (String)

    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.

Parameters:

  • authorizer (Authorizer)

    The current authorizer.

  • request (WEBrick::HTTPRequest)

    The request that the remote endpoint made to notify authorization status.

  • response (WEBrick::HTTPResponse)

    The request to send to the browser.

Returns:

  • (String|nil)

    The oAuth access token. Returning nil means authorization denied.



147
148
149
# File 'lib/clavem/authorizer.rb', line 147

def default_response_handler(authorizer, request, response)
  request.query['oauth_token'].ensure_string
end