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

Make endpoint and helpers available in auth blocks #531

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ bin
tags

## PROJECT::SPECIFIC
.project
20 changes: 17 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def call(env)
end

def call!(env)
extend helpers

env['api.endpoint'] = self
if options[:app]
options[:app].call(env)
Expand Down Expand Up @@ -372,7 +374,6 @@ def run(env)
@params = @request.params
@headers = @request.headers

extend helpers
cookies.read(@request)

run_filters befores
Expand Down Expand Up @@ -426,8 +427,21 @@ def build_middleware
end
end

b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic
b.use Rack::Auth::Digest::MD5, settings[:auth][:realm], settings[:auth][:opaque], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_digest
if settings[:auth]
auth_proc = settings[:auth][:proc]
auth_proc_context = self
auth_middleware = {
http_basic: { class: Rack::Auth::Basic, args: [settings[:auth][:realm]] },
http_digest: { class: Rack::Auth::Digest::MD5, args: [settings[:auth][:realm], settings[:auth][:opaque]] }
}[settings[:auth][:type]]

# evaluate auth proc in context of endpoint
if auth_middleware
b.use auth_middleware[:class], *auth_middleware[:args] do |*args|
auth_proc_context.instance_exec(*args, &auth_proc)
end
end
end

if settings[:version]
b.use Grape::Middleware::Versioner.using(settings[:version_options][:using]),
Expand Down
47 changes: 46 additions & 1 deletion spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def before
end
end
end
describe '.basic' do
describe '.http_basic' do
it 'protects any resources on the same scope' do
subject.http_basic do |u, p|
u == 'allow'
Expand Down Expand Up @@ -913,6 +913,51 @@ def before
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
last_response.status.should eql 200
end

it 'has access to the current endpoint' do
basic_auth_context = nil

subject.http_basic do |u, p|
basic_auth_context = self

u == 'allow'
end

subject.get(:hello) { "Hello, world." }
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
basic_auth_context.should be_an_instance_of(Grape::Endpoint)
end

it 'has access to helper methods' do
subject.helpers do
def authorize(u, p)
u == 'allow' && p == 'whatever'
end
end

subject.http_basic do |u, p|
authorize(u, p)
end

subject.get(:hello) { "Hello, world." }
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
last_response.status.should eql 200
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('disallow', 'whatever')
last_response.status.should eql 401
end

it 'can set instance variables accessible to routes' do
subject.http_basic do |u, p|
@hello = "Hello, world."

u == 'allow'
end

subject.get(:hello) { @hello }
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
last_response.status.should eql 200
last_response.body.should eql "Hello, world."
end
end

describe '.logger' do
Expand Down