summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2016-12-02 11:25:35 +0000
committerazul <azul@riseup.net>2016-12-02 11:25:35 +0000
commit8bfd27e0c851e9786110be4e6485635f769d853f (patch)
treeaed9cc67f5711352e86d8314a4718c777a23cc43
parent244d8a4702f98619078c0e09e485d7e1944e5013 (diff)
parent0aa101524270c0c398fe17c287d51927f810e499 (diff)
Merge branch 'bugfix/couch-auth' into 'master'
bugfix: use user:password@ prefix in http basic auth See merge request !9
-rw-r--r--lib/nickserver/adapters/celluloid_http.rb21
-rw-r--r--lib/nickserver/adapters/http.rb24
-rw-r--r--lib/nickserver/adapters/local.rb8
-rw-r--r--lib/nickserver/adapters/remote.rb4
-rw-r--r--lib/nickserver/couch_db/source.rb10
-rw-r--r--test/unit/adapters/couch_db.rb15
-rw-r--r--test/unit/adapters/local_test.rb5
-rw-r--r--test/unit/couch_db/source_unit_test.rb11
8 files changed, 48 insertions, 50 deletions
diff --git a/lib/nickserver/adapters/celluloid_http.rb b/lib/nickserver/adapters/celluloid_http.rb
index 92c5c53..62f905f 100644
--- a/lib/nickserver/adapters/celluloid_http.rb
+++ b/lib/nickserver/adapters/celluloid_http.rb
@@ -1,28 +1,19 @@
-require 'nickserver/adapters'
-require 'nickserver/config'
+require 'nickserver/adapters/http'
silence_warnings do
require 'celluloid/io'
end
-require 'http'
module Nickserver::Adapters
- class CelluloidHttp
+ class CelluloidHttp < Http
silence_warnings do
include Celluloid::IO
end
- def get(url, options = {})
- response = HTTP.get url,
- params: options[:query],
- ssl_context: ctx,
- ssl_socket_class: Celluloid::IO::SSLSocket
- return response.code, response.to_s
- end
+ protected
- def ctx
- OpenSSL::SSL::SSLContext.new.tap do |ctx|
- ctx.ca_file = Nickserver::Config.hkp_ca_file
- end
+ def default_options
+ super.merge ssl_socket_class: Celluloid::IO::SSLSocket
end
+
end
end
diff --git a/lib/nickserver/adapters/http.rb b/lib/nickserver/adapters/http.rb
index b0ba728..636aceb 100644
--- a/lib/nickserver/adapters/http.rb
+++ b/lib/nickserver/adapters/http.rb
@@ -6,12 +6,30 @@ module Nickserver::Adapters
class Http
def get(url, options = {})
- response = HTTP.get url,
- params: options[:query],
- ssl_context: ctx
+ url = HTTP::URI.parse url.to_s
+ response = get_with_auth url, params: options[:query]
return response.code, response.to_s
end
+ protected
+
+ def get_with_auth(url, options)
+ options = default_options.merge options
+ http_with_basic_auth(url).get url, options
+ end
+
+ def http_with_basic_auth(url)
+ if url.password && (url.password != '')
+ HTTP.basic_auth(user: url.user, pass: url.password)
+ else
+ HTTP
+ end
+ end
+
+ def default_options
+ { ssl_context: ctx }
+ end
+
def ctx
OpenSSL::SSL::SSLContext.new.tap do |ctx|
ctx.ca_file = Nickserver::Config.hkp_ca_file
diff --git a/lib/nickserver/adapters/local.rb b/lib/nickserver/adapters/local.rb
deleted file mode 100644
index d6210c3..0000000
--- a/lib/nickserver/adapters/local.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-require 'nickserver/adapters'
-
-class Nickserver::Adapters::Local
-
- def query(nick)
- end
-
-end
diff --git a/lib/nickserver/adapters/remote.rb b/lib/nickserver/adapters/remote.rb
deleted file mode 100644
index e12bd26..0000000
--- a/lib/nickserver/adapters/remote.rb
+++ /dev/null
@@ -1,4 +0,0 @@
-require 'nickserver/adapters'
-
-class Nickserver::Adapters::Remote
-end
diff --git a/lib/nickserver/couch_db/source.rb b/lib/nickserver/couch_db/source.rb
index 7c3ad95..dd29c2a 100644
--- a/lib/nickserver/couch_db/source.rb
+++ b/lib/nickserver/couch_db/source.rb
@@ -6,17 +6,27 @@ require 'nickserver/couch_db/response'
require 'nickserver/config'
module Nickserver::CouchDB
+ class Error < StandardError; end
+
class Source < Nickserver::Source
VIEW = '/_design/Identity/_view/pgp_key_by_email'
+ UNEXPECTED_RESPONSE_CODES = [401, 500]
def query(nick)
status, body = adapter.get url, query: query_for(nick)
+ handle_unexpected_responses(status, body)
Response.new(nick, status: status, body: body)
end
protected
+ def handle_unexpected_responses(status, body)
+ if UNEXPECTED_RESPONSE_CODES.include? status
+ raise Error.new("Couch responded with #{status}: #{body}")
+ end
+ end
+
def url
Nickserver::Config.couch_url + VIEW
end
diff --git a/test/unit/adapters/couch_db.rb b/test/unit/adapters/couch_db.rb
deleted file mode 100644
index 339e4f4..0000000
--- a/test/unit/adapters/couch_db.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-require 'testhelper'
-require 'nickserver/adapters/couch_db'
-
-class Nickserver::Adapters::CouchDBTest < Minitest::Test
-
- def test_query_404
- adapter.query(nil) do |status, content|
- assert_equal 404, status
- end
- end
-
- def adapter
- @adapter ||= Nickserver::Adapters::CouchDB.new
- end
-end
diff --git a/test/unit/adapters/local_test.rb b/test/unit/adapters/local_test.rb
deleted file mode 100644
index 97d394f..0000000
--- a/test/unit/adapters/local_test.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'test_helper'
-require 'nickserver/adapters/local'
-
-class Nickserver::Adapters::LocalTest < Minitest::Test
-end
diff --git a/test/unit/couch_db/source_unit_test.rb b/test/unit/couch_db/source_unit_test.rb
index fd07808..e5a4f43 100644
--- a/test/unit/couch_db/source_unit_test.rb
+++ b/test/unit/couch_db/source_unit_test.rb
@@ -12,5 +12,16 @@ module Nickserver::CouchDB
Source.new(adapter).query address
adapter.verify
end
+
+ def test_401
+ address = "nick@domain.tl"
+ adapter = Minitest::Mock.new
+ adapter.expect :get, [401, nil],
+ [String, {query: { reduce: "false", key: "\"#{address}\"" }}]
+ assert_raises Error do
+ Source.new(adapter).query address
+ end
+ adapter.verify
+ end
end
end