1+ # frozen_string_literal: true
2+
13require "oauth2"
24require "omniauth"
35require "securerandom"
@@ -24,7 +26,7 @@ def self.inherited(subclass)
2426 option :client_secret , nil
2527 option :client_options , { }
2628 option :authorize_params , { }
27- option :authorize_options , [ : scope, : state]
29+ option :authorize_options , %i[ scope state ]
2830 option :token_params , { }
2931 option :token_options , [ ]
3032 option :auth_token_params , { }
@@ -52,15 +54,7 @@ def request_phase
5254 def authorize_params
5355 verifier = SecureRandom . hex ( 64 )
5456
55- if options . pkce
56- # NOTE: see https://tools.ietf.org/html/rfc7636#appendix-A
57- challenge = Base64
58- . urlsafe_encode64 ( Digest ::SHA2 . digest ( verifier ) )
59- . split ( "=" )
60- . first
61- options . authorize_params [ :code_challenge ] = challenge
62- options . authorize_params [ :code_challenge_method ] = "S256"
63- end
57+ pkce_authorize_params! ( verifier )
6458
6559 options . authorize_params [ :state ] = SecureRandom . hex ( 24 )
6660 params = options . authorize_params . merge ( options_for ( "authorize" ) )
@@ -70,16 +64,15 @@ def authorize_params
7064 @env [ "rack.session" ] ||= { }
7165 end
7266
73- session [ "omniauth.pkce.verifier" ] = verifier if options . pkce
74- session [ "omniauth.state" ] = params [ :state ]
67+ build_authorize_session! ( params , verifier )
7568 params
7669 end
7770
7871 def token_params
7972 options . token_params . merge ( options_for ( "token" ) ) . merge ( pkce_token_params )
8073 end
8174
82- def callback_phase # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, PerceivedComplexity
75+ def callback_phase # rubocop:disable Metrics/ AbcSize, Metrics/ CyclomaticComplexity, Metrics/ MethodLength, Metrics/ PerceivedComplexity
8376 error = request . params [ "error_reason" ] || request . params [ "error" ]
8477 if error
8578 fail! ( error , CallbackError . new ( request . params [ "error" ] , request . params [ "error_description" ] || request . params [ "error_reason" ] , request . params [ "error_uri" ] ) )
@@ -100,10 +93,27 @@ def callback_phase # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength
10093
10194 protected
10295
96+ def build_authorize_session! ( params , verifier )
97+ session [ "omniauth.pkce.verifier" ] = verifier if options . pkce
98+ session [ "omniauth.state" ] = params [ :state ]
99+ end
100+
101+ def pkce_authorize_params! ( verifier )
102+ return unless options . pkce
103+
104+ # NOTE: see https://tools.ietf.org/html/rfc7636#appendix-A
105+ challenge = Base64
106+ . urlsafe_encode64 ( Digest ::SHA2 . digest ( verifier ) )
107+ . split ( "=" )
108+ . first
109+ options . authorize_params [ :code_challenge ] = challenge
110+ options . authorize_params [ :code_challenge_method ] = "S256"
111+ end
112+
103113 def pkce_token_params
104114 return { } unless options . pkce
105115
106- { code_verifier : session . delete ( "omniauth.pkce.verifier" ) }
116+ { :code_verifier => session . delete ( "omniauth.pkce.verifier" ) }
107117 end
108118
109119 def build_access_token
@@ -121,10 +131,10 @@ def options_for(option)
121131 hash = { }
122132 options . send ( :"#{ option } _options" ) . select { |key | options [ key ] } . each do |key |
123133 hash [ key . to_sym ] = if options [ key ] . respond_to? ( :call )
124- options [ key ] . call ( env )
125- else
126- options [ key ]
127- end
134+ options [ key ] . call ( env )
135+ else
136+ options [ key ]
137+ end
128138 end
129139 hash
130140 end
0 commit comments