Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sub uri and lambda uri in Shield::Middleware #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/shield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

module Shield
class Middleware
attr :url

def initialize(app, url = "/login")
@app = app
@url = url
Expand All @@ -14,15 +12,23 @@ def call(env)
tuple = @app.call(env)

if tuple[0] == 401
[302, headers(env["SCRIPT_NAME"] + env["PATH_INFO"]), []]
[302, headers(url(env), env["SCRIPT_NAME"] + env["PATH_INFO"]), []]
else
tuple
end
end

def url(env)
if @url.respond_to? :call
@url.call(env)
else
env["SCRIPT_NAME"] + @url
end
end

private
def headers(path)
{ "Location" => "%s?return=%s" % [url, encode(path)],
def headers(redirect_url, return_path)
{ "Location" => "%s?return=%s" % [redirect_url, encode(return_path)],
"Content-Type" => "text/html",
"Content-Length" => "0"
}
Expand Down
8 changes: 8 additions & 0 deletions test/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@
assert_equal 302, status
assert_equal "/login?return=%2Fsecured", headers["Location"]
end

test do
env = { "PATH_INFO" => "/secured", "SCRIPT_NAME" => "/suburi" }
status, headers, body = Cuba.call(env)

assert_equal 302, status
assert_equal "/suburi/login?return=%2Fsuburi%2Fsecured", headers["Location"]
end
30 changes: 30 additions & 0 deletions test/middleware_lambda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative "helper"
require_relative "user"
require "cuba"

Cuba.use Rack::Session::Cookie, secret: "foo"
Cuba.use Shield::Middleware, lambda { |env| env["SCRIPT_NAME"] + "/login" }

Cuba.plugin Shield::Helpers

Cuba.define do
on "secured" do
if not authenticated(User)
halt [401, { "Content-Type" => "text/html" }, []]
end

res.write "You're in"
end

on "foo" do
puts env.inspect
end
end

test do
env = { "PATH_INFO" => "/secured", "SCRIPT_NAME" => "/lambda" }
status, headers, body = Cuba.call(env)

assert_equal 302, status
assert_equal "/lambda/login?return=%2Flambda%2Fsecured", headers["Location"]
end
2 changes: 1 addition & 1 deletion test/nested.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Cuba.plugin Shield::Helpers

class Admin < Cuba
use Shield::Middleware, "/admin/login"
use Shield::Middleware, "/login"

define do
on "login" do
Expand Down