99
1010module OmniAuth
1111 using Slack ::OAuth2Refinements
12-
12+
1313 module Strategies
14-
14+
1515 # This is the OmniAuth strategy for Slack.
1616 # It is used as Rack middleware.
1717 #
@@ -20,34 +20,35 @@ module Strategies
2020 # end
2121 #
2222 class Slack < OmniAuth ::Strategies ::OAuth2
23- include OmniAuth ::Slack ::Debug
24-
25-
23+ include OmniAuth ::Slack ::Debug
24+
25+
2626 ### Options ###
2727
2828 # Master list of authorization options handled by omniauth-slack.
2929 # See below for redirect_uri.
30- #
30+ #
3131 AUTH_OPTIONS = %i( scope user_scope team team_domain )
32-
32+
3333 debug { "#{ self } setting up default options" }
34-
34+
3535 # Default strategy name
3636 option :name , 'slack'
37-
37+
3838 # Options that can be passed with provider authorization URL.
3939 option :authorize_options , AUTH_OPTIONS - %i( team_domain )
40-
40+
4141 # OAuth2::Client options.
4242 option :client_options , {
43- site : 'https://slack.com' ,
44- authorize_url : '/oauth/v2/authorize' ,
45- token_url : '/api/oauth.v2.access' ,
43+ access_token_class : OmniAuth ::Slack ::OAuth2 ::AccessToken ,
4644 auth_scheme : :basic_auth ,
47- raise_errors : false , # MUST be false to allow Slack's get-token response from v2 API.
45+ authorize_url : '/oauth/v2/authorize' ,
4846 history : Array . new ,
47+ raise_errors : false , # MUST be false to allow Slack's get-token response from v2 API.
48+ site : 'https://slack.com' ,
49+ token_url : '/api/oauth.v2.access' ,
4950 }
50-
51+
5152 # Authorization token-exchange API call options.
5253 option :auth_token_params , {
5354 mode : :query ,
@@ -56,20 +57,20 @@ class Slack < OmniAuth::Strategies::OAuth2
5657
5758
5859 ### Omniauth Slack custom options ###
59-
60+
6061 # redirect_uri does not need to be in authorize_options,
6162 # since it inserted anyway by omniauth-oauth2 during both
6263 # the request (authorization) phase and the callback (get-token) phase.
6364 # The magic of redirect_uri actually happens in the callback_url method.
6465 option :redirect_uri
65-
66+
6667 # Options allowed to pass from omniauth /auth/<provider> URL
6768 # to provider authorization URL.
6869 option :pass_through_params , %i( team )
69-
70+
7071
7172 ### Data ###
72-
73+
7374 # User ID is not guaranteed to be globally unique across all Slack users.
7475 # The combination of user ID and team ID, on the other hand, is guaranteed
7576 # to be globally unique.
@@ -150,7 +151,7 @@ def authorize_params
150151 session [ 'omniauth.authorize_params' ] = prms
151152 end
152153 end
153-
154+
154155 # Pre-sets env vars for super.
155156 #
156157 # OmniAuth callback phase to extract session var for
@@ -162,7 +163,7 @@ def callback_phase #(*args)
162163 env [ 'omniauth.authorize_params' ] = session . delete ( 'omniauth.authorize_params' )
163164 super
164165 end
165-
166+
166167 # Returns OmniAuth::Slack::AuthHash
167168 #
168169 # Super result is converted to plain hash first,
@@ -171,7 +172,7 @@ def callback_phase #(*args)
171172 def auth_hash
172173 OmniAuth ::Slack ::AuthHash . new super . to_hash
173174 end
174-
175+
175176 # Uses `OmniAuth::Slack::OAuth2::Client` to handle Slack-specific features.
176177 #
177178 # * Logs API requests with OmniAuth.logger.
@@ -185,9 +186,9 @@ def client
185186 @client ||= (
186187 team_domain = ( pass_through_params . include? ( 'team_domain' ) && request . params [ 'team_domain' ] ) ? request . params [ 'team_domain' ] : options . team_domain
187188 new_client = OmniAuth ::Slack ::OAuth2 ::Client . new ( options . client_id , options . client_secret , deep_symbolize ( options . client_options . merge ( { subdomain :team_domain } ) ) )
188-
189+
189190 debug { "Strategy #{ self } using Client #{ new_client } with callback_url #{ callback_url } " }
190-
191+
191192 new_client
192193 )
193194 end
@@ -198,18 +199,18 @@ def client
198199 def callback_url
199200 options . redirect_uri || full_host + script_name + callback_path
200201 end
201-
202+
202203 ### Possibly obsolete
203204 #
204205 # def user_id
205206 # # access_token['user_id'] || access_token['user'].to_h['id'] || access_token['authorizing_user'].to_h['user_id']
206207 # access_or_user_token&.user_id
207208 # end
208- #
209+ #
209210 # def team_id
210211 # access_token&.team_id
211212 # end
212-
213+
213214 # Gets and decodes :pass_through_params option.
214215 #
215216 def pass_through_params
@@ -244,13 +245,13 @@ def raw_info
244245 debug { "Retrieved raw_info (size #{ @raw_info . size } ) (object_id #{ @raw_info . object_id } )" }
245246 @raw_info
246247 end
247-
248+
248249 # Gets 'authed_user' sub-token from main access token.
249250 #
250251 def user_token
251252 access_token &.user_token
252253 end
253-
254+
254255 # Gets main access_token, if valid, otherwise gets user_token, if valid.
255256 # Handles Slack v1 and v2 API (v2 is non-conformant with OAUTH2 spec).
256257 def access_or_user_token
@@ -262,14 +263,14 @@ def access_or_user_token
262263 access_token
263264 end
264265 end
265-
266+
266267 def scopes_requested
267268 # omniauth.authorize_params is an enhancement to omniauth functionality for omniauth-slack.
268269 out = {
269270 scope : env [ 'omniauth.authorize_params' ] . to_h [ 'scope' ] ,
270271 user_scope : env [ 'omniauth.authorize_params' ] . to_h [ 'user_scope' ]
271272 }
272-
273+
273274 debug { "scopes_requested: #{ out } " }
274275 return out
275276 end
0 commit comments