Skip to content

Commit e23069a

Browse files
joelvhdblock
authored andcommitted
Helpers are now available to auth middleware, executing in the context of the endpoint.
1 parent 33c91e1 commit e23069a

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ bin
4242
tags
4343

4444
## PROJECT::SPECIFIC
45+
.project

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Next Release
1010
* [#526](https://github.com/intridea/grape/pull/526): Allow specifying headers in `error!` - [@dblock](https://github.com/dblock).
1111
* [#523](https://github.com/intridea/grape/pull/523): Aliased `before` as `before_validation` - [@myitcv](https://github.com/myitcv).
1212
* [#527](https://github.com/intridea/grape/pull/527): `before_validation` now a distinct callback (supersedes [#523](https://github.com/intridea/grape/pull/523)) - [@myitcv](https://github.com/myitcv).
13+
* [#531](https://github.com/intridea/grape/pull/531): Helpers are now available to auth middleware, executing in the context of the endpoint - [@joelvh](https://github.com/joelvh).
1314
* Your contribution here.
1415

1516
#### Fixes

lib/grape/endpoint.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def call(env)
146146
end
147147

148148
def call!(env)
149+
extend helpers
150+
149151
env['api.endpoint'] = self
150152
if options[:app]
151153
options[:app].call(env)
@@ -372,7 +374,6 @@ def run(env)
372374
@params = @request.params
373375
@headers = @request.headers
374376

375-
extend helpers
376377
cookies.read(@request)
377378

378379
run_filters befores
@@ -426,8 +427,21 @@ def build_middleware
426427
end
427428
end
428429

429-
b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic
430-
b.use Rack::Auth::Digest::MD5, settings[:auth][:realm], settings[:auth][:opaque], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_digest
430+
if settings[:auth]
431+
auth_proc = settings[:auth][:proc]
432+
auth_proc_context = self
433+
auth_middleware = {
434+
http_basic: { class: Rack::Auth::Basic, args: [settings[:auth][:realm]] },
435+
http_digest: { class: Rack::Auth::Digest::MD5, args: [settings[:auth][:realm], settings[:auth][:opaque]] }
436+
}[settings[:auth][:type]]
437+
438+
# evaluate auth proc in context of endpoint
439+
if auth_middleware
440+
b.use auth_middleware[:class], *auth_middleware[:args] do |*args|
441+
auth_proc_context.instance_exec(*args, &auth_proc)
442+
end
443+
end
444+
end
431445

432446
if settings[:version]
433447
b.use Grape::Middleware::Versioner.using(settings[:version_options][:using]),

spec/grape/api_spec.rb

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ def before
874874
end
875875
end
876876
end
877-
describe '.basic' do
877+
describe '.http_basic' do
878878
it 'protects any resources on the same scope' do
879879
subject.http_basic do |u, p|
880880
u == 'allow'
@@ -913,6 +913,51 @@ def before
913913
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
914914
last_response.status.should eql 200
915915
end
916+
917+
it 'has access to the current endpoint' do
918+
basic_auth_context = nil
919+
920+
subject.http_basic do |u, p|
921+
basic_auth_context = self
922+
923+
u == 'allow'
924+
end
925+
926+
subject.get(:hello) { "Hello, world." }
927+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
928+
basic_auth_context.should be_an_instance_of(Grape::Endpoint)
929+
end
930+
931+
it 'has access to helper methods' do
932+
subject.helpers do
933+
def authorize(u, p)
934+
u == 'allow' && p == 'whatever'
935+
end
936+
end
937+
938+
subject.http_basic do |u, p|
939+
authorize(u, p)
940+
end
941+
942+
subject.get(:hello) { "Hello, world." }
943+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
944+
last_response.status.should eql 200
945+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('disallow', 'whatever')
946+
last_response.status.should eql 401
947+
end
948+
949+
it 'can set instance variables accessible to routes' do
950+
subject.http_basic do |u, p|
951+
@hello = "Hello, world."
952+
953+
u == 'allow'
954+
end
955+
956+
subject.get(:hello) { @hello }
957+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
958+
last_response.status.should eql 200
959+
last_response.body.should eql "Hello, world."
960+
end
916961
end
917962

918963
describe '.logger' do

0 commit comments

Comments
 (0)