Skip to content

Commit c888cf7

Browse files
committed
Allow to use Proc for generation of state
1 parent 464fcef commit c888cf7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/omniauth/strategies/oauth2.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def self.inherited(subclass)
2424
option :client_secret, nil
2525
option :client_options, {}
2626
option :authorize_params, {}
27-
option :authorize_options, [:scope]
27+
option :authorize_options, [:scope, :state]
2828
option :token_params, {}
2929
option :token_options, []
3030
option :auth_token_params, {}
@@ -100,7 +100,11 @@ def deep_symbolize(options)
100100
def options_for(option)
101101
hash = {}
102102
options.send(:"#{option}_options").select { |key| options[key] }.each do |key|
103-
hash[key.to_sym] = options[key]
103+
hash[key.to_sym] = if options[key].respond_to?(:call)
104+
options[key].call(env)
105+
else
106+
options[key]
107+
end
104108
end
105109
hash
106110
end

spec/omniauth/strategies/oauth2_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ def app
5252
instance = subject.new("abc", "def", :authorize_options => [:scope, :foo, :state], :scope => "bar", :foo => "baz")
5353
expect(instance.authorize_params["scope"]).to eq("bar")
5454
expect(instance.authorize_params["foo"]).to eq("baz")
55+
expect(instance.authorize_params["state"]).not_to be_empty
5556
end
5657

5758
it "includes random state in the authorize params" do
5859
instance = subject.new("abc", "def")
5960
expect(instance.authorize_params.keys).to eq(["state"])
6061
expect(instance.session["omniauth.state"]).not_to be_empty
6162
end
63+
64+
it "includes custom state in the authorize params" do
65+
instance = subject.new("abc", "def", state: Proc.new { "qux" } )
66+
expect(instance.authorize_params.keys).to eq(["state"])
67+
expect(instance.session["omniauth.state"]).to eq("qux")
68+
end
6269
end
6370

6471
describe "#token_params" do

0 commit comments

Comments
 (0)