Class: Clavem::Authorizer

Inherits:
Object
  • Object
show all
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

#initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler) ⇒ Authorizer

Creates a new authorizer.


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/clavem/authorizer.rb', line 75

def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
  @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
  @host = host.ensure_string
  @port = port.to_integer
  @command = command.ensure_string
  @timeout = timeout.to_integer
  @response_handler = response_handler
  @token = nil
  @status = :waiting

  sanitize_arguments
  self
end

Instance Attribute Details

#:i18n(: i18n) ⇒ Bovem::I18n (readonly)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#commandString


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#hostString


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#i18nObject (readonly)

Returns the value of attribute i18n


48
49
50
# File 'lib/clavem/authorizer.rb', line 48

def i18n
  @i18n
end

#portFixnum


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#response_handlerObject

Returns the response handler for the authorizer.


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#statusSymbol


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#timeoutFixnum


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#tokenString


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

#urlString


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/clavem/authorizer.rb', line 39

class Authorizer
  attr_accessor :url
  attr_accessor :host
  attr_accessor :port
  attr_accessor :command
  attr_accessor :timeout
  attr_accessor :response_handler
  attr_accessor :token
  attr_accessor :status
  attr_reader :i18n

  # Returns a unique (singleton) instance of the authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @param force [Boolean] If to force recreation of the instance.
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
  def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
    @instance = nil if force
    @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
    @instance
  end

  # Creates a new authorizer.
  #
  # @param host [String] The host address on which listening for replies. Default is `localhost`.
  # @param port [Fixnum] The port on which listening for replies. Default is `7772`.
  # @param command [String|nil] The command to open the URL. `{{URL}}` is replaced with the specified URL. Default is `open "{{URL}}"`.
  # @param timeout [Fixnum] The amount of seconds to wait for response from the remote endpoint before returning a failure.
  #   Default is `0`, which means *disabled*.
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
  # @return [Authorizer] The new authorizer.
  def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
    @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
    @host = host.ensure_string
    @port = port.to_integer
    @command = command.ensure_string
    @timeout = timeout.to_integer
    @response_handler = response_handler
    @token = nil
    @status = :waiting

    sanitize_arguments
    self
  end

  # Starts the authorization flow.
  #
  # @param url [String] The URL where to send the user to start authorization.
  # @param append_callback [Boolean] If to append the callback to the url using `oauth_callback` parameter.
  # @return [Boolean] `true` if authorization succeeded, `false` otherwise.
  def authorize(url, append_callback = true)
    url = Addressable::URI.parse(url)
    url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

    @url = url.to_s
    @status = :waiting
    @token = nil

    begin
      perform_request
      process_response
    rescue => e
      @status = :failed
      raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
    end

    succeeded?
  end

  # Returns the callback_url for this authorizer.
  #
  # @return [String] The callback_url for this authorizer.
  def callback_url
    Addressable::URI.new(scheme: "http", host: host, port: port).to_s
  end

  # Returns the response handler for the authorizer.
  #
  def response_handler
    @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
  end

  # Checks if authentication succeeded.
  #
  # @return [Boolean] `true` if authorization succeeded, `false otherwise`.
  def succeeded?
    @status == :succeeded
  end

  # Checks if authentication was denied.
  #
  # @return [Boolean] `true` if authorization was denied, `false otherwise`.
  def denied?
    @status == :denied
  end

  # Checks if authentication failed (which means that some error occurred).
  #
  # @return [Boolean] `true` if authorization failed, `false otherwise`.
  def failed?
    @status == :failed
  end

  # Checks if authentication is still pending.
  #
  # @return [Boolean] `true` if authorization is still pending, `false otherwise`.
  def waiting?
    @status == :waiting
  end

  private

  # :nodoc:
  def sanitize_arguments
    @host = "localhost" if @host.blank?
    @port = 7772 if @port.to_integer < 1
    @command = "open \"{{URL}}\"" if @command.blank?
    @timeout = 0 if @timeout < 0
  end

  # :nodoc:
  def perform_request
    # Open the oAuth endpoint into the browser
    Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
  rescue => e
    raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
  end

  # :nodoc:
  def process_response
    server = Thread.new do
      Clavem::Server.new(self)
    end

    @timeout > 0 ? server.join(@timeout) : server.join
  rescue Interrupt
    raise(Clavem::Exceptions::Failure, @i18n.interrupted)
  end
end

Class Method Details

.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler) ⇒ Authorizer

Returns a unique (singleton) instance of the authorizer.


60
61
62
63
64
# File 'lib/clavem/authorizer.rb', line 60

def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
  @instance = nil if force
  @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
  @instance
end

Instance Method Details

#authorize(url, append_callback = true) ⇒ Boolean

Starts the authorization flow.


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/clavem/authorizer.rb', line 94

def authorize(url, append_callback = true)
  url = Addressable::URI.parse(url)
  url.query_values = (url.query_values || {}).merge({oauth_callback: callback_url}) if append_callback

  @url = url.to_s
  @status = :waiting
  @token = nil

  begin
    perform_request
    process_response
  rescue => e
    @status = :failed
    raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
  end

  succeeded?
end

#callback_urlString

Returns the callback_url for this authorizer.


116
117
118
# File 'lib/clavem/authorizer.rb', line 116

def callback_url
  Addressable::URI.new(scheme: "http", host: host, port: port).to_s
end

#denied?Boolean

Checks if authentication was denied.


136
137
138
# File 'lib/clavem/authorizer.rb', line 136

def denied?
  @status == :denied
end

#failed?Boolean

Checks if authentication failed (which means that some error occurred).


143
144
145
# File 'lib/clavem/authorizer.rb', line 143

def failed?
  @status == :failed
end

#succeeded?Boolean

Checks if authentication succeeded.


129
130
131
# File 'lib/clavem/authorizer.rb', line 129

def succeeded?
  @status == :succeeded
end

#waiting?Boolean

Checks if authentication is still pending.


150
151
152
# File 'lib/clavem/authorizer.rb', line 150

def waiting?
  @status == :waiting
end