Skip to content

Commit

Permalink
allow to pass authorize params dynamically (marknadig#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slashek authored and marknadig committed Jul 31, 2018
1 parent 54d1529 commit ce14aae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lib/omniauth/strategies/azure_oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def client
options.client_secret = provider.client_secret
options.tenant_id =
provider.respond_to?(:tenant_id) ? provider.tenant_id : 'common'
options.base_azure_url =
options.base_azure_url =
provider.respond_to?(:base_azure_url) ? provider.base_azure_url : BASE_AZURE_URL

options.authorize_params = provider.authorize_params if provider.respond_to?(:authorize_params)
options.authorize_params.domain_hint = provider.domain_hint if provider.respond_to?(:domain_hint) && provider.domain_hint
options.authorize_params.prompt = request.params['prompt'] if request.params['prompt']
options.client_options.authorize_url = "#{options.base_azure_url}/#{options.tenant_id}/oauth2/authorize"
Expand All @@ -52,7 +53,7 @@ def client
tid: raw_info['tid']
}
end

def token_params
azure_resource = request.env['omniauth.params'] && request.env['omniauth.params']['azure_resource']
super.merge(resource: azure_resource || options.resource)
Expand Down
40 changes: 22 additions & 18 deletions spec/omniauth/strategies/azure_oauth2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,30 @@ module JWT; end
subject do
OmniAuth::Strategies::AzureOauth2.new(app, {client_id: 'id', client_secret: 'secret', tenant_id: 'tenant', base_azure_url: 'https://login.microsoftonline.de'}.merge(options))
end

describe '#client' do
it 'has correct authorize url' do
allow(subject).to receive(:request) { request }
expect(subject.client.options[:authorize_url]).to eql('https://login.microsoftonline.de/tenant/oauth2/authorize')
end

it 'has correct authorize params' do
allow(subject).to receive(:request) { request }
subject.client
expect(subject.authorize_params[:domain_hint]).to be_nil
end

it 'has correct token url' do
allow(subject).to receive(:request) { request }
expect(subject.client.options[:token_url]).to eql('https://login.microsoftonline.de/tenant/oauth2/token')
end

it 'has correct token params' do
allow(subject).to receive(:request) { request }
subject.client
expect(subject.token_params[:resource]).to eql('00000002-0000-0000-c000-000000000000')
end

describe "overrides" do
it 'should override domain_hint' do
@options = {domain_hint: 'hint'}
Expand All @@ -97,7 +97,7 @@ module JWT; end
end
end
end

describe 'static common configuration' do
let(:options) { @options || {} }
subject do
Expand Down Expand Up @@ -137,6 +137,9 @@ def tenant_id
'tenant'
end

def authorize_params
{ custom_option: 'value' }
end
}
}

Expand All @@ -156,6 +159,7 @@ def tenant_id
it 'has correct authorize params' do
subject.client
expect(subject.authorize_params[:domain_hint]).to be_nil
expect(subject.authorize_params[:custom_option]).to eql('value')
end

it 'has correct token url' do
Expand Down Expand Up @@ -184,52 +188,52 @@ def tenant_id
Class.new {
def initialize(strategy)
end

def client_id
'id'
end

def client_secret
'secret'
end

def tenant_id
'tenant'
end

def base_azure_url
'https://login.microsoftonline.de'
end
}
}

subject do
OmniAuth::Strategies::AzureOauth2.new(app, provider_klass)
end

before do
allow(subject).to receive(:request) { request }
end

describe '#client' do
it 'has correct authorize url' do
expect(subject.client.options[:authorize_url]).to eql('https://login.microsoftonline.de/tenant/oauth2/authorize')
end

it 'has correct authorize params' do
subject.client
expect(subject.authorize_params[:domain_hint]).to be_nil
end

it 'has correct token url' do
expect(subject.client.options[:token_url]).to eql('https://login.microsoftonline.de/tenant/oauth2/token')
end

it 'has correct token params' do
subject.client
expect(subject.token_params[:resource]).to eql('00000002-0000-0000-c000-000000000000')
end

# todo: how to get this working?
# describe "overrides" do
# it 'should override domain_hint' do
Expand All @@ -239,7 +243,7 @@ def base_azure_url
# end
# end
end

end

describe 'dynamic common configuration' do
Expand Down

0 comments on commit ce14aae

Please sign in to comment.