Class: OmniAuth::Strategies::Slack

Inherits:
OAuth2
  • Object
show all
Includes:
OmniAuth::Slack::Debug
Defined in:
lib/omniauth/strategies/slack.rb

Overview

This is the OmniAuth strategy for Slack. It is used as Rack middleware.

use OmniAuth::Builder do
  provider :slack, OAUTH_KEY, OAUTH_SECRET, options...
end

Constant Summary collapse

AUTH_OPTIONS =

Master list of authorization options handled by omniauth-slack. See below for redirect_uri.

%i(scope user_scope team team_domain)

Constants included from OmniAuth::Slack::Debug

OmniAuth::Slack::Debug::LOG_ALL, OmniAuth::Slack::Debug::LOG_NONE

Instance Method Summary collapse

Methods included from OmniAuth::Slack::Debug

#debug, included

Methods included from OmniAuth::Slack::CallerMethodName

#caller_method_name, included

Instance Method Details

#access_or_user_tokenObject

Gets main access_token, if valid, otherwise gets user_token, if valid. Handles Slack v1 and v2 API (v2 is non-conformant with OAUTH2 spec).



256
257
258
259
260
261
262
263
264
# File 'lib/omniauth/strategies/slack.rb', line 256

def access_or_user_token
  if access_token&.token
    access_token
  elsif user_token
    user_token
  else
    access_token
  end
end

#auth_hashObject

Returns OmniAuth::Slack::AuthHash

Super result is converted to plain hash first, so AuthHash can do its recursive build magic.



171
172
173
# File 'lib/omniauth/strategies/slack.rb', line 171

def auth_hash
  OmniAuth::Slack::AuthHash.new super.to_hash
end

#authorize_paramsObject

Wraps OmniAuth::Oauth2#authorize_params so that specified params can be passed on to Slack authorization GET request. See https://github.com/omniauth/omniauth/issues/390



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/omniauth/strategies/slack.rb', line 139

def authorize_params
  super.tap do |prms|
    params_digest = prms.hash
    debug{"Using omniauth authorize_params #{prms}"}
    debug{"Considering request.params #{request.params}"}
    debug{"Considering pass_through_params #{pass_through_params}"}
    filtered_ptp = pass_through_params.reject{|o| o.to_s == 'team_domain'}
    filtered_rp  = request.params.reject{|k,v| !filtered_ptp.any?{|ptp| ptp.to_s == k.to_s}}
    debug{"Filtered request params #{filtered_rp}"}
    prms.merge! filtered_rp
    log(:debug, "Using modified authorize_params #{prms}") if prms.hash != params_digest
    session['omniauth.authorize_params'] = prms
  end
end

#callback_phaseObject

Pre-sets env vars for super.

OmniAuth callback phase to extract session var for omniauth.authorize_params into env (this is how omniauth does this).



159
160
161
162
163
164
# File 'lib/omniauth/strategies/slack.rb', line 159

def callback_phase #(*args)
  # This technique copied from OmniAuth::Strategy,
  # (this is how they do it for other omniauth objects).
  env['omniauth.authorize_params'] = session.delete('omniauth.authorize_params')
  super
end

#callback_urlObject

Dropping query_string from the default OmniAuth callback_url prevents some errors in call to /api/oauth.[v2.]access.



198
199
200
# File 'lib/omniauth/strategies/slack.rb', line 198

def callback_url
  options.redirect_uri || full_host + script_name + callback_path
end

#clientObject

Uses OmniAuth::Slack::OAuth2::Client to handle Slack-specific features.

  • Logs API requests with OmniAuth.logger.
  • Allows passthrough of Slack team_domain.
  • Enables/disables Client instance history.
  • Allows use of OmniAuth::Slack::OAuth2::AccessToken.

Returns instance of OmniAuth::Slack::OAuth2::Client.



184
185
186
187
188
189
190
191
192
193
# File 'lib/omniauth/strategies/slack.rb', line 184

def client
  @client ||= (
    team_domain = (pass_through_params.include?('team_domain') && request.params['team_domain']) ? request.params['team_domain'] : options.team_domain
    new_client = OmniAuth::Slack::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options.merge({subdomain:team_domain})))
  
    debug{"Strategy #{self} using Client #{new_client} with callback_url #{callback_url}"}
    
    new_client
  )
end

#pass_through_paramsObject

Gets and decodes :pass_through_params option.



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/omniauth/strategies/slack.rb', line 215

def pass_through_params
  ptp = [options.pass_through_params].flatten.compact
  case
    when ptp[0].to_s == 'all'
      options.pass_through_params = AUTH_OPTIONS
    when ptp[0].to_s == 'none'
      []
    else
      ptp
  end
end

#raw_infoObject

Points to client @history, which is filled with API response objects.



242
243
244
245
246
# File 'lib/omniauth/strategies/slack.rb', line 242

def raw_info
  @raw_info ||= access_token.client.history
  debug{"Retrieved raw_info (size #{@raw_info.size}) (object_id #{@raw_info.object_id})"}
  @raw_info
end

#scopes_requestedObject



266
267
268
269
270
271
272
273
274
275
# File 'lib/omniauth/strategies/slack.rb', line 266

def scopes_requested
  # omniauth.authorize_params is an enhancement to omniauth functionality for omniauth-slack.
  out = {
    scope: env['omniauth.authorize_params'].to_h['scope'],
    user_scope: env['omniauth.authorize_params'].to_h['user_scope']
  }
  
  debug{"scopes_requested: #{out}"}
  return out
end

#user_tokenObject

Gets 'authed_user' sub-token from main access token.



250
251
252
# File 'lib/omniauth/strategies/slack.rb', line 250

def user_token
  access_token&.user_token
end