Skip to content

Commit

Permalink
Merge pull request #88 from tejanium/ts/authenticator-in-controller
Browse files Browse the repository at this point in the history
Evaluate authenticator in the controller context
  • Loading branch information
pond authored Jan 11, 2024
2 parents 3394c6a + 065c4dd commit 8b3bd8d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/controllers/scimitar/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ def authenticate

def authenticated?
result = if Scimitar.engine_configuration.basic_authenticator.present?
authenticate_with_http_basic(&Scimitar.engine_configuration.basic_authenticator)
authenticate_with_http_basic do |username, password|
instance_exec(username, password, &Scimitar.engine_configuration.basic_authenticator)
end
end

result ||= if Scimitar.engine_configuration.token_authenticator.present?
authenticate_with_http_token(&Scimitar.engine_configuration.token_authenticator)
authenticate_with_http_token do |token, options|
instance_exec(token, options, &Scimitar.engine_configuration.token_authenticator)
end
end

return result
Expand Down
48 changes: 48 additions & 0 deletions spec/controllers/scimitar/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,54 @@ def index
end
end

context 'authenticator evaluated within controller context' do
before do
Scimitar.engine_configuration = Scimitar::EngineConfiguration.new(
token_authenticator: Proc.new do | token, options |
token == valid_token
end
)
end

controller do
def index
render json: { 'message' => 'cool, cool!' }, format: :scim
end

def valid_token
'B'
end
end

it 'renders success when valid creds are given' do
request.env['HTTP_AUTHORIZATION'] = 'Bearer B'

get :index, params: { format: :scim }
expect(response).to be_ok
expect(JSON.parse(response.body)).to eql({ 'message' => 'cool, cool!' })
expect(response.headers['WWW-Authenticate']).to eql('Bearer')
end

it 'renders failure with bad token' do
request.env['HTTP_AUTHORIZATION'] = 'Bearer Invalid'

get :index, params: { format: :scim }
expect(response).not_to be_ok
end

it 'renders failure with blank token' do
request.env['HTTP_AUTHORIZATION'] = 'Bearer'

get :index, params: { format: :scim }
expect(response).not_to be_ok
end

it 'renders failure with missing header' do
get :index, params: { format: :scim }
expect(response).not_to be_ok
end
end

context 'authenticated' do
controller do
rescue_from StandardError, with: :handle_resource_not_found
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
config.use_transactional_fixtures = true

Kernel.srand config.seed

config.around :each do | example |
original_engine_configuration = Scimitar.instance_variable_get('@engine_configuration')
example.run()
ensure
Scimitar.instance_variable_set('@engine_configuration', original_engine_configuration)
end
end

# ============================================================================
Expand Down

0 comments on commit 8b3bd8d

Please sign in to comment.