Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce State Container #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/omniauth-oauth2.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require "omniauth-oauth2/version"
require "omniauth/strategies/oauth2/state_container"
require "omniauth/strategies/oauth2"
11 changes: 8 additions & 3 deletions lib/omniauth/strategies/oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def self.inherited(subclass)
},
:code_challenge_method => "S256",
}
option :state_container, StateContainer.new

attr_accessor :access_token

Expand All @@ -60,7 +61,7 @@ def request_phase
end

def authorize_params # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
options.authorize_params[:state] = SecureRandom.hex(24)
options.authorize_params[:state] = new_state

if OmniAuth.config.test_mode
@env ||= {}
Expand All @@ -72,7 +73,7 @@ def authorize_params # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
.merge(pkce_authorize_params)

session["omniauth.pkce.verifier"] = options.pkce_verifier if options.pkce
session["omniauth.state"] = params[:state]
options.state_container.store(self, params[:state])

params
end
Expand All @@ -83,7 +84,7 @@ def token_params

def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
error = request.params["error_reason"] || request.params["error"]
if !options.provider_ignores_state && (request.params["state"].to_s.empty? || request.params["state"] != session.delete("omniauth.state"))
if !options.provider_ignores_state && (request.params["state"].to_s.empty? || request.params["state"] != options.state_container.take(self))
fail!(:csrf_detected, CallbackError.new(:csrf_detected, "CSRF detected"))
elsif error
fail!(error, CallbackError.new(request.params["error"], request.params["error_description"] || request.params["error_reason"], request.params["error_uri"]))
Expand All @@ -100,6 +101,10 @@ def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexi
fail!(:failed_to_connect, e)
end

def new_state
SecureRandom.hex(24)
end

protected

def pkce_authorize_params
Expand Down
15 changes: 15 additions & 0 deletions lib/omniauth/strategies/oauth2/state_container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module OmniAuth
module Strategies
class OAuth2
class StateContainer
def store(oauth2, state)
oauth2.session["omniauth.state"] = state
end

def take(oauth2)
oauth2.session.delete("omniauth.state")
end
end
end
end
end
29 changes: 29 additions & 0 deletions spec/omniauth/strategies/oauth2/state_container_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "helper"

describe OmniAuth::Strategies::OAuth2::StateContainer do
let(:state) { "random_state" }
let(:oauth2) { double("OAuth2", session: {}) }

describe "#save_state" do
it "saves the state in the session" do
subject.store(oauth2, state)

expect(oauth2.session["omniauth.state"]).to eq(state)
end
end

describe "#take_state" do
before do
subject.store(oauth2, state)
end

it "removes the state from the session" do
expect(oauth2.session).to include("omniauth.state")

taken_state = subject.take(oauth2)

expect(oauth2.session).not_to include("omniauth.state")
expect(taken_state).to eq(state)
end
end
end