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

Support for concurrent authentication (multiple states and origins) #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions lib/omniauth/strategies/oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def authorize_params
@env ||= {}
@env["rack.session"] ||= {}
end
session["omniauth.state"] = params[:state]
session["omniauth.states"] ||= []
session["omniauth.states"] << params[:state]

session["omniauth.state_origins"] ||= {}
session["omniauth.state_origins"][params[:state]] = session['omniauth.origin']
params
end

Expand All @@ -67,11 +71,15 @@ def callback_phase # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength
error = request.params["error_reason"] || request.params["error"]
if error
fail!(error, CallbackError.new(request.params["error"], request.params["error_description"] || request.params["error_reason"], request.params["error_uri"]))
elsif !options.provider_ignores_state && (request.params["state"].to_s.empty? || request.params["state"] != session.delete("omniauth.state"))
elsif !options.provider_ignores_state && (request.params["state"].to_s.empty? || session["omniauth.states"].empty? || !session["omniauth.states"].delete(request.params["state"]))
fail!(:csrf_detected, CallbackError.new(:csrf_detected, "CSRF detected"))
else
self.access_token = build_access_token
self.access_token = access_token.refresh! if access_token.expired?

if session['omniauth.state_origins'] && session['omniauth.state_origins'][request.params['state']]
env['omniauth.origin'] = session['omniauth.state_origins'].delete(request.params['state'])
end
super
end
rescue ::OAuth2::Error, CallbackError => e
Expand Down