Skip to content

Commit f202dfc

Browse files
Send access_token_class as client option
- In order to adap to the new code on Oauth2 we send the access_token_class as client param instead of sending it as param to get_token method
1 parent 9d20291 commit f202dfc

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

lib/omniauth-slack/oauth2/client.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ module OmniAuth
99
module Slack
1010
module OAuth2
1111
class Client < ::OAuth2::Client
12-
12+
1313
include OmniAuth::Slack::Debug
14-
14+
1515
#using StringRefinements
1616
#using OAuth2Refinements
17-
17+
1818
# If this is an array, request history will be stored.
1919
# Only store request history if each Client instance is relatively short-lived.
2020
#
@@ -33,9 +33,9 @@ class Client < ::OAuth2::Client
3333
#
3434
HISTORY_DEFAULT=nil
3535
SUBDOMAIN_DEFAULT=nil
36-
36+
3737
attr_accessor :logger, :history, :subdomain
38-
38+
3939
def initialize(*args, **options)
4040
debug{"args: #{args}"}
4141
super
@@ -45,22 +45,22 @@ def initialize(*args, **options)
4545
self.history && self.history = self.history.dup
4646
self.subdomain ||= options[:subdomain] || SUBDOMAIN_DEFAULT
4747
end
48-
48+
4949
# Wraps OAuth2::Client#get_token to pass in the omniauth-slack AccessToken class.
50-
def get_token(params, access_token_opts = {}, access_token_class = OmniAuth::Slack::OAuth2::AccessToken) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
50+
def get_token(params, access_token_opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
5151
debug{"params #{params}, access_token_opts #{access_token_opts}"}
52-
rslt = super(params, access_token_opts, access_token_class)
52+
rslt = super(params, access_token_opts)
5353
debug{"Client #{self} built AccessToken #{rslt}"}
5454
rslt
5555
end
56-
56+
5757
# Logs each API request and stores the API result in History array (if exists).
5858
#
5959
# Storage can be disabled by setting client_options: {history: false}.
6060
# Storage can be enabled by setting client_options: {history: Array.new}.
6161
# Storage is enabled by default, when client is created from Strategy.
62-
#
63-
#
62+
#
63+
#
6464
def request(*args)
6565
logger.debug "(slack) API request '#{args[0..1]}'." # in thread '#{Thread.current.object_id}'." # by Client '#{self}'
6666
debug{"API request args #{args}"}
@@ -88,8 +88,8 @@ def site(*args)
8888
super
8989
end
9090
end
91-
91+
9292
end
9393
end
9494
end
95-
end
95+
end

lib/omniauth/strategies/slack.rb

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
module 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

Comments
 (0)