@@ -40,6 +40,7 @@ def self.inherited(subclass)
40
40
} ,
41
41
:code_challenge_method => "S256" ,
42
42
}
43
+ option :state_container , StateContainer . new
43
44
44
45
attr_accessor :access_token
45
46
@@ -60,7 +61,7 @@ def request_phase
60
61
end
61
62
62
63
def authorize_params # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
63
- options . authorize_params [ :state ] = SecureRandom . hex ( 24 )
64
+ options . authorize_params [ :state ] = new_state
64
65
65
66
if OmniAuth . config . test_mode
66
67
@env ||= { }
@@ -72,7 +73,7 @@ def authorize_params # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
72
73
. merge ( pkce_authorize_params )
73
74
74
75
session [ "omniauth.pkce.verifier" ] = options . pkce_verifier if options . pkce
75
- session [ "omniauth.state" ] = params [ :state ]
76
+ options . state_container . store ( self , params [ :state ] )
76
77
77
78
params
78
79
end
@@ -83,7 +84,7 @@ def token_params
83
84
84
85
def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
85
86
error = request . params [ "error_reason" ] || request . params [ "error" ]
86
- if !options . provider_ignores_state && ( request . params [ "state" ] . to_s . empty? || request . params [ "state" ] != session . delete ( "omniauth.state" ) )
87
+ if !options . provider_ignores_state && ( request . params [ "state" ] . to_s . empty? || request . params [ "state" ] != options . state_container . take ( self ) )
87
88
fail! ( :csrf_detected , CallbackError . new ( :csrf_detected , "CSRF detected" ) )
88
89
elsif error
89
90
fail! ( error , CallbackError . new ( request . params [ "error" ] , request . params [ "error_description" ] || request . params [ "error_reason" ] , request . params [ "error_uri" ] ) )
@@ -100,6 +101,10 @@ def callback_phase # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexi
100
101
fail! ( :failed_to_connect , e )
101
102
end
102
103
104
+ def new_state
105
+ SecureRandom . hex ( 24 )
106
+ end
107
+
103
108
protected
104
109
105
110
def pkce_authorize_params
0 commit comments