Skip to content

Commit

Permalink
Merge branch 'master' into hotfix/for_rack_3_1
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling authored Oct 3, 2024
2 parents c71a1e0 + e1ec9ce commit 4d969dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
13 changes: 5 additions & 8 deletions lib/omniauth/strategies/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,17 @@ def registration_phase
info { identity.info }

def registration_path
options[:registration_path] || "#{path_prefix}/#{name}/register"
options[:registration_path] || "#{script_name}#{path_prefix}/#{name}/register"
end

def on_registration_path?
on_path?(registration_path)
end

def identity
if options[:locate_conditions].is_a? Proc
conditions = instance_exec(request, &options[:locate_conditions])
conditions.to_hash
else
conditions = options[:locate_conditions].to_hash
end
conditions = options[:locate_conditions]
conditions = conditions.is_a?(Proc) ? instance_exec(request, &conditions).to_hash : conditions.to_hash

@identity ||= model.authenticate(conditions, request.params['password'])
end

Expand Down Expand Up @@ -169,7 +166,7 @@ def registration_failure(message)

def registration_result
if @identity.persisted?
env['PATH_INFO'] = callback_path
env['PATH_INFO'] = "#{path_prefix}/#{name}/callback"
callback_phase
else
registration_failure(options[:registration_failure_message])
Expand Down
60 changes: 47 additions & 13 deletions spec/omniauth/strategies/identity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let(:auth_hash) { env_hash['omniauth.auth'] }
let(:identity_hash) { env_hash['omniauth.identity'] }
let(:identity_options) { {} }
let(:app_options) { {} }
let(:anon_ar) do
AnonymousActiveRecord.generate(
parent_klass: 'OmniAuth::Identity::Models::ActiveRecord',
Expand All @@ -24,25 +25,58 @@ def balloon
end
end

# customize rack app for testing, if block is given, reverts to default
# rack app after testing is done
def set_app!(identity_options = {})
old_app = app
# the rack testing framework will call this method (to get app) as needed to run tests
def app
opts = identity_options.reverse_merge({ model: anon_ar })
script_name = app_options[:script_name]
self.app = Rack::Builder.app do
use Rack::Session::Cookie, secret: '1234567890qwertyuiop'
use OmniAuth::Strategies::Identity, identity_options
if script_name
map script_name do
use OmniAuth::Strategies::Identity, opts
end
else
use OmniAuth::Strategies::Identity, opts
end
run ->(env) { [404, { 'env' => env }, ['HELLO!']] }
end
if block_given?
yield
self.app = old_app
end
app
end

before do
opts = identity_options.reverse_merge({ model: anon_ar })
set_app!(opts)
describe 'path handling' do
script_names = [nil, '/my_path']
path_prefixes = ['/auth', '/my_auth', '']
provider_names = ['identity', 'my_id']
script_names.product(path_prefixes, provider_names).each do |script_name, path_prefix, provider_name|
ext_base_path = "#{script_name}#{path_prefix}/#{provider_name}"
ext_callback_path = "#{ext_base_path}/callback"
ext_register_path = "#{ext_base_path}/register"

context "with base path '#{ext_base_path}'" do
let(:app_options) { { script_name: script_name } }
let(:identity_options) {
{ enable_registration: true, enable_login: true, path_prefix: path_prefix, name: provider_name }
}
it "calls app" do
get "#{script_name}/hello/world"
expect(last_response.body).to eq('HELLO!')
end
it '#request_phase displays form' do
get ext_base_path
expect(last_response.body).not_to eq('HELLO!')
expect(last_response.body).to include('<form')
expect(last_response.body).to include("action='#{ext_callback_path}'")
expect(last_response.body).to include("href='#{ext_register_path}'")
end
it '#callback_phase is handled' do
get ext_callback_path
expect(last_response.body).not_to eq('HELLO!')
end
it '#registration_phase is handled' do
get ext_register_path
expect(last_response.body).not_to eq('HELLO!')
end
end
end
end

describe '#request_phase' do
Expand Down

0 comments on commit 4d969dc

Please sign in to comment.