class OmniAuth::Strategies::Slack

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

Constants

AUTH_OPTIONS

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

Public Instance Methods

access_or_user_token() click to toggle source

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).

# 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_hash() click to toggle source

Returns

Returns OmniAuth::Slack::AuthHash

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

Calls superclass method
# File lib/omniauth/strategies/slack.rb, line 171
def auth_hash
  OmniAuth::Slack::AuthHash.new super.to_hash
end
authorize_params() click to toggle source

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

Calls superclass method
# 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_phase() click to toggle source

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).

Calls superclass method
# 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_url() click to toggle source

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

# File lib/omniauth/strategies/slack.rb, line 198
def callback_url
  options.redirect_uri || full_host + script_name + callback_path
end
client() click to toggle source

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

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

# 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_params() click to toggle source

Gets and decodes :pass_through_params option.

# 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_info() click to toggle source

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

# 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_requested() click to toggle source
# 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_token() click to toggle source

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

# File lib/omniauth/strategies/slack.rb, line 250
def user_token
  access_token&.user_token
end