Skip to content

Commit

Permalink
Add alias to_ary due to it being removed in rack update (#90)
Browse files Browse the repository at this point in the history
* Relies on Rack::Response to handle unauthorized response.
  • Loading branch information
carmensea authored Jan 16, 2020
1 parent ae00090 commit bfefb13
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
20 changes: 8 additions & 12 deletions lib/stitches/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ module Stitches
# ApiClient that it maps to.
class ApiKey < Stitches::AllowlistMiddleware

def initialize(app,options = {})
super(app,options)
@realm = rails_app_module
end

protected

def do_call(env)
Expand All @@ -45,13 +40,13 @@ def do_call(env)
env[@configuration.env_var_to_hold_api_client] = client
@app.call(env)
else
UnauthorizedResponse.new("key invalid",@realm,@configuration.custom_http_auth_scheme)
unauthorized_response("key invalid")
end
else
UnauthorizedResponse.new("bad authorization type",@realm,@configuration.custom_http_auth_scheme)
unauthorized_response("bad authorization type")
end
else
UnauthorizedResponse.new("no authorization header",@realm,@configuration.custom_http_auth_scheme)
unauthorized_response("no authorization header")
end
end

Expand All @@ -68,10 +63,11 @@ def rails_app_module
parent.to_s
end

class UnauthorizedResponse < Rack::Response
def initialize(reason,realm,custom_http_auth_scheme)
super("Unauthorized - #{reason}", 401, { "WWW-Authenticate" => "#{custom_http_auth_scheme} realm=#{realm}" })
end
def unauthorized_response(reason)
status = 401
body = "Unauthorized - #{reason}"
header = { "WWW-Authenticate" => "#{@configuration.custom_http_auth_scheme} realm=#{rails_app_module}" }
Rack::Response.new(body, status, header).finish
end

end
Expand Down
11 changes: 6 additions & 5 deletions lib/stitches/valid_mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ def do_call(env)
if accept =~ %r{application/json} && accept =~ %r{version=\d+}
@app.call(env)
else
NotAcceptableResponse.new(accept)
not_acceptable_response(accept)
end
end

private

class NotAcceptableResponse < Rack::Response
def initialize(accept_header)
super("Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version", 406)
end
def not_acceptable_response(accept_header)
status = 406
body = "Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version"
header = { "WWW-Authenticate" => accept_header }
Rack::Response.new(body, status, header).finish
end

end
Expand Down
10 changes: 6 additions & 4 deletions spec/api_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ def self.column_names

shared_examples "an unauthorized response" do
it "returns a 401" do
expect(@response.status).to eq(401)
status, _headers, _body = @response
expect(status).to eq(401)
end
it "sets the proper header" do
expect(@response.headers["WWW-Authenticate"]).to eq("MyAwesomeInternalScheme realm=MyApp")
_status, headers, _body = @response
expect(headers["WWW-Authenticate"]).to eq("MyAwesomeInternalScheme realm=MyApp")
end
it "stops the call chain preventing anything from happening" do
expect(app).not_to have_received(:call)
end
it "sends a reasonable message" do
expect(@response.body).to eq([expected_body])
_status, _headers, body = @response
expect(body).to eq([expected_body])
end
end

Expand Down Expand Up @@ -166,7 +169,6 @@ def self.column_names
context "unauthorized responses" do
before do
@response = middleware.call(env)
@response.finish
end
context "invalid key" do
let(:env) {
Expand Down
7 changes: 4 additions & 3 deletions spec/valid_mime_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

shared_examples "an unacceptable response" do
it "returns a 406" do
expect(@response.status).to eq(406)
status, _headers, _body = @response
expect(status).to eq(406)
end
it "stops the call chain preventing anything from happening" do
expect(app).not_to have_received(:call)
end
it "sends a reasonable message" do
expect(@response.body.first).to match(/didn't have the right mime type or version number. We only accept application\/json/)
_status, _headers, body = @response
expect(body.first).to match(/didn't have the right mime type or version number. We only accept application\/json/)
end
end

Expand Down Expand Up @@ -133,7 +135,6 @@
context "unacceptable responses" do
before do
@response = middleware.call(env)
@response.finish
end
context "no header" do
let(:env) {
Expand Down

0 comments on commit bfefb13

Please sign in to comment.