From c888cf73efe8e9abadfd5f5a2646e87536ab37f3 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 26 Jul 2016 14:05:23 +0300 Subject: [PATCH] Allow to use Proc for generation of state --- lib/omniauth/strategies/oauth2.rb | 8 ++++++-- spec/omniauth/strategies/oauth2_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/omniauth/strategies/oauth2.rb b/lib/omniauth/strategies/oauth2.rb index 3ffff1b..a5ad6d6 100644 --- a/lib/omniauth/strategies/oauth2.rb +++ b/lib/omniauth/strategies/oauth2.rb @@ -24,7 +24,7 @@ def self.inherited(subclass) option :client_secret, nil option :client_options, {} option :authorize_params, {} - option :authorize_options, [:scope] + option :authorize_options, [:scope, :state] option :token_params, {} option :token_options, [] option :auth_token_params, {} @@ -100,7 +100,11 @@ def deep_symbolize(options) def options_for(option) hash = {} options.send(:"#{option}_options").select { |key| options[key] }.each do |key| - hash[key.to_sym] = options[key] + hash[key.to_sym] = if options[key].respond_to?(:call) + options[key].call(env) + else + options[key] + end end hash end diff --git a/spec/omniauth/strategies/oauth2_spec.rb b/spec/omniauth/strategies/oauth2_spec.rb index 0bffcde..4e98ef9 100644 --- a/spec/omniauth/strategies/oauth2_spec.rb +++ b/spec/omniauth/strategies/oauth2_spec.rb @@ -52,6 +52,7 @@ def app instance = subject.new("abc", "def", :authorize_options => [:scope, :foo, :state], :scope => "bar", :foo => "baz") expect(instance.authorize_params["scope"]).to eq("bar") expect(instance.authorize_params["foo"]).to eq("baz") + expect(instance.authorize_params["state"]).not_to be_empty end it "includes random state in the authorize params" do @@ -59,6 +60,12 @@ def app expect(instance.authorize_params.keys).to eq(["state"]) expect(instance.session["omniauth.state"]).not_to be_empty end + + it "includes custom state in the authorize params" do + instance = subject.new("abc", "def", state: Proc.new { "qux" } ) + expect(instance.authorize_params.keys).to eq(["state"]) + expect(instance.session["omniauth.state"]).to eq("qux") + end end describe "#token_params" do