Skip to content

Commit

Permalink
Merge pull request #31 from Jureamer/b2c_login
Browse files Browse the repository at this point in the history
Changes in URL branching
  • Loading branch information
pond authored Oct 16, 2024
2 parents ec41d3d + 4f327c8 commit 5b480b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
18 changes: 11 additions & 7 deletions lib/omniauth/strategies/azure_activedirectory_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,19 @@ def client

options.custom_policy =
provider.respond_to?(:custom_policy) ? provider.custom_policy : nil
options.tenant_name =
provider.respond_to?(:tenant_name) ? provider.tenant_name : nil

oauth2 = provider.respond_to?(:adfs?) && provider.adfs? ? 'oauth2' : 'oauth2/v2.0'
options.client_options.authorize_url = "#{options.base_azure_url}/#{options.tenant_id}/#{oauth2}/authorize"
options.client_options.token_url =
if options.custom_policy
"#{options.base_azure_url}/#{options.tenant_id}/#{options.custom_policy}/#{oauth2}/token"
else
"#{options.base_azure_url}/#{options.tenant_id}/#{oauth2}/token"
end

base_url = if options.custom_policy && options.tenant_name
"https://#{options.tenant_name}.b2clogin.com/#{options.tenant_name}.onmicrosoft.com/#{options.custom_policy}"
else
"#{options.base_azure_url}/#{options.tenant_id}"
end

options.client_options.authorize_url = "#{base_url}/#{oauth2}/authorize"
options.client_options.token_url = "#{base_url}/#{oauth2}/token"

super
end
Expand Down
16 changes: 12 additions & 4 deletions spec/omniauth/strategies/azure_activedirectory_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
expect(subject.client.options[:token_url]).to eql('https://login.microsoftonline.com/tenant/oauth2/v2.0/token')
end

context 'when a custom policy is present' do
it 'includes custom policy in token url' do
@options = { custom_policy: 'my_policy' }
context 'when a custom policy is present and start with b2c and tenant_name is present for b2c login' do
it 'includes custom policy and tenane name in authorize url' do
@options = { tenant_name: "test", custom_policy: 'my_policy' }
allow(subject).to receive(:request) { request }
expect(subject.client.options[:token_url]).to eql('https://login.microsoftonline.com/tenant/my_policy/oauth2/v2.0/token')
expect(subject.client.options[:token_url]).to eql('https://test.b2clogin.com/test.onmicrosoft.com/my_policy/oauth2/v2.0/token')
end
end

Expand Down Expand Up @@ -104,6 +104,14 @@
expect(subject.authorize_params[:scope]).to eql('openid profile email')
end

context 'when a custom policy is present and start with b2c and tenant_name is present for b2c login' do
it 'includes custom policy and tenane name in authorize url' do
@options = { tenant_name: "test", custom_policy: 'my_policy' }
allow(subject).to receive(:request) { request }
expect(subject.client.options[:authorize_url]).to eql('https://test.b2clogin.com/test.onmicrosoft.com/my_policy/oauth2/v2.0/authorize')
end
end

describe "overrides" do
it 'should override domain_hint' do
@options = {domain_hint: 'hint'}
Expand Down

0 comments on commit 5b480b2

Please sign in to comment.