clavem 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@
6
6
  <title>
7
7
  Top Level Namespace
8
8
 
9
- &mdash; Documentation by YARD 0.8.7.4
9
+ &mdash; Documentation by YARD 0.8.7.6
10
10
 
11
11
  </title>
12
12
 
@@ -103,9 +103,9 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sun Apr 6 15:36:21 2014 by
106
+ Generated on Wed Mar 30 10:47:20 2016 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
- 0.8.7.4 (ruby-2.1.0).
108
+ 0.8.7.6 (ruby-2.3.0).
109
109
  </div>
110
110
 
111
111
  </body>
data/lib/clavem.rb CHANGED
@@ -4,9 +4,11 @@
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
- require "lazier"
8
7
  require "addressable/uri"
8
+ require "socket"
9
+ require "lazier"
10
+ require "bovem/i18n"
9
11
 
10
- require "clavem/version" if !defined?(Clavem::Version)
12
+ require "clavem/version" unless defined?(Clavem::Version)
11
13
  require "clavem/server"
12
- require "clavem/authorizer"
14
+ require "clavem/authorizer"
@@ -34,10 +34,9 @@ module Clavem
34
34
  # @return [String] The token obtained by the remote endpoint.
35
35
  # @attribute status
36
36
  # @return [Symbol] The status of the request. Can be `:succeeded`, `:denied`, `:failed` and `:waiting`.
37
- # @attribute :i18n
38
- # @return [R18N::Translation] A localizer object.
37
+ # @attribute [r] :i18n
38
+ # @return [Bovem::I18n] A localizer object.
39
39
  class Authorizer
40
- include R18n::Helpers
41
40
  attr_accessor :url
42
41
  attr_accessor :host
43
42
  attr_accessor :port
@@ -46,7 +45,7 @@ module Clavem
46
45
  attr_accessor :response_handler
47
46
  attr_accessor :token
48
47
  attr_accessor :status
49
- attr_accessor :i18n
48
+ attr_reader :i18n
50
49
 
51
50
  # Returns a unique (singleton) instance of the authorizer.
52
51
  #
@@ -58,9 +57,9 @@ module Clavem
58
57
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
59
58
  # @param force [Boolean] If to force recreation of the instance.
60
59
  # @return [Authorizer] The unique (singleton) instance of the authorizer.
61
- def self.instance(host = "localhost", port = 7772, command = nil, timeout = 0, force = false, &response_handler)
60
+ def self.instance(host: "localhost", port: 7772, command: nil, timeout: 0, force: false, &response_handler)
62
61
  @instance = nil if force
63
- @instance ||= Clavem::Authorizer.new(host, port, command, timeout, &response_handler)
62
+ @instance ||= Clavem::Authorizer.new(host: host, port: port, command: command, timeout: timeout, &response_handler)
64
63
  @instance
65
64
  end
66
65
 
@@ -73,8 +72,8 @@ module Clavem
73
72
  # Default is `0`, which means *disabled*.
74
73
  # @param response_handler [Proc] A Ruby block to handle response and check for success. See {#response_handler}.
75
74
  # @return [Authorizer] The new authorizer.
