Skip to content

Fix: broken registration_path when SCRIPT_NAME is not empty string #120

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

Merged
merged 1 commit into from
Sep 18, 2024
Merged
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
4 changes: 2 additions & 2 deletions lib/omniauth/strategies/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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?
Expand Down Expand Up @@ -169,7 +169,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