From dbe53fab569aaedbea5daa161ce0e6a0e862e961 Mon Sep 17 00:00:00 2001 From: Kevin Mehlbrech Date: Sun, 2 Aug 2015 13:27:46 -0700 Subject: [PATCH 1/4] updating oauth2 client to accept lambda functions as parameter (for dynamic credential switching in a multitenant environment --- lib/omniauth/strategies/oauth2.rb | 4 +++- spec/omniauth/strategies/oauth2_spec.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/omniauth/strategies/oauth2.rb b/lib/omniauth/strategies/oauth2.rb index 0197452..31a2615 100644 --- a/lib/omniauth/strategies/oauth2.rb +++ b/lib/omniauth/strategies/oauth2.rb @@ -33,7 +33,9 @@ def self.inherited(subclass) attr_accessor :access_token def client - ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options)) + client_id = options.client_id.respond_to?(:call) ? options.client_id.call : options.client_id + client_secret = options.client_secret.respond_to?(:call) ? options.client_secret.call : options.client_secret + ::OAuth2::Client.new(client_id, client_secret, deep_symbolize(options.client_options)) end def callback_url diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index 0bffcde..698d7a0 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -37,6 +37,18 @@ def app instance = subject.new(app, :client_options => {"ssl" => {"ca_path" => "foo"}}) expect(instance.client.options[:connection_opts][:ssl]).to eq(:ca_path => "foo") end + + it "allows client_id to be passed as a lambda function" do + client_id = lambda { 'abc' } + instance = subject.new(Object, client_id, 'def') + expect(instance.client.id).to eq('abc') + end + + it "allows client_secret to be passed as a lambda function" do + client_secret = lambda { 'def' } + instance = subject.new(Object, 'abc', client_secret) + expect(instance.client.secret).to eq('def') + end end describe "#authorize_params" do From 9cf47a84cd48c891683a73ee80f4e7a6f891a275 Mon Sep 17 00:00:00 2001 From: Kevin Mehlbrech Date: Sun, 2 Aug 2015 13:46:18 -0700 Subject: [PATCH 2/4] updating lambda syntax in spec file --- spec/omniauth/strategies/oauth2_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index 698d7a0..abe5d59 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -39,13 +39,13 @@ def app end it "allows client_id to be passed as a lambda function" do - client_id = lambda { 'abc' } + client_id = -> { 'abc' } instance = subject.new(Object, client_id, 'def') expect(instance.client.id).to eq('abc') end it "allows client_secret to be passed as a lambda function" do - client_secret = lambda { 'def' } + client_secret = -> { 'def' } instance = subject.new(Object, 'abc', client_secret) expect(instance.client.secret).to eq('def') end From ce64aaddb20724a1a5b7ff3707eb781fb5361221 Mon Sep 17 00:00:00 2001 From: Kevin Mehlbrech Date: Sun, 2 Aug 2015 13:48:01 -0700 Subject: [PATCH 3/4] updating new specs to use double quoted strings --- spec/omniauth/strategies/oauth2_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index abe5d59..1ebc558 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -39,15 +39,15 @@ def app end it "allows client_id to be passed as a lambda function" do - client_id = -> { 'abc' } - instance = subject.new(Object, client_id, 'def') - expect(instance.client.id).to eq('abc') + client_id = -> { "abc" } + instance = subject.new(Object, client_id, "def") + expect(instance.client.id).to eq("abc") end it "allows client_secret to be passed as a lambda function" do - client_secret = -> { 'def' } - instance = subject.new(Object, 'abc', client_secret) - expect(instance.client.secret).to eq('def') + client_secret = -> { "def" } + instance = subject.new(Object, "abc", client_secret) + expect(instance.client.secret).to eq("def") end end From 9faef42b08e41f727a57efc302e0320fcd89c000 Mon Sep 17 00:00:00 2001 From: Kevin Mehlbrech Date: Sun, 2 Aug 2015 13:54:01 -0700 Subject: [PATCH 4/4] fixing rubocop problems --- lib/omniauth/strategies/oauth2.rb | 10 ++++++++-- spec/omniauth/strategies/oauth2_spec.rb | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/omniauth/strategies/oauth2.rb b/lib/omniauth/strategies/oauth2.rb index 31a2615..b17a7a3 100644 --- a/lib/omniauth/strategies/oauth2.rb +++ b/lib/omniauth/strategies/oauth2.rb @@ -33,11 +33,17 @@ def self.inherited(subclass) attr_accessor :access_token def client - client_id = options.client_id.respond_to?(:call) ? options.client_id.call : options.client_id - client_secret = options.client_secret.respond_to?(:call) ? options.client_secret.call : options.client_secret ::OAuth2::Client.new(client_id, client_secret, deep_symbolize(options.client_options)) end + def client_id + options.client_id.respond_to?(:call) ? options.client_id.call : options.client_id + end + + def client_secret + options.client_secret.respond_to?(:call) ? options.client_secret.call : options.client_secret + end + def callback_url full_host + script_name + callback_path end diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index 1ebc558..af06b36 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -39,13 +39,13 @@ def app end it "allows client_id to be passed as a lambda function" do - client_id = -> { "abc" } + client_id = proc { "abc" } instance = subject.new(Object, client_id, "def") expect(instance.client.id).to eq("abc") end it "allows client_secret to be passed as a lambda function" do - client_secret = -> { "def" } + client_secret = proc { "def" } instance = subject.new(Object, "abc", client_secret) expect(instance.client.secret).to eq("def") end