76
- def initialize(host = "localhost", port = 7772, command = nil, timeout = 0, &response_handler)
77
- @i18n = self.localize
75
+ def initialize(host: "localhost", port: 7772, command: nil, timeout: 0, &response_handler)
76
+ @i18n = Bovem::I18n.new(root: "clavem.errors", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
78
77
  @host = host.ensure_string
79
78
  @port = port.to_integer
80
79
  @command = command.ensure_string
@@ -105,7 +104,7 @@ module Clavem
105
104
  process_response
106
105
  rescue => e
107
106
  @status = :failed
108
- raise Clavem::Exceptions::Failure.new(@i18n.errors.response_failure(e.to_s))
107
+ raise(Clavem::Exceptions::Failure, @i18n.response_failure(e.to_s))
109
108
  end
110
109
 
111
110
  succeeded?
@@ -121,16 +120,7 @@ module Clavem
121
120
  # Returns the response handler for the authorizer.
122
121
  #
123
122
  def response_handler
124
- @response_handler || Proc.new {|querystring| (querystring || {})["oauth_token"] }
125
- end
126
-
127
- # Set the current locale for messages.
128
- #
129
- # @param locale [String] The new locale. Default is the current system locale.
130
- # @return [R18n::Translation] The new translations object.
131
- def localize(locale = nil)
132
- @i18n_locales_path ||= ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
133
- R18n::I18n.new([locale || :en, ENV["LANG"], R18n::I18n.system_locale].compact, @i18n_locales_path).t.clavem
123
+ @response_handler || ->(querystring) { (querystring || {})["oauth_token"] }
134
124
  end
135
125
 
136
126
  # Checks if authentication succeeded.
@@ -162,35 +152,32 @@ module Clavem
162
152
  end
163
153
 
164
154
  private
165
- # sanitize_arguments
166
- def sanitize_arguments
167
- @host = "localhost" if @host.blank?
168
- @port = 7772 if @port.to_integer < 1
169
- @command = "open \"{{URL}}\"" if @command.blank?
170
- @timeout = 0 if @timeout < 0
171
- end
172
155
 
173
- # Performs the authentication request.
174
- def perform_request
175
- # Open the oAuth endpoint into the browser
176
- begin
177
- Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
178
- rescue => e
179
- raise Clavem::Exceptions::Failure.new(@i18n.errors.open_failure(@url.ensure_string, e.to_s))
180
- end
181
- end
156
+ # :nodoc:
157
+ def sanitize_arguments
158
+ @host = "localhost" if @host.blank?
159
+ @port = 7772 if @port.to_integer < 1
160
+ @command = "open \"{{URL}}\"" if @command.blank?
161
+ @timeout = 0 if @timeout < 0
162
+ end
163
+
164
+ # :nodoc:
165
+ def perform_request
166
+ # Open the oAuth endpoint into the browser
167
+ Kernel.system(@command.gsub("{{URL}}", @url.ensure_string))
168
+ rescue => e
169
+ raise(Clavem::Exceptions::Failure, @i18n.open_failure(@url.ensure_string, e.to_s))
170
+ end
182
171
 
183
- # Processes the authentication response.
184
- def process_response
185
- begin
186
- server = Thread.new do
187
- Clavem::Server.new(self)
188
- end
189
-
190
- @timeout > 0 ? server.join(@timeout) : server.join
191
- rescue Interrupt
192
- raise Clavem::Exceptions::Failure.new(@i18n.errors.interrupted)
193
- end
172
+ # :nodoc:
173
+ def process_response
174
+ server = Thread.new do
175
+ Clavem::Server.new(self)
194
176
  end
177
+
178
+ @timeout > 0 ? server.join(@timeout) : server.join
179
+ rescue Interrupt
180
+ raise(Clavem::Exceptions::Failure, @i18n.interrupted)
181
+ end
195
182
  end
196
- end
183
+ end
data/lib/clavem/server.rb CHANGED
@@ -8,23 +8,24 @@ module Clavem
8
8
  # A class to handle oAuth callbacks on the browser via HTTP.
9
9
  class Server
10
10
  # The template to send to the browser.
11
- TEMPLATE = <<-EOTEMPLATE
12
- <html>
13
- <head>
14
- <title>Clavem</title>
15
- <script type="text/javascript" charset="utf8">(function(){ window.open("", "_self", ""); window.close(); })();</script>
16
- </head>
17
- <body>
18
- %s
19
- </body>
20
- </html>
11
+ TEMPLATE = <<~EOTEMPLATE
12
+ <html>
13
+ <head>
14
+ <title>Clavem</title>
15
+ </head>
16
+ <body>
17
+ %s
18
+ </body>
19
+ </html>
21
20
  EOTEMPLATE
21
+ .freeze
22
22
 
23
23
  # Creates a new server.
24
24
  #
25
25
  # @param authorizer [Authorizer] The authorizer of this server.
26
26
  def initialize(authorizer)
27
27
  @authorizer = authorizer
28
+ @i18n = Bovem::I18n.new(root: "clavem", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
28
29
 
29
30
  process_http_request
30
31
  end
@@ -44,35 +45,36 @@ module Clavem
44
45
  # Handle the token
45
46
  token = @authorizer.response_handler.call(querystring)
46
47
 
47
- if token then
48
- @authorizer.token = token
49
- @authorizer.status = :succeeded
50
- else
51
- @authorizer.status = :denied
52
- end
53
-
48
+ update_authorizer(token)
54
49
  server.close
55
50
  end
56
51
 
57
52
  private
58
- # Creates the server.
59
- #
60
- # @return [TCPServer] The TCP server
61
- def create_server
62
- server = TCPServer.new(@authorizer.host, @authorizer.port)
63
- server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
64
- server.setsockopt(:SOCKET, :REUSEADDR, 1)
65
- server
66
- end
67
53
 
68
- # Sends the response to the client.
69
- #
70
- # @param socket [TCPSocket] The socket to the client.
71
- def send_response(socket)
72
- response = TEMPLATE % [@authorizer.i18n.template]
73
- socket.print(["HTTP/1.1 200 OK", "Content-Type: text/html; charset=utf-8", "Content-Length: #{response.bytesize}", "Connection: close"].join("\r\n"))
74
- socket.print("\r\n\r\n" + response)
75
- socket.close
54
+ # :nodoc:
55
+ def create_server
56
+ server = TCPServer.new(@authorizer.host, @authorizer.port)
57
+ server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
58
+ server.setsockopt(:SOCKET, :REUSEADDR, 1)
59
+ server
60
+ end
61
+
62
+ # :nodoc:
63
+ def send_response(socket)
64
+ response = TEMPLATE % [@i18n.template]
65
+ socket.print(["HTTP/1.1 200 OK", "Content-Type: text/html; charset=utf-8", "Content-Length: #{response.bytesize}", "Connection: close"].join("\r\n"))
66
+ socket.print("\r\n\r\n" + response)
67
+ socket.close
68
+ end
69
+
70
+ # :nodoc:
71
+ def update_authorizer(token)
72
+ if token
73
+ @authorizer.token = token
74
+ @authorizer.status = :succeeded
75
+ else
76
+ @authorizer.status = :denied
76
77
  end
78
+ end
77
79
  end
78
- end
80
+ end
@@ -13,7 +13,7 @@ module Clavem
13
13
  MAJOR = 2
14
14
 
15
15
  # The minor version.
16
- MINOR = 1
16
+ MINOR = 2
17
17
 
18
18
  # The patch version.
19
19
  PATCH = 0
data/locales/en.yml CHANGED
@@ -5,12 +5,13 @@
5
5
  #
6
6
 
7
7
  ---
8
+ en:
8
9
  clavem:
9
10
  errors:
10
- response_failure: "Cannot handle response: %1"
11
- open_failure: "Cannot open URL %1: %2"
11
+ response_failure: "Cannot handle response: %s"
12
+ open_failure: "Cannot open URL %s: %s"
12
13
  interrupted: "Authentication interrupted by the user."
13
- template: "This window will close in few moments. If this doesn't happens you can close it by yourself, this won't cause any problem."
14
+ template: "The authentication process on the URL has ended. You can now close this window and continue in the console."
14
15
  application_description: "A local callback server for oAuth web-flow."
15
16
  application_help_url: "The URL where to send the user to start authorization."
16
17
  application_help_host: "The host address on which listening for the reply. Default is localhost."
@@ -21,7 +22,7 @@
21
22
  application_help_quiet: "Do not print anything but errors."
22
23
  application_meta_port: "PORT"
23
24
  application_meta_command: "COMMAND"
24
- cli_running: "Clavem is waiting for a response at URL {mark=bright}%1{/mark} ..."
25
- cli_obtaining: "Obtaining token from URL {mark=bright}%1{/mark}."
26
- cli_succeeded: "Obtained access token is: {mark=bright}%1{/mark}"
25
+ cli_running: "Clavem is waiting for a response at URL {mark=bright}%s{/mark} ..."
26
+ cli_obtaining: "Obtaining token from URL {mark=bright}%s{/mark}."
27
+ cli_succeeded: "Obtained access token is: {mark=bright}%s{/mark}"
27
28
  cli_failed: "No access token obtained."
data/locales/it.yml CHANGED
@@ -5,12 +5,13 @@
5
5
  #
6
6
 
7
7
  ---
8
+ it:
8
9
  clavem:
9
10
  errors:
10
- response_failure: "Impossibile gestire una risposta: %1"
11
- open_failure: "Impossible aprire l'URL %1: %2"
11
+ response_failure: "Impossibile gestire una risposta: %s"
12
+ open_failure: "Impossible aprire l'URL %s: %s"
12
13
  interrupted: "Autenticazione interrotta dall'utente."
13
- template: "Questa finestra si chiuderà in pochi momenti. Se non succede puoi chiuderla da solo, ciò non causerà alcun problema."
14
+ template: "Il processo di autenticazione sull'URL si è concluso. Puoi ora chiudere questa finestra e continuare nel terminale."
14
15
  application_description: "Un server callback locale per il web-flow oAuth."
15
16
  application_help_url: "L'URL dove mandare l'utente per iniziare l'autenticazione."
16
17
  application_help_host: "L'indirizzo host dove rimanere in ascolto per la risposta. Il default è localhost."
@@ -21,7 +22,7 @@
21
22
  application_help_quiet: "Non mostrare nulla tranne che gli errori."
22
23
  application_meta_port: "PORTA"
23
24
  application_meta_command: "COMANDO"
24
- cli_running: "Clavem è in attesa di una risposta all'URL {mark=bright}%1{/mark} ..."
25
- cli_obtaining: "Ottengo il token dall'URL {mark=bright}%1{/mark}."
26
- cli_succeeded: "Il token di accesso ottenuto è: {mark=bright}%1{/mark}"
25
+ cli_running: "Clavem è in attesa di una risposta all'URL {mark=bright}%s{/mark} ..."
26
+ cli_obtaining: "Ottengo il token dall'URL {mark=bright}%s{/mark}."
27
+ cli_succeeded: "Il token di accesso ottenuto è: {mark=bright}%s{/mark}"
27
28
  cli_failed: "Nessun token di accesso ottenuto."
@@ -11,20 +11,20 @@ describe Clavem::Authorizer do
11
11
 
12
12
  describe ".instance" do
13
13
  it "should call .new with the passed arguments" do
14
- expect(::Clavem::Authorizer).to receive(:new).with("HOST", "PORT", "COMMAND", "TIMEOUT")
15
- ::Clavem::Authorizer.instance("HOST", "PORT", "COMMAND", "TIMEOUT")
14
+ expect(::Clavem::Authorizer).to receive(:new).with(host: "HOST", port: "PORT", command: "COMMAND", timeout: "TIMEOUT")
15
+ ::Clavem::Authorizer.instance(host: "HOST", port: "PORT", command: "COMMAND", timeout: "TIMEOUT")
16
16
  end
17
17
 
18
18
  it "should return the same instance" do
19
19
  allow(::Clavem::Authorizer).to receive(:new) { Time.now }
20
- authorizer = ::Clavem::Authorizer.instance("FIRST")
21
- expect(::Clavem::Authorizer.instance("SECOND")).to be(authorizer)
20
+ authorizer = ::Clavem::Authorizer.instance(host: "FIRST")
21
+ expect(::Clavem::Authorizer.instance(host: "SECOND")).to be(authorizer)
22
22
  end
23
23
 
24
24
  it "should return a new instance if requested to" do
25
25
  allow(::Clavem::Authorizer).to receive(:new) { Time.now }
26
- authorizer = ::Clavem::Authorizer.instance("FIRST")
27
- expect(::Clavem::Authorizer.instance("HOST", "PORT", "COMMAND", "TIMEOUT", true)).not_to be(authorizer)
26
+ authorizer = ::Clavem::Authorizer.instance(host: "FIRST")
27
+ expect(::Clavem::Authorizer.instance(host: "HOST", port: "PORT", command: "COMMAND", timeout: "TIMEOUT", force: true)).not_to be(authorizer)
28
28
  end
29
29
  end
30
30
 
@@ -39,7 +39,7 @@ describe Clavem::Authorizer do
39
39
  end
40
40
 
41
41
  it "should assign arguments" do
42
- authorizer = ::Clavem::Authorizer.new("HOST", 7773, "COMMAND", 2) do end
42
+ authorizer = ::Clavem::Authorizer.new(host: "HOST", port: 7773, command: "COMMAND", timeout: 2) do end
43
43
  expect(authorizer.host).to eq("HOST")
44
44
  expect(authorizer.port).to eq(7773)
45
45
  expect(authorizer.command).to eq("COMMAND")
@@ -48,7 +48,7 @@ describe Clavem::Authorizer do
48
48
  end
49
49
 
50
50
  it "should correct wrong arguments" do
51
- authorizer = ::Clavem::Authorizer.new("IP", -10, "", -1)
51
+ authorizer = ::Clavem::Authorizer.new(host: "IP", port: -10, command: "", timeout: -1)
52
52
  expect(authorizer.port).to eq(7772)
53
53
  expect(authorizer.timeout).to eq(0)
54
54
  end
@@ -69,7 +69,7 @@ describe Clavem::Authorizer do
69
69
  allow(subject).to receive(:perform_request)
70
70
  allow(subject).to receive(:process_response)
71
71
  subject.authorize("URL")
72
- expect(subject.waiting?).to be_true
72
+ expect(subject.waiting?).to be_truthy
73
73
  end
74
74
 
75
75
  it "should make a request" do
@@ -96,16 +96,16 @@ describe Clavem::Authorizer do
96
96
  allow(subject).to receive(:perform_request)
97
97
 
98
98
  allow(subject).to receive(:process_response) { subject.status = :succeeded }
99
- expect(subject.authorize("URL")).to be_true
99
+ expect(subject.authorize("URL")).to be_truthy
100
100
 
101
101
  allow(subject).to receive(:process_response) { subject.status = :failed }
102
- expect(subject.authorize("URL")).to be_false
102
+ expect(subject.authorize("URL")).to be_falsey
103
103
  end
104
104
 
105
105
  it "should handle errors" do
106
106
  allow(Kernel).to receive(:system).and_raise(RuntimeError.new)
107
107
  expect { subject.authorize("URL") }.to raise_error(Clavem::Exceptions::Failure)
108
- expect(subject.failed?).to be_true
108
+ expect(subject.failed?).to be_truthy
109
109
  end
110
110
 
111
111
  it "should handle timeouts" do
@@ -120,14 +120,14 @@ describe Clavem::Authorizer do
120
120
  allow(Kernel).to receive(:system)
121
121
  allow(Clavem::Server).to receive(:new).and_raise(Interrupt)
122
122
  expect { subject.authorize("URL") }.to raise_error(Clavem::Exceptions::Failure)
123
- expect(subject.failed?).to be_true
123
+ expect(subject.failed?).to be_truthy
124
124
  end
125
125
  end
126
126
 
127
127
  describe "#callback_url" do
128
128
  it "should return the correct callback" do
129
129
  expect(::Clavem::Authorizer.new.callback_url).to eq("http://localhost:7772")
130
- expect(::Clavem::Authorizer.new("10.0.0.1", "80").callback_url).to eq("http://10.0.0.1:80")
130
+ expect(::Clavem::Authorizer.new(host: "10.0.0.1", port: "80").callback_url).to eq("http://10.0.0.1:80")
131
131
  end
132
132
  end
133
133
 
@@ -143,58 +143,39 @@ describe Clavem::Authorizer do
143
143
  end
144
144
  end
145
145
 
146
- describe ".localize" do
147
- it "should set the right locale path" do
148
- expect(subject.instance_variable_get(:@i18n_locales_path)).to eq(File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../../locales/"))
149
- subject.localize
150
- end
151
-
152
- it "should set using English if called without arguments" do
153
- authorizer = ::Clavem::Authorizer.new
154
- expect(R18n::I18n).to receive(:new).with([:en, ENV["LANG"], R18n::I18n.system_locale].compact, File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../../locales/")).and_call_original
155
- authorizer.localize
156
- end
157
-
158
- it "should set the requested locale" do
159
- authorizer = ::Clavem::Authorizer.new
160
- expect(R18n::I18n).to receive(:new).with([:it, ENV["LANG"], R18n::I18n.system_locale].compact, File.absolute_path(::Pathname.new(File.dirname(__FILE__)).to_s + "/../../locales/")).and_call_original
161
- authorizer.localize(:it)
162
- end
163
- end
164
-
165
146
  describe "#succeeded?" do
166
147
  it "should return the correct status" do
167
148
  subject.status = :other
168
- expect(subject.succeeded?).to be_false
149
+ expect(subject.succeeded?).to be_falsey
169
150
  subject.status = :succeeded
170
- expect(subject.succeeded?).to be_true
151
+ expect(subject.succeeded?).to be_truthy
171
152
  end
172
153
  end
173
154
 
174
155
  describe "#denied?" do
175
156
  it "should return the correct status" do
176
157
  subject.status = :other
177
- expect(subject.denied?).to be_false
158
+ expect(subject.denied?).to be_falsey
178
159
  subject.status = :denied
179
- expect(subject.denied?).to be_true
160
+ expect(subject.denied?).to be_truthy
180
161
  end
181
162
  end
182
163
 
183
164
  describe "#failed?" do
184
165
  it "should return the correct status" do
185
166
  subject.status = :other
186
- expect(subject.failed?).to be_false
167
+ expect(subject.failed?).to be_falsey
187
168
  subject.status = :failed
188
- expect(subject.failed?).to be_true
169
+ expect(subject.failed?).to be_truthy
189
170
  end
190
171
  end
191
172
 
192
173
  describe "#waiting?" do
193
174
  it "should return the correct status" do
194
175
  subject.status = :other
195
- expect(subject.waiting?).to be_false
176
+ expect(subject.waiting?).to be_falsey
196
177
  subject.status = :waiting
197
- expect(subject.waiting?).to be_true
178
+ expect(subject.waiting?).to be_truthy
198
179
  end
199
180
  end
200
181
  